java导出代码

//导出
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletResponse response) throws IOException {
    try {
        //导出代码
       /* response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("studentDto列表.xlsx", "UTF-8"));
        response.setHeader("Connection", "close");
        response.setHeader("Content-Type", "application/octet-stream");
        workbook.write(response.getOutputStream());*/

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //创建表头
        String[] titles={"学生编号","身份证","姓名","年龄","性别","创建日期","班级名称"};
        //创建Excel
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建sheet页
        XSSFSheet sheet = workbook.createSheet();
        //创建row行
        XSSFRow row = sheet.createRow(0);

        //循环填充首行
        for (int i = 0; i < titles.length; i++) {
            row.createCell(i).setCellValue(titles[i]);

        }

        //循环填充核心数据
        //核心数据
        List<StudentDto> studentDtoList = studentService.findAll();
        for (int j = 0; j < studentDtoList.size(); j++) {
            XSSFRow row1 = sheet.createRow(j + 1);
            StudentDto studentDto = studentDtoList.get(j);
            row1.createCell(0).setCellValue(studentDto.getId());
            row1.createCell(1).setCellValue(studentDto.getCode());
            row1.createCell(2).setCellValue(studentDto.getName());
            row1.createCell(3).setCellValue(studentDto.getAge());
            row1.createCell(4).setCellValue(studentDto.getSex());
            row1.createCell(5).setCellValue(sdf.format(studentDto.getCreatedDate()));
            row1.createCell(6).setCellValue(studentDto.getCname());
        }


        OutputStream os;
        File file = new File("D:\\" + "xxxx" + ".xlsx");
        if (!file.exists()){
            file.createNewFile();
            System.out.println("创建文件路径为:"+file.getPath());
        }
        os=new FileOutputStream(file);
        workbook.write(os);
        os.close();

    } catch (IOException e) {
        e.printStackTrace();

    }


}
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值