spring boot导出excel

这篇博客记录了博主使用Spring Boot实现Excel导出过程中遇到的问题及解决方案。主要涉及Controller层的处理代码和一个名为Export的公用类,包括设置列宽、标题样式等内容,确保在谷歌浏览器下正常导出Excel文件。
摘要由CSDN通过智能技术生成

    博主第一次导出,踩了很多坑,最坑的是,本地能导出,但是一旦上传服务器就over了,没办法,只能换,参考了其他的博客,避免一些问题,共学习吧。话不多说,直接上代码。(博主用的是谷歌浏览器)

     1.首先是controller层

     @ResponseBody 

     @GetMapping("/Excel")

    R Excel(HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params) throws Exception {   //按照你项目的实际需要,根据什么条件导出,就传什么参数

         List<Waybill> waybills = waybillService.list(params);  

        Export.main(request, response, waybills);
        return “成功 ”;   

}

 

2.公用类Export

@SuppressWarnings({ "deprecation" })

public class Export {
    
    public static void main(HttpServletRequest request, HttpServletResponse response, List<Waybill> dataList) throws Exception {

        String sheetName = "运单数据";
        String titleName = "运单数据";
        String fileName = "运单数据";
        int columnNumber = 3;
        int[] columnWidth = { 10, 10, 10 };
        String[] columnName = { "序号", "运单号", "运单"};
        new Export().exportWithResponse(sheetName, titleName, fileName, columnNumber, columnWidth, columnName, dataList, response);
    }

public void exportWithResponse(String sheetName, String titleName, String fileName, int columnNumber,
            int[] columnWidth, String[] columnName, List<WaybillListDTO> dataList,HttpServletResponse response)
            throws Exception {

        if (columnNumber == columnWidth.length && columnWidth.length == columnName.length) {
            // 第一步,创建一个webbook,对应一个Excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet(sheetName);
            //统一设置列宽

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值