项目数据导出成PDF文件

项目数据导出成PDF文件

模板准备:

数据格式模板如:abc.xlsx

页面处理:
绑定事件:
<el-button @click="exportPDF">导出Excel</el-button>运营数据统计
发送请求:

不能用ajax,因为接受的是文件输出流,ajax接受的是json

methods:{exportPDF(){        
window.location.href = '/report/exportBusinessReport4PDF.do';
}}
后端代码实现:
1.查询出数据
2.获取并编译模板
3.填充数据
4.使用输出流进行表格下载
    @RequestMapping("/exportBusinessReport4PDF")
    public void exportBusinessReport4PDF(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //1.获取数据
        Map<String, Object> result = reportService.getBusinessReportData();
        List<Map> hotSetmeal = (List<Map>)result.get("hotSetmeal");
        //2.获取模板文件路径
        String jrmxlPath=request.getSession().getServletContext().getRealPath("/template")+File.separator
                + "health_business3.jrxml";
        String jasperPath=request.getSession().getServletContext().getRealPath("/template")+File.separator
                + "health_business3.jasper";
        // 编译模板
        JasperCompileManager.compileReportToFile(jrmxlPath,jasperPath);
        // 3.填充数据
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath, result, new JRBeanCollectionDataSource(hotSetmeal));
        //4.设置响应信息
        ServletOutputStream out = response.getOutputStream();
        response.setContentType("application/pdf");
        response.setHeader("content-disposition","attachment;filename=report.pdf");
        // 输出文件
        JasperExportManager.exportReportToPdfStream(jasperPrint,out);
    }

业务层,持久层查询语句:

略…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用开源库如iText来将数据导出PDF。iText是一个功能强大的Java PDF库,支持创建、处理和修改PDF文档。下面是一些简要步骤: 1. 引入iText库:首先需要在项目中引入iText库的依赖。可以通过在项目的pom.xml文件中添加相应依赖来实现。 2. 创建PDF文档:使用iText的Document类,可以创建一个空白的PDF文档对象。 3. 添加内容:通过向PDF文档对象添加内容来生PDF文件,可以在文档中添加文本、图片、表格等元素。 4. 设置样式:可以使用iText提供的各种样式来设置文本字体、颜色、对齐方式等。 5. 导出PDF:使用iText提供的PdfWriter类,将文档对象写入输出流,产生一个完整的PDF文件。 下面是一个简单的例子: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileOutputStream; public class PdfExporter { public static void main(String[] args) { // 创建PDF文档对象 Document document = new Document(); try { // 绑定输出流,产生一个PDF文件 PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); // 打开文档 document.open(); // 添加内容 document.add(new Paragraph("Hello, World!")); // 关闭文档 document.close(); System.out.println("PDF文件导出功!"); } catch (DocumentException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这个例子创建了一个简单的PDF文档,包含一个"Hello, World!"的段落。执行该程序后,会在项目的根目录下生一个名为"output.pdf"的PDF文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值