码迷,mamicode.com
首页 > 编程语言 > 详细

java的XSSFWorkbook导出简单示例

时间:2020-04-27 15:30:19      阅读:411      评论:0      收藏:0      [点我收藏+]

标签:orm   ++   isp   div   mat   dal   字节   warning   work   

java代码

@GetMapping("/exportExcel")
    public void exportExcel(@RequestParam(value = "ztId") int ztId,
                            HttpServletResponse response) throws IOException {
   
    XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
    XSSFSheet sheet = xssfWorkbook.createSheet("测试");
    XSSFRow row0 = sheet.createRow(0);
    row0.createCell(0).setCellValue("编码");
    row0.createCell(1).setCellValue("名称");
    List<Ce> ces = ceRepository.findAllByZtId(ztId);
    int num=0;
    for(var it : ces){
      num++;
      XSSFRow row = sheet.createRow(num);
      row.createCell(0).setCellValue(it.getCode());
      row.createCell(1).setCellValue(it.getName());
    }
   response.setHeader("Content-Disposition", "attachment; filename=Cs.xlsx");
     // 响应类型,编码
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    // 将指定的字节写入此输出流
    xssfWorkbook.write(response.getOutputStream());
    response.flushBuffer();
    xssfWorkbook.close();
}

前端代码:

<el-button type="warning" size="small" @click="exportExcel">导出</el-button>

.........


  methods: {
    exportExcel() {
       httpDownload(`/exportExcel/?ztId=${this.ztId}`, "Ajgl.xlsx");
    }
}
.....

 

java的XSSFWorkbook导出简单示例

标签:orm   ++   isp   div   mat   dal   字节   warning   work   

原文地址:https://www.cnblogs.com/pzw23/p/12786744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!