记录一次复杂的Excel导出(一)

描述:由于导出的表格过于复杂,使用了bel格式的文件赋值,然后由html页面转Excel格式导出;后期想要实现Excel多sheet导出,直接挠头了。。。

引入easypoi

        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>3.3.0</version>
        </dependency>

1.封装数据,把复杂的数据封装成自己想要的,数据可以根据需求自己封装

        数据封装--略
        //数据导出,名称
        String sheetName = "测试表格";
        String filePath = beetlProperties.getFilePathIn()+sheetName+".html";//html文件地址
        //绑定生成模板方法 ExportHtmlUtil().generateFile(dailyReport,null,filePath,sheetName,"/html/test.html.btl");

        DateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
        String now = df.format(DateTimeKit.date());
        //html转Excel方法
        htmlToExcelToStr(response, filePath, sheetName, granaryInfo.getStoreCode() + "测试-" + now);
        

 

2.写html页面,然后用beetle赋值,数据比较麻烦,费了很大的功夫

   页面模板:

//sheetName 是必须要有的属性
<table sheetName="${tempData.grName!''}" style="width:100%;border-collapse:collapse;text-align: center;" border="1" cellspacing='0' >
        <thead>
            <tr>
                //这里合并是按照数据数据,更加灵活,从后台返回
                <td  colspan="${parseInt(colspanTotal)}" style="font: bold 22pt 'Microsoft YaHei';height:40%">${tempData.grName!''}测试表</td>
            </tr>
</thead>
 <tbody>
    <%  if(null != dayCheckList) {   %>
            <% for(temp in dayCheckList) { %>
            <tr>
                <td  rowspan="2" style="font-size: 12px;">层</td>
                <td  colspan="11" style="font-size: 12px;">无(第${temp.channelNum}行)</td>
            </tr>
            <tr>
              <% for(rootNum in temp.rootNumList) { %>
                <td style="font-size: 12px;">${rootNum.rootNum}(列)</td>
              <% } %>
            </tr>
            <% for(layerNumList in temp.layerTempList) { %>
            <% var total = 0; %>
            <tr>
                <% for(layerNum in layerNumList) {  %>
                   <% total = total +1; %>
                   <% if( strutil.startWith(layerNum.temp,"*") && parseDouble(tempData.maxTemp) == parseDouble(strutil.subString(layerNum.temp,1))) { %>
                        <td style="font-size: 12px;color:red;">${layerNum.temp}</td>
                    <% }else if( strutil.startWith(layerNum.temp,"#") && parseDouble(tempData.minTemp) == parseDouble(strutil.subString(layerNum.temp,1))) { %>
                        <td style="font-size: 12px;color:blue;">${layerNum.temp}</td>
                    <% }else{ %>
                        <td style="font-size: 12px;">${layerNum.temp}</td>
                    <% } %>
                <% } %>
                <% for(var i=0;i<12-total;i++) { %>
                    <td style="font-size: 12px;"></td>
                <% } %>
            </tr>
            <% } %>
            <% } %>
           <% } %>
            <tr>
                <td style="font-size: 12px;">仓</td>
                <td colspan="2" style="font-size: 14px;">${tempData.inTemp}</td>
                <td style="font-size: 12px;">仓</td>
                <td colspan="2" style="font-size: 12px;">${tempData.inHum}</td>
                <td style="font-size: 12px;">气</td>
                <td colspan="2" style="font-size: 12px;">${tempData.outTemp}</td>
                <td style="font-size: 12px;">气</td>
                <td colspan="2" style="font-size: 12px;">${tempData.outHum}</td>
            </tr>
            <tr>
                <td style="font-size: 12px;">数</td>
                <td colspan="3" style="font-size: 12px;">最高</td>
                <td colspan="4" style="font-size: 12px;">最低</td>
                <td colspan="4" style="font-size: 12px;">平均</td>
            </tr>
            <tr>
                <td style="font-size: 12px;">所有</td>
                <td colspan="3" style="font-size: 12px;">${tempData.maxTemp}</td>
                <td colspan="4" style="font-size: 12px;">${tempData.minTemp}</td>
                <td colspan="4" style="font-size: 12px;">${tempData.avgTemp}</td>
            </tr>
          <% if(null != layerTempList) { %>
            <% for(layer in layerTempList) { %>
            <tr>
                <td style="font-size: 12px;">${layer.layerNum}</td>
                <td colspan="3" style="font-size: 12px;">${layer.maxTemp}</td>
                <td colspan="4" style="font-size: 12px;">${layer.minTemp}</td>
                <td colspan="4" style="font-size: 12px;">${layer.avgTemp}</td>
            </tr>
            <% } %>
          <% } %>
 </tbody>
</table>

  给html赋值并生成模板:

/**
     * 赋值beetl模板并生成到指定地址
     * @param dailyReportData
     * @param fileName
     */
    public void generateFile(DailyReportVO dailyReportData,List<DailyReportVO> list,String filePath, String fileName,String templatePrefixPath) {

        Template pageTemplate = groupTemplate.getTemplate(templatePrefixPath);
        if (null != dailyReportData){
            pageTemplate.binding("tempData", dailyReportData);
            pageTemplate.binding("dayCheckList", dailyReportData.getDayCheckShowList());
            pageTemplate.binding("layerTempList", dailyReportData.getLayerTempList());
            //
            pageTemplate.binding("layerTempList", dailyReportData.getLayerTempList());
        }

        //
        if (null != list && !list.isEmpty()){
            pageTemplate.binding("outTemp",list.get(0).getOutTemp());
            pageTemplate.binding("outHum",list.get(0).getOutHum());
            pageTemplate.binding("list",list);
            //最多
            pageTemplate.binding("maxLayer",list.get(0).getStoreWay());
            //单元格总合并数和需要填充的数
            pageTemplate.binding("colspanTotal",list.get(0).getHum());
            pageTemplate.binding("colspanLayer",list.get(0).getGrainOrigin());
            pageTemplate.binding("companyName",list.get(0).getCompanyName());
        }

        if (PlatformUtil.isWindows()) {
            filePath = filePath.replaceAll("/+|\\\\+", "\\\\");
        } else {
            filePath = filePath.replaceAll("/+|\\\\+", "/");
        }
        File file = new File(filePath);
        File parentFile = file.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            pageTemplate.renderTo(fileOutputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ToolUtil.isNotEmpty(fileOutputStream)){
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

  转Excel到指定位置

/**
     * HTML生成xlsx,并读取到指定位置
     * @param response
     * @param filePath
     * @param sheetName
     * @param excelName
     * @return
     * @throws Exception
     */
private Workbook htmlToExcelToStr(HttpServletResponse response,String filePath,String sheetName,String excelName) throws Exception {
        StringBuilder html = new StringBuilder();
        InputStream is= new BufferedInputStream(new FileInputStream(filePath));
        Scanner s = new Scanner(is, "utf-8");
        while (s.hasNext()) {
            html.append(s.nextLine());
        }
        s.close();
        //easypoi方法
        Workbook workbook = ExcelXorHtmlUtil.htmlToExcel(html.toString(), ExcelType.XSSF);
        File savefile = new File(beetlProperties.getHtmlPathIn());
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(beetlProperties.getHtmlPathIn()+"\\"+sheetName+".xlsx");
        workbook.write(fos);
        fos.close();
//        workbook = ExcelXorHtmlUtil.htmlToExcel(html.toString(), ExcelType.HSSF);
//        fos = new FileOutputStream(beetlProperties.getHtmlPathIn()+"\\"+sheetName+".xls");
//        workbook.write(fos);
//        fos.close();

        return workbook;

    }

未完待续。。。

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当需要导出大量数据到Excel时,可以考虑采用分页导出的方式,每次只导出一页数据,避免一次性获取数据过多导致内存溢出的问题。以下是一种常见的分页导出Excel的方式: 1. 将需要导出的数据按照分页大小进行分页,每次查询一页数据。 2. 使用POI等Java Excel操作库,将查询到的一页数据写入Excel文件中。 3. 循环执行步骤1和步骤2,直到所有数据都导出Excel文件中。 下面是一个简单的代码示例: ``` // 设置分页大小 int pageSize = 1000; // 创建Excel文件和工作表 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 查询总记录数 int total = getDataTotal(); // 分页导出数据 for(int i = 0; i < total; i += pageSize) { // 查询一页数据 List<Data> dataList = getDataByPage(i, pageSize); // 将一页数据写入Excel文件中 for(int j = 0; j < dataList.size(); j++) { Data data = dataList.get(j); Row row = sheet.createRow(i + j); row.createCell(0).setCellValue(data.getId()); row.createCell(1).setCellValue(data.getName()); row.createCell(2).setCellValue(data.getValue()); // ... } } // 将Excel文件写入输出流 OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); ``` 在上面的代码中,首先设置了分页大小为1000条数据,然后查询总记录数,接着循环执行分页查询数据并写入Excel文件中,最后将Excel文件写入输出流返回给前端。这样可以避免一次性获取数据过多导致内存溢出的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千羽公子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值