jxl暂时不支持excel2007(xlsx格式),解析xlsx格式可以使用poi,大体思路:先解析旧的excel,将合并的单元格拆分成一个个的单元格生成新的excel,在通过解析新的excel存入数据库
/**
* 不支持xlsx
* @throws BiffException
* @throws IOException
* @throws WriteException
*/
public void createExcel() throws BiffException, IOException, WriteException {
String path = "C:/Users/hasee/Desktop/标签对照.xls"; //源文件
String newPath = "C:/Users/hasee/Desktop/副本.xls"; //目标文件
Workbook wb = Workbook.getWorkbook(new FileInputStream(new File(path)));
WritableWorkbook wwb = Workbook.createWorkbook(new File(newPath));
Sheet sheet = wb.getSheet(0);
WritableSheet sheet1 = wwb.createSheet("副本",0);
Range[] rangeCell = sheet.getMergedCells(); //获取合并单元格的数组
//将合并单元部分全部展示出来
for (int i = 1; i < sheet.getRows(); i++) {
for (int j = 0; j < sheet.getColumns(); j++) {
String str = sheet.getCell(j, i).getContents();
for (Range r : rangeCell) {
if (i > r.getTopLeft().getRow() && i <= r.getBottomRight().getRow()
&& j >= r.getTopLeft().getColumn() && j <= r.getBottomRight().getColumn()) {
str = sheet.getCell(r.getTopLeft().getColumn(),r.getTopLeft().getRow()).getContents();
}
}
Label label = new Label(j,i,str);
sheet1.addCell(label);
}
}
wwb.write();
wwb.close();
wb.close();
//将数据导入数据库
//readExcel(newPath);
}