首页 > 其他分享 >利用反射导出简单pdf报表

利用反射导出简单pdf报表

时间:2022-10-09 18:36:16浏览次数:39  
标签:报表 pt 导出 cell reportTitleList new pdf document

leader给了导出pdf的需求,给了我示例代码,但我发现是硬编码的形式极大的影响了开发的效率。

然后我思考了一下,它就是将实体对象的属性按顺序赋值,那我为何不用反射获取所有属性进行遍历赋值呢。所以我为此封装了pdf导出的工具类,只需传入数据,标题就可以导出了,不需要大量的硬编码。代码如下

/**
     *
     * @param response
     * @param records 数据List
     * @param reportTitleList 标题List
     * @param  titleName    标题名称
     * @throws DocumentException
     * @throws IOException
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public static void getPdfReport(HttpServletResponse response,
                                    List<?> records, List<String> reportTitleList,String titleName) throws DocumentException, IOException, IllegalAccessException {
        //设置纸张
        Rectangle rect = new Rectangle(PageSize.A4);
        //设置底色
        rect.setBackgroundColor(BaseColor.WHITE);
        //创建文档实例
        Document document = new Document(rect);
        String fileName = new Date().getTime() + ".pdf";
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
        //创建输出流
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        //添加中文字体
        BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        //设置字体样式
        Font firsetTitleFont = new Font(bf, 12, Font.NORMAL);//标题
        Font textFont = new Font(bf, 10, Font.NORMAL);//正常
        //设置页脚
        PdfReportM1HeaderFooter footer = new PdfReportM1HeaderFooter();
        writer.setPageEvent(footer);
        //----------------------------------以上最好都写在document.open()前-----------------------------------------
        //文件开始
        document.open();
        //创建文件新页面(第1页)
        document.newPage();
        //创建标题长度列表格
        PdfPTable pt = new PdfPTable(reportTitleList.size());
        pt.setTotalWidth(550);//设置总宽度
        pt.setLockedWidth(true);
        int[] sizeList=new int[reportTitleList.size()];
        int length =reportTitleList.size();
        for (int i = 0; i < length; i++) {
            sizeList[i]=1;
        }
        pt.setWidths(sizeList);
        pt.setSpacingBefore(30);//设置表格上面空白
        pt.setSpacingAfter(30);//设置表格下面空白
        //创建cell
        PdfPCell cell = new PdfPCell();
        cell.setBorder(0);//设置无边框


        Paragraph paragraph = new Paragraph(10);// 边距
        // 1 2 3 中右左
        paragraph.setAlignment(1); // 对齐方式
        Chunk chunk = new Chunk(titleName,firsetTitleFont);
        paragraph.add(chunk);
        document.add(paragraph);

        //列标题
        for (int i = 0; i < reportTitleList.size(); i++) {
            cell = new PdfPCell();
            cell.setUseAscender(true);
            Paragraph elements = new Paragraph(reportTitleList.get(i), firsetTitleFont);
            elements.setAlignment(1);
            cell.addElement(elements);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
            pt.addCell(cell);
        }

        //pdf内容
        for (Object record : records) { //List里的对象
            Field[] declaredFields = record.getClass().getDeclaredFields();  //对象里的参数数组
            for (Field field  : declaredFields) {
                field.setAccessible(true);
                cell = new PdfPCell();
                cell.setUseAscender(true);
                Paragraph area = new Paragraph(String.valueOf(field.get(record)), textFont);
                area.setAlignment(1);
                cell.addElement(area);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
                pt.addCell(cell);
            }
        }
        document.add(pt);
        //文件关闭
        document.close();
        response.setCharacterEncoding("UTF-8");
        response.setContentLength(baos.size());
        response.setHeader("Content-type", "application/pdf;charset=utf-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(baos.toByteArray(), 0, baos.size());
        outputStream.flush();
        outputStream.close();
    }

  这个工具类在大量报表导出时,我多次能用到,节省了大量的开发时间,也增强了接口阅读性。

后面我也将同事们写的导出excel的接口重构了一下,将excel导出封装成工具类,只需要在实体类上加入@excel注解即可。切忌硬编码编写代码

标签:报表,pt,导出,cell,reportTitleList,new,pdf,document
From: https://www.cnblogs.com/gujiajie/p/16773211.html

相关文章

  • Thmeleaf.pdf下载使用
    1、如何使用springboot整合2、书籍下载链接:​​https://pan.baidu.com/s/19y_1U3kBvh0r-Qn9ZS7mDg​​提取码:q7s23、常用总结a连接带参数请求的两种方式第一种:通过拼接字符......
  • vue 前端导出PDF文件
    原理:通过对页面的截图进行生成PDF文件。PS:这里的截图指的是系统对页面的截图,不是你手动的截图,这里的截图进行可以长截图并且生成对应页数的PDF页码。  首先用npm引入......
  • Matlab simulink常见问题和数据导出到变量区
    1、simulink常见问题问题1:scope波形图没有或者看不清解决方案:你要使图像自适应,点击scope里面的这个按钮问题2:运行代码出现下面提示框解决方案:这是系统提示你的文件不在matla......
  • wireshark网络封包抓包工具导入/导出pcap文件
    1、Wireshark导入文件打开Wiresharkwiki,点击SampleCaptures,可以看到Wireshark官方上传的一些pcap文件。点击SampleCaptures后,可以看到文件后缀名有cap,pcap,pcapng,pc......
  • 导出PDF
    收到需求后,做了下调研。在网上找了一下,发现大家都是在用itext。iText是著名的开放项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以......
  • Docker全攻略 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/12tHbxcpr3GrQwj14QXUGUw点击这里获取提取码 ......
  • 流畅的Python pdf
    高清扫描版下载链接:https://pan.baidu.com/s/15KL7G8WrlhLmtcSCXu7w_A点击这里获取提取码 ......
  • 日志管理与分析权威指南 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/1WAbVHBdw39F75_eG6bBZ3w点击这里获取提取码 ......
  • 深度探索C++对象模型 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/1lcioBQey_wZamaRo8WLLhA点击这里获取提取码 ......
  • 深入JAVA虚拟机第二版 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/1XrtkbYr0Rop2hrqY4SRSUA点击这里获取提取码 ......