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("");
}
}
}
請正確縮進你的代碼,否則很難讀取。 – jazzbassrob 2013-04-11 12:18:58
你是否嘗試過使用索引單元格,並將單元格映射到那個單元格對象中包含什麼,有什麼喲你試圖做到這麼遠? – CBredlow 2013-04-11 12:20:02
http://stackoverflow.com/q/14136721/545127 – Raedwald 2013-04-11 12:32:18