栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

根据模板导出数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

根据模板导出数据

1、将模板放到服务器的目录里(随便哪个目录均可,自定义)

模板一般只定义Title

 

2、Controller

    @RequestMapping(value = "/exportBlackCat", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(httpMethod = "POST", value = "导出申报资料")
    public void exportBlackCat(@Valid @ApiParam @RequestBody ExportBlackCatIn exportBlackCatIn, HttpServletRequest request, HttpServletResponse response) {
        XSSFWorkbook workbook = null;
        OutputStream out = null;
        InputStream ips = null;
        try {
            // 从数据库中查出对应数据
            List> dataList = service.exprotBlackCat(exportBlackCatIn);
            ips = new FileInputStream(BLACKCATEXPORTPATH);
            workbook = new XSSFWorkbook(ips);
            BlcakCatUtil.xssfBlackCat(dataList, workbook);
            // 写入Excel文件到前端
            if (null != workbook) {
                String fileName = "导出申报资料文件.xlsx";
                fileName = new String(fileName.getBytes("UTF-8"), "iso8859-1");
                response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                response.setContentType("application/x-download");
                response.setCharacterEncoding("UTF-8");
                response.addHeader("Pargam", "no-cache");
                response.addHeader("Cache-Control", "no-cache");
                response.flushBuffer();
                out = response.getOutputStream();
                workbook.write(out);
                out.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != workbook) {
                    workbook.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (null != out) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

3、向模板中插入数据(数据库查询出来的数据)

    public static void xssfBlackCatData(List> list, XSSFWorkbook wb) {
        // 读取Excel文件内容
        XSSFSheet sheet = wb.getSheet("Sheet1");
        // title的行号,从0开始
        XSSFRow xSSFRow = sheet.getRow(0);
        // 插入数据的行号,一般为title下一行
        int row = 1;
        for (int i = 0; i < list.size(); i++) {
            Map tBlackCatOut = list.get(i);
            XSSFRow newRow = sheet.createRow(row);
            int columnIndex = 1;
            for (Iterator itet = xSSFRow.cellIterator(); itet.hasNext(); ) {
                XSSFCell cell = (XSSFCell) itet.next();
                XSSFCell newCell = newRow.createCell(cell.getColumnIndex());
                newRow.setHeight(xSSFRow.getHeight());
                newCell.setCellStyle(cell.getCellStyle());
                if (columnIndex == 1) {
                    newCell.setCellValue(i + 1);
                } else if (columnIndex == 2) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("waybillNum")));
                } else if (columnIndex == 3) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("tradeNum")));
                } else if (columnIndex == 4) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("ladingbillNo")));
                } else if (columnIndex == 5) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("changeNum")));
                } else if (columnIndex == 6) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("boxCode")));
                } else if (columnIndex == 7) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("receZipCode")));
                } else if (columnIndex == 8) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("receName")));
                } else if (columnIndex == 9) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("receiverTel")));
                } else if (columnIndex == 10) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("receAddOne")));
                } else if (columnIndex == 11) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("productName")));
                } else if (columnIndex == 12) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("count")));
                } else if (columnIndex == 13) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("weight")));
                } else if (columnIndex == 14) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("importTime")));
                } else if (columnIndex == 15) {
                    if (CommUtil.toString(tBlackCatOut.get("isPrint")).equals("1")) {
                        newCell.setCellValue("已打印");
                    } else {
                        newCell.setCellValue("未打印");
                    }
                } else if (columnIndex == 16) {
                    newCell.setCellValue(CommUtil.toString(tBlackCatOut.get("printTimes")));
                } else if (columnIndex == 17) {
                    newCell.setCellValue((Date) tBlackCatOut.get("printDate"));
                } else if (columnIndex == 18) {
                    newCell.setCellValue((Date) tBlackCatOut.get("ediCreatTime"));
                } else if (columnIndex == 19) {
                    if (CommUtil.toString(tBlackCatOut.get("isEdi")).equals("1")) {
                        newCell.setCellValue("已上传");
                    } else {
                        newCell.setCellValue("未上传");
                    }
                }
                columnIndex++;
            }
            row++;
        }
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/972347.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号