我使用APACHE POI從excel文件中讀取數據。我想將它們存儲在列表中(如c中的列表),因爲之後我會嘗試將它們存儲在僅調用list [0],list [1]的mysql數據庫中。我會嘗試做的是使這個列表,並在我將使用jdbc驅動程序,並給這個列表在MySQL中的表。 用於讀取Excel文件中的代碼是上述:使用java讀取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({ 「未選中」, 「選中」}) 公共靜態無效的主要(字串[] args)拋出異常{
//
// An excel file name. You can create a file name with a full
// path information.
//
String filename = "C:\\Users\\xxx\\Documents\\test.xls";
//
// Create an ArrayList to store the data read from excel sheet.
//
List sheetData = new ArrayList();
FileInputStream fis = null;
try {
//
// Create a FileInputStream that will be use to read the
// excel file.
//
fis = new FileInputStream(filename);
//
// Create an excel workbook from the file system.
//
HSSFWorkbook workbook = new HSSFWorkbook(fis);
//
// Get the first sheet on the workbook.
//
HSSFSheet sheet = workbook.getSheetAt(0);
//
// When we have a sheet object in hand we can iterator on
// each sheet's rows and on each row's cells. We store the
// data read on an ArrayList so that we can printed the
// content of the excel to the console.
//
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) {
//
// Iterates the data and print it out to the console.
//
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("");
}
}
}
我需要添加什麼才能解釋你?
這是什麼問題? – Vitaly 2013-04-09 12:44:31
你能解釋你的問題嗎?據我所見,你已經有了你的數據在列表中,那麼你的問題是什麼步驟? – redc0w 2013-04-09 12:45:13
閱讀excel文件後,我想將數據存儲在mysql中。將給出數據庫的名稱,但在這個數據庫中,每個excel文件將是一個表。我的目的是當我創建表給出了合適的命令來讀取它讀取excel文件之前讀取的值! – user2235454 2013-04-09 13:07:42