Excel导出(poi)
生成excel:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 创建workbook对象
Workbook wb = new HSSFWorkbook();
FileOutputStream fileOut = null;
try {
// 创建sheet
Sheet sheet = wb.createSheet("sheet0");
Row row0 = sheet.createRow(0);
// 创建一行
Cell cell0 = row0.createCell(0);
cell0.setCellValue("姓名");
Cell cell1 = row0.createCell(1);
cell1.setCellValue("年龄");
Cell cell2 = row0.createCell(2);
cell2.setCellValue("手机");
Row row1 = sheet.createRow(1);
Cell cell10 = row1.createCell(0);
cell10.setCellValue("张三");
Cell cell11 = row1.createCell(1);
cell11.setCellValue((short)18);
Cell cell12 = row1.createCell(2);
cell12.setCellValue("188888888888");
fileOut = new FileOutputStream("D:\\tmp\\workbook.xls");
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
// ignore
}
解析excel:
1
2
3
4
5
6
7
Workbook wb = new HSSFWorkbook(new FileInputStream("D:\\tmp\\workbook.xls"));
Sheet sheet = wb.getSheetAt(0);
int counts = sheet.getLastRowNum();
for (int i = 1; i <= counts; i++) {
Row row = sheet.getRow(i);
System.out.println("姓名:" + row.getCell(0).getStringCellValue() + "年龄:" + row.getCell(1).getNumericCellValue() + "手机:" + row.getCell(2).getStringCellValue());
}
本文由作者按照 CC BY 4.0 进行授权