<!--生成EXCEL插件-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<!-- ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
- 读取
public List<Param> getSheets(String key) {
try {
Workbook workbook = WorkbookFactory.create(object.getObjectContent());
Sheet sheet = workbook.getSheetAt(0);
int idex;
for (idex = 0;idex <= sheet.getLastRowNum(); idex++){
Row row = sheet.getRow(idex);
Cell a = row.getCell(0);
Cell b = row.getCell(1);
if (a.getCellType() == Cell.CELL_TYPE_NUMERIC) {
outParam.seta(new DecimalFormat("#").format(a.getNumericCellValue()));
} else {
outParam.seta(a.getStringCellValue());
}
if (b.getCellType() == Cell.CELL_TYPE_BLANK) {
outParam.setb("0");
} else {
if (b.getCellType() == Cell.CELL_TYPE_NUMERIC) {
outParam.setb(new DecimalFormat("#").format(b.getNumericCellValue()));
} else {
outParam.setb(b.getStringCellValue());
}
}
if (idex >= 1) {
ParamList.add(outParam);
}
retrun null;
}
- 写EXCEL
/**-
生成创意xls表
*/
private XSSFWorkbook getSheets(List<Param> List) {
// 第一步,创建一个webbook,对应一个Excel文件
XSSFWorkbook wb = new XSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
XSSFSheet sheet = wb.createSheet("report");
// 第三步,在sheet中添加表头第0行
XSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);XSSFCell cell = row.createCell(0);
cell.setCellValue("老师");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("工号");
cell.setCellStyle(style);
// 第五步,写入实体数据
int rowIndex = 1;
for (Param DTO : List) {
row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(DTO.getName());
row.createCell(1).setCellValue(DTO.getId());
}
return wb;
}
-
网友评论