想用java代码读取excel(2013)表格里的内容,但是excel里面有好几个sheet,还有好几个表,该怎么办,而且还要把读取出来的内容储存到数据库中去,求大神帮助
public static List<String[]> readExcel(String filePath) {
try {
List<String[]> list = new ArrayList<String[]>();
InputStream in = new FileInputStream(filePath);
Workbook book = null;
if (filePath.endsWith(".xls")) {
book = new HSSFWorkbook(in);
} else if (filePath.endsWith(".xlsx")) {
book = new XSSFWorkbook(in);
}
// HSSFWorkbook hssfWorkbook = new HSSFWorkbook(in);
// 循环工作表Sheet
for (int i = 0; i < book.getNumberOfSheets(); i++) {
Sheet sheet = book.getSheetAt(i);
if (sheet == null) {
continue;
}
// 循环行Row
for (int j = 1; j <= sheet.getLastRowNum(); j++) {
Row row = sheet.getRow(j);
if (row == null) {
continue;
}
String[] values = new String[row.getLastCellNum()];
for (int k = 0; k < row.getLastCellNum(); k++) {
Cell xh = row.getCell(k);
if (xh == null) {
continue;
}
values[k] = getValue(xh);
}
list.add(values);
}
}
return list;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
使用POI组建读excel数据,1楼的代码可以参照下
一个sheet,一个sheet的读,,然后将数据保存到数据库就完了,,