java的XSSFWorkbook导出简单示例

2021-02-05 18:16

阅读:721

标签: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 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();
}

前端代码:

导出
.........


  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


评论


亲,登录后才可以留言!