2013-04-11 116 views
0

如何將下面我的函數中的sheetData放入Hashmap中?HashMap是一個包含ArrayList的函數

私有靜態無效showExcelData(名單sheetData){

for (int i = 0; i < sheetData.size(); i++) { 
    List list = (List) sheetData.get(i); 
    for (int j = 0; j < list.size(); j++) { 
     Cell cell = (Cell) list.get(j); 
     if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) 
     System.out.print(cell.getNumericCellValue()); 
     else if (cell.getCellType() == Cell.CELL_TYPE_STRING) 
     System.out.print(cell.getRichStringCellValue()); 
     else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) 
     System.out.print(cell.getBooleanCellValue()); 
    } 
    if (j < list.size() - 1) { 
     System.out.print(", "); 
    } 
} 
    System.out.println(""); 
} 

}

程序從Excel文件讀取數據!

import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 

import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Cell; 

public class readexcel{ 

@SuppressWarnings({ "unchecked", "unchecked" }) 
public static void main(String[] args) throws Exception { 

String filename = "C:\\Users\\xxxx\\Documents\\test5.xls"; 


List sheetData = new ArrayList(); 
FileInputStream fis = null; 
try { 

fis = new FileInputStream(filename); 


HSSFWorkbook workbook = new HSSFWorkbook(fis); 

HSSFSheet sheet = workbook.getSheetAt(0); 

Iterator rows = sheet.rowIterator(); 
while (rows.hasNext()) { 
HSSFRow row = (HSSFRow) rows.next(); 
Iterator cells = row.cellIterator(); 

List data = new ArrayList(); 
while (cells.hasNext()) { 
HSSFCell cell = (HSSFCell) cells.next(); 
data.add(cell); 
} 

sheetData.add(data); 
} 
} catch (IOException e) { 
e.printStackTrace(); 
} finally { 
if (fis != null) { 
fis.close(); 
} 
} 

showExcelData(sheetData); 
} 

private static void showExcelData(List sheetData) { 

for (int i = 0; i < sheetData.size(); i++) { 
List list = (List) sheetData.get(i); 
for (int j = 0; j < list.size(); j++) { 
Cell cell = (Cell) list.get(j); 
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { 
System.out.print(cell.getNumericCellValue()); 
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { 
System.out.print(cell.getRichStringCellValue()); 
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { 
System.out.print(cell.getBooleanCellValue()); 
} 
if (j < list.size() - 1) { 
System.out.print(", "); 
} 
} 
System.out.println(""); 
} 
} 
} 
+2

請正確縮進你的代碼,否則很難讀取。 – jazzbassrob 2013-04-11 12:18:58

+0

你是否嘗試過使用索引單元格,並將單元格映射到那個單元格對象中包含什麼,有什麼喲你試圖做到這麼遠? – CBredlow 2013-04-11 12:20:02

+0

http://stackoverflow.com/q/14136721/545127 – Raedwald 2013-04-11 12:32:18

回答

0

目前尚不清楚,但我認爲你是問如何使用策略的設計模式,使您使用Map而不是if .. else if

所以,你需要

  1. 寫表示抽象操作的接口或抽象基類。
  2. 編寫類型來操作的Map從細胞類型的操作映射(
  3. 改變你showExcelData方法來使用映射找到要使用的操作,然後委託給該操作。

但是我不會爲你寫這段代碼,因爲這不是一個代碼寫入服務。

+0

我之前完成了這個工作:http://stackoverflow.com/a/14216559/545127 – Raedwald 2013-04-11 12:33:10

相關問題