导出Excel
程序员文章站
2024-03-20 18:12:10
...
文件导出Excel格式:
依赖:
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency> | |
void exportPlan(Plan plan); | |
---|---|
@Override |
public void exportPlan(Plan plan) {
PlanExample example = new PlanExample();
PlanExample.Criteria criteria = example.createCriteria();
if(BlankUtil.IsNotBlank(plan.getSender())){
criteria.andSenderEqualTo(plan.getSender());
}
if(BlankUtil.IsNotBlank(plan.getReceiver())){
criteria.andSenderEqualTo(plan.getReceiver());
}
planMapper.selectByExample(example);
}
@RequestMapping(“exportPlan”) |
public void exportPlan(Plan plan, HttpServletResponse response) throws IOException {
//新建一个Excel
HSSFWorkbook workbook = new HSSFWorkbook();
//创建建一个sheet
Sheet sheet = workbook.createSheet();
//创建建一行
Row row = sheet.createRow(0);
Cell cell0 = row.createCell(0);
Cell cell1 = row.createCell(1);
Cell cell2 = row.createCell(2);
Cell cell3 = row.createCell(3);
Cell cell4 = row.createCell(4);
Cell cell5 = row.createCell(5);
Cell cell6 = row.createCell(6);
cell0.setCellValue("id");
cell1.setCellValue("senduname");
cell2.setCellValue("senddname");
cell3.setCellValue("sender");
cell4.setCellValue("receiveuname");
cell5.setCellValue("receivedname");
cell6.setCellValue("receiver");
//遍历列表,并填充表格
List planList = planService.getPlan(plan);
for (int i = 0; i < planList.size(); i++) {
Plan plan1 = planList.get(i);
Row row1 = sheet.createRow(i + 1);
row1.createCell(0).setCellValue(plan1.getId());
row1.createCell(1).setCellValue(plan1.getSenduname());
row1.createCell(2).setCellValue(plan1.getSenddname());
row1.createCell(3).setCellValue(plan1.getSender());
row1.createCell(4).setCellValue(plan1.getReceiveuname());
row1.createCell(5).setCellValue(plan1.getReceivedname());
row1.createCell(6).setCellValue(plan1.getReceiver());
}
ServletOutputStream out = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment;filename=工作计划.xls");
workbook.write(out);
out.flush();
out.close();
} | |
– |
上一篇: 自定义异常类和使用