Springboot往模板excel中填写数据并导出

    @Override
    public void modelexport(HttpServletResponse response) throws IOException {
//===方法一
        String templatePath=this.getClass().getClassLoader().getResource("templates/model.xlsx").getPath();
        InputStream is = new FileInputStream(new File(templatePath));
        Workbook workbook = new XSSFWorkbook(is);
        org.apache.poi.ss.usermodel.Sheet sheet =workbook.getSheetAt(0); //获取到第一个工作表

        Row row1=sheet.createRow(1);
        row1.createCell(1).setCellValue("asdfasdfsafd");
        Row row6=sheet.createRow(6);
        row6.createCell(1).setCellValue("sdafsdafsd");
        Row row7=sheet.createRow(7);
        row7.createCell(1).setCellValue("sadfasdfsadfd");

        //清空response
        response.reset();
        //设置response的Header
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //重要点

//        String filename = new String(fileName.getBytes(), "iso8859-1") + dateFormat.format(new Date().getTime())+".xls";
        response.addHeader("Content-Disposition", "attachment;filename=example.xlsx");
        OutputStream os = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/vnd.ms-excel;charset=gb2312");
//            response.setCharacterEncoding("UTF-8");
        //将excel写入到输出流中
        workbook.write(os);
        os.flush();
        os.close();
//            log.info("设置浏览器下载成功!");




//===方法二

//        ClassPathResource resource = new ClassPathResource("templates/233.xlsx");
//        boolean isFile = resource.isFile();
//        if(!isFile){ //如果不存在返回
//             return;
//        }
//        String path = resource.getFile().getPath();
//        //获取文件路径 /* 数据写入模板文件中 */
//        // 更改文件名编码
//        String fileName = "ex.xlsx";
//        String gFileName = URLEncoder.encode(fileName, "UTF-8");
//        //如进行下载名为:文件(3).txt,下载时显示名为:文件+(3).txt --空格变为了+号
//        //解决办法如下
//        String dFileName = gFileName.replaceAll("\\+", "%20");
//        InputStream in = null; Workbook exl = null;
//        ByteArrayOutputStream out = new ByteArrayOutputStream();
//        try {
//            in = new FileInputStream(path);
//            exl = WorkbookFactory.create(in);
//            Sheet sheet1 = exl.getSheetAt(0);
//            int rowNums = sheet1.getLastRowNum();
//            //模板的行数0开始,返回值比行数小1
//                 Row row = sheet1.createRow( 1 );
//                 row.createCell(2).setCellValue("sadfasdfgsfsafsdafafsa"); //序号
//
//                 //激活浏览器弹出窗口
//            response.setContentType("application/x-msdownload");
//             //浏览器弹出窗口显示的文件名
//            response.addHeader("Content-Disposition", "attachment;filename=" + dFileName);
//            exl.write(out);
//            //in = new ByteArrayInputStream(out.toByteArray());
//            response.getOutputStream().write(out.toByteArray());
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (InvalidFormatException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (exl != null) {
//                    exl.close();
//                } if (out != null) {
//                    out.close();
//                } if (in != null) {
//                    in.close();
//                }
//            } catch (IOException ex) {
//                ex.printStackTrace();
//            }
//        }

    }

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Spring Boot,可以使用多种方式实现Excel表格的导出。下面介绍一种常见的方法,使用EasyPOI库来实现Excel表格的导出。*** <artifactId>easypoi-base</artifactId> <version>3.0.3******可以参考EasyPOI的官方文档和示例代码来了解具体的用法。 除了EasyPOI,还有其他一些第三方库也可以用于在Spring Boot实现Excel表格的导出,例如Apache POI、Jxls、Aspose.Cells等。这些库也提供了相应的API来处理Excel电子表格。 总结起来,Spring Boot实现Excel表格导出的方式有很多种,包括使用Apache POI、EasyPOI、Jxls等库,根据具体的需求和喜好选择适合的方法即可。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [《springboot实现excel表格导出》](https://blog.csdn.net/weixin_52236586/article/details/129602624)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [Springboot 指定自定义模板导出Excel文件](https://blog.csdn.net/qq_35387940/article/details/125804467)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值