导出Excel需要注意的

导出Excel

最近可能犯病了,一直在做导入导出,之前讲过导入,今天说下导出excel时候碰到的坑。

导出失败的几种情况(只说我自己遇到的):
1.什么错都不报,下载页面一直在loading,然后404。
2.报了个奇葩的错,主机强迫停止一个进程,吧啦吧啦那一堆。

解决错误:
1.记得把你的前端改成这种样式,千万不要脑子里记得,然后代码忘改了。(没错了,这人是我)
使用GET 请求,参数挂在后面就可以了。

 window.open('/api/export/selectMoreInfore?BATCH_NO='+row.APPLY_CODE);

2.报的那个异常错忘了没复制出来,百度后看到的奇葩解决办法,痛不欲生,不过有个评论说可能是response重复使用需要重置,特别感谢这个大哥给了我解决办法,转换之前先处理下response,使用response.reset();

接下来把代码粘给你们 ,有需要的可以直接使用:
备注:(我的是从数据库里面查出数据,然后给excel赋值,导出)


			//查询数据,这块返回值list的类型最好用实体bean,用map的话我不太确定,没试过
            List<ExportMoreInfoBean> list = this.exportDataService.getSimCardList(BATCH_NO);
            //二、 数据转成excel
            response.reset();
            response.setContentType("application/vnd.ms-excel");

            String nowTime = DateUtil.nowTime("yyyyMMddHHmmss");
            String fileName = nowTime + "DataInfo.xlsx";
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader(
                    "Content-Disposition",
                    "attachment;filename="
                            + new String(fileName.getBytes("gb2312"),
                            "ISO-8859-1") + ".xlsx");
            // 第一步:定义一个新的工作簿
            XSSFWorkbook wb = new XSSFWorkbook();
            // 第二步:创建一个Sheet页
            XSSFSheet sheet = wb.createSheet("startTimeendTime");
            sheet.setDefaultRowHeight((short) (2 * 256));//设置行高
            sheet.setColumnWidth(0, 5500);//设置列宽
            sheet.setColumnWidth(1, 4500);//设置列宽
            sheet.setColumnWidth(2, 4500);//设置列宽
            sheet.setColumnWidth(3, 4500);//设置列宽
            sheet.setColumnWidth(4, 4500);//设置列宽
            sheet.setColumnWidth(5, 4500);//设置列宽
            sheet.setColumnWidth(6, 4500);//设置列宽
            sheet.setColumnWidth(7, 4500);//设置列宽
            sheet.setColumnWidth(8, 4500);//设置列宽
            sheet.setColumnWidth(9, 4500);//设置列宽
            sheet.setColumnWidth(10, 5700);//设置列宽

            XSSFFont font = wb.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 16);

            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("列名称1");
            cell = row.createCell(1);
            cell.setCellValue("列名称2");
            cell = row.createCell(2);
            cell.setCellValue("列名称3");
            cell = row.createCell(3);
            cell.setCellValue("列名称4");
            cell = row.createCell(4);
            cell.setCellValue("列名称5");
            cell = row.createCell(5);
            cell.setCellValue("列名称6");
            cell = row.createCell(6);
            cell.setCellValue("列名称7");
            cell = row.createCell(7);
            cell.setCellValue("列名称8");
            cell = row.createCell(8);
            cell.setCellValue("列名称9");
            cell = row.createCell(9);
            cell.setCellValue("列名称10");
            cell = row.createCell(10);

            XSSFRow rows;
            XSSFCell cells;
            for (int i = 0; i < list.size(); i++) {
                // 第三步:在这个sheet页里创建一行
                rows = sheet.createRow(i + 1);
                // 第四步:在该行创建一个单元格
                cells = rows.createCell(0);
                // 第五步:在该单元格里设置值
                cells.setCellValue(list.get(i).getSIM_CARD_NO());
                cells = rows.createCell(1);
                cells.setCellValue(list.get(i).getKIND_NAME());
                cells = rows.createCell(2);
                cells.setCellValue(list.get(i).getSUPPLIER_NAME());
                cells = rows.createCell(3);
                cells.setCellValue(list.get(i).getPROVINCE_NAME());
                cells = rows.createCell(4);
                cells.setCellValue(list.get(i).getEPARCHY_NAME());
                cells = rows.createCell(5);
                cells.setCellValue(list.get(i).getREGION_NAME());
                cells = rows.createCell(6);
                cells.setCellValue(list.get(i).getSYSTEM_NAME());
                cells = rows.createCell(7);
                cells.setCellValue(list.get(i).getBUREAU_NAME());
                cells = rows.createCell(8);
                cells.setCellValue(list.get(i).getCREATE_PERSON_NAME());
                cells = rows.createCell(9);
                cells.setCellValue(list.get(i).getCREATE_DATE_STR());
                cells = rows.createCell(10);

            }
            try {
                OutputStream out = response.getOutputStream();
                wb.write(out);
                out.close();
                wb.close();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值