您的位置:首页 > 其它

poi 输出Excel显示内容

2015-12-04 12:12 459 查看
在业务系统中多少回接触到Excel解析。在java开发平台下选择 Apache POI是一个非常明智的选择,POI提供非常完善API来读取或写入Microsoft Office Excel。

目前对导入的数据都会进行二次加工,我们开发模式就是先把Excel中的内容直接原样导入数据库对应的一张数据表中,然后再进行二次加工。什么是原样,那就是我们在excle看到是什么样的,导入的数据就是什么样的,那怎样实现呢?

首先考虑到,exce另存为csv,然后在解析csv就可以,excel另存为csv后,如下

private ArrayList<String[]> GetExcel(String filename)
throws FileNotFoundException, IOException, Exception {
File f = new File(filename);
ArrayList data = new ArrayList();
if (f.exists()) {
InputStream fis = new FileInputStream(f);
Workbook wb = WorkbookFactory.create(fis);
Sheet fst = wb.getSheetAt(0);
int rowcount = fst.getLastRowNum();
Row headrow = fst.getRow(0);
int colcount = headrow.getLastCellNum();
for (int ri = 0; ri <= rowcount; ri++) {
System.out.println("");
System.out.println("第" + ri + "行数据");
String[] colValues = new String[colcount];
Row row = fst.getRow(ri);
for (int i = 0; i < colcount; i++) {
Cell cell = row.getCell(i);
String value = "";
if (cell != null) {
DataFormatter df = new DataFormatter();
if (cell.getCellType() == cell.CELL_TYPE_FORMULA) {
FormulaEvaluator formulaEval = wb.getCreationHelper().createFormulaEvaluator();
value = df.formatCellValue(cell, formulaEval);
} else {
value = df.formatCellValue(cell);
}
}
colValues[i] = value;
System.out.print(colValues[i] + "--");
}
data.add(colValues);
}
}
f.delete();
return data;
}


View Code
2015-12-04
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: