你好! 请教你个问题 java web程序如何将读取的excel表格里的数据插入到数据库,并显示在JSP页面上?
主要用poi.jar 包。包含两jar就可以了:poi-3.16.jar、poi-ooxml-3.16.jar
主要方法步:
/**
* filePath 文件路
* unCaseRow 要排除的行数(从上往下)
* unCaseLine 要排除的列数(往右)
*/
public List readExcel(String filePath, int unCaseRow, int unCaseLine) throws Exception {
Sheet sheet = null;
FileInputStream inStream = null;
try {
inStream = new FileInputStream(new File(filePath));
Workbook workBook = WorkbookFactory.create(inStream);
sheet = workBook.getSheetAt(0);
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
} finally {
try {
if (inStream != null) {
inStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
List list = init(sheet, unCaseRow, unCaseLine);// 需要排除行数
return list;
}
// 初始化表格中的每一行,并得到每一个单元格的值
private List init(Sheet sheet, int unCaseRow, int unCaseLine) {
int rowNum = sheet.getLastRowNum() 1; // 从零开始
List result = new ArrayList();
String[] rowArr = null;
Row row = null;
Cell cell = null;
int rowLength = 0;
int rowIndex = 0;
String rowStr = null;
for (int i =