50,543
社区成员
发帖
与我相关
我的任务
分享
public void readExcel2007(DBModel dbConn, String filePath)
throws Exception {
try {
// InputStream inp = new FileInputStream(filePath);
// Workbook wb = WorkbookFactory.create(inp);
XSSFWorkbook wb = new XSSFWorkbook(filePath); //内存溢出,一直卡在这儿
int sheetSize = wb.getNumberOfSheets();
for (int i = 0; i < sheetSize; i++) {
XSSFSheet sheet = wb.getSheetAt(i);
String tableName = sheet.getSheetName(); //获取sheet页名字 对应找到数据库表名
for (Iterator rit = sheet.rowIterator(); rit.hasNext();) {
// 迭代行
XSSFRow row = (XSSFRow) rit.next();
if(row.getRowNum()>0){ //第二行开始读取
// 迭代单元格
Vector datas = new Vector();
for (Iterator cit = row.cellIterator(); cit.hasNext();) {
// 定义集合datas用于存Excel中一个行的数据
XSSFCell cell = (XSSFCell) cit.next();
String value = getValue2007(cell);
// 注意行和列是基于0索引的
// System.out.print(cell.getRowIndex() + ":" + cell.getColumnIndex() + " ");
datas.add(value);
// 开始操作单元格
}
// 向表中插入数据
DBFactory.insertData(dbConn, tableName, datas);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw e;
} catch (InvalidFormatException e) {
e.printStackTrace();
throw e;
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}