String filePath = ResourceUtils.getURL(“classpath:”).getPath() + “exportExcel/problemsList.xls”;
HSSFWorkbook workbook = null;
try {
workbook = new HSSFWorkbook(new FileInputStream(filePath));
// 获取准备好的样式
HSSFCellStyle cellStyle = workbook.getSheetAt(0).getRow(1).getCell(0).getCellStyle();
// workbook有内容的工作簿
HSSFSheet sheet = workbook.getSheetAt(0);
// 行数(从0开始,0是列名,模板里已经写好了,所以从第二行那个开始)
int rowIndex = 1;
// list是存放的数据
for (NationalProblems hs : list) {
// 创建行
HSSFRow row = sheet.createRow(rowIndex);
// 创建列
HSSFCell cell = row.createCell(0);
// 往第一列添加内容
// 序号
cell.setCellValue(rowIndex);
// 使用的样式
cell.setCellStyle(cellStyle);
// 审计年度
cell = row.createCell(1);
cell.setCellValue(hs.getEnd()+'年');
cell.setCellStyle(cellStyle);
// 问题标题
cell = row.createCell(2);
cell.setCellValue(hs.getTitle());
cell.setCellStyle(cellStyle);
// 问题类别
cell = row.createCell(3);
cell.setCellValue(hs.getType());
cell.setCellStyle(cellStyle);
// 问题描述
cell = row.createCell(4);
cell.setCellValue(hs.getDescribe());
cell.setCellStyle(cellStyle);
// 整改状态
cell = row.createCell(5);
LVResults lvs = getZgztResult();
for (LVResult lvResult : lvs) {
if (hs.getStatus().equals(lvResult.getValue().toString())) {
hs.setStatus(lvResult.label);
break;
}
}
cell.setCellValue(hs.getStatus());
cell.setCellStyle(cellStyle);
// 行数
rowIndex++;
}
// 相当与下载Excel
// 输出流
ServletOutputStream outputStream = response.getOutputStream();
// 一个流 两个头
response.setHeader(“Content-Disposition”, “attachment; filename=” + URLEncoder.encode(“整改问题统计列表.xls”, “UTF-8”));
workbook.write(outputStream);
outputStream.close();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (workbook != null) {
workbook.close();
}
}