报表技术之Excel格式报表生成(POI)

报表技术之Excel格式报表生成(POI)

1.找到你的页面的导出Excel的按钮
这里写图片描述
2.给导出按钮添加事件
这里写图片描述
3. 编写 ReportAction 添加 exportXls 方法

  • POI 生成 Excel 步骤写 Excel 过程一样,新建 Excel 文档 –新建 Sheet – 新建 Row –
    新建 Cell 单元格 – 写单元格数据
  • POI 生成 HSSF (xls)和 XSSF (xlsx)
 //导出Excel报表
    @Action(value = "report_exportXls")
    public String exportXls() throws IOException {
        //查出满足条件的数据

        List<WayBill> wayBills = wayBillService.findWayBills(model);

            //生成Excel
//        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//        HSSFSheet sheet = hssfWorkbook.createSheet("运单数据");
            //创建工作簿
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
            XSSFSheet sheet = xssfWorkbook.createSheet("运单数据");
            //创建Row
            XSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("运单号");
            row.createCell(1).setCellValue("寄件人");
            row.createCell(2).setCellValue("寄件人电话");
            row.createCell(3).setCellValue("寄件人地址");
            row.createCell(4).setCellValue("收件人");
            row.createCell(5).setCellValue("收件人电话");
            row.createCell(6).setCellValue("收件人地址");

            for (WayBill wayBill : wayBills) {
                XSSFRow xssfRow = sheet.createRow(sheet.getLastRowNum()+1);
                xssfRow.createCell(0).setCellValue(wayBill.getWayBillNum());
                xssfRow.createCell(1).setCellValue(wayBill.getSendName());
                xssfRow.createCell(2).setCellValue(wayBill.getSendMobile());
                xssfRow.createCell(3).setCellValue(wayBill.getSendAddress());
                xssfRow.createCell(4).setCellValue(wayBill.getRecName());
                xssfRow.createCell(5).setCellValue(wayBill.getRecMobile());
                xssfRow.createCell(6).setCellValue(wayBill.getRecAddress());
            }

            //下载导出
            //设置头信息
            ServletActionContext.getResponse().setContentType("application/vnd.ms-excel");
            String filename = "运单数据.xlsx";
            String agent = ServletActionContext.getRequest().getHeader("user-agent");
            filename = FileUtils.encodeDownloadFilename(filename, agent);
            //处理中文乱码
            //String filename = new String(filename.getBytes(),"ISO-8859-1");
            ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename="+filename);



            ServletOutputStream outputStream = ServletActionContext.getResponse().getOutputStream();

            xssfWorkbook.write(outputStream);

            //关闭
            xssfWorkbook.close();

        return NONE;
    }

4.编写Service代码
无条件查询所有数据不分页
这里写图片描述
有条件,查询分页数据,第一页所有数据
这里写图片描述
这里写图片描述

结果展示
导出的Excel文件展示
这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用POI和Freemarker来生成HTML格式报表。 首先,需要使用POI来读取Excel文件中的数据,并将数据存储到Java对象中。然后,使用Freemarker来生成HTML模板,并将Java对象中的数据填充到模板中。最后,将生成的HTML文件输出到指定的目录中。 以下是一个简单的示例代码: 1. 读取Excel文件并将数据存储到Java对象中 ```java public class ExcelReader { public static List<Report> readExcel(String filePath) throws IOException { List<Report> reportList = new ArrayList<>(); FileInputStream inputStream = new FileInputStream(filePath); Workbook workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { Report report = new Report(); report.setName(row.getCell(0).getStringCellValue()); report.setValue(row.getCell(1).getNumericCellValue()); reportList.add(report); } workbook.close(); inputStream.close(); return reportList; } } ``` 2. 使用Freemarker生成HTML模板 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Report</title> </head> <body> <table border="1"> <thead> <tr> <th>Name</th> <th>Value</th> </tr> </thead> <tbody> <#list reportList as report> <tr> <td>${report.name}</td> <td>${report.value}</td> </tr> </#list> </tbody> </table> </body> </html> ``` 3. 填充数据并输出HTML文件 ```java public class ReportGenerator { public static void generateReport(List<Report> reportList, String htmlFilePath) throws IOException, TemplateException { Configuration cfg = new Configuration(Configuration.VERSION_2_3_23); cfg.setClassForTemplateLoading(ReportGenerator.class, "/templates/"); cfg.setDefaultEncoding("UTF-8"); Template template = cfg.getTemplate("report.ftl"); Map<String, List<Report>> data = new HashMap<>(); data.put("reportList", reportList); File htmlFile = new File(htmlFilePath); Writer out = new FileWriter(htmlFile); template.process(data, out); out.close(); } } ``` 在以上示例代码中,`Report`类是一个简单的Java类,用来存储Excel文件中的数据,`report.ftl`是HTML模板文件,`ReportGenerator`类用来将数据填充到模板中,并输出HTML文件。 需要注意的是,需要将`report.ftl`文件放到`/templates/`目录下,以便`Freemarker`能够正确地加载模板文件。 最后,可以使用以下代码来调用以上示例代码: ```java public class Application { public static void main(String[] args) throws IOException, TemplateException { List<Report> reportList = ExcelReader.readExcel("report.xlsx"); ReportGenerator.generateReport(reportList, "report.html"); } } ``` 以上就是使用POI和Freemarker来生成HTML格式报表的示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值