生成PDF格式的报表

public String reportPde() throws Exception{

        //获取数据

        //下载导出

        //设置头信息
        ServletActionContext.getResponse().setContentType("application/pdf");
        String filename="xxx.pdf";
        String agent=ServletActionContext.getRequest().getHeader("user-agent");
        filename=FileUtils.encodeDownloadFilename(filename,agent);
        ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename="+filename);
        //生成PDF文件
        Document document=new Document();
        PdfWriter.getInstance(document, ServletActionContext.getResponse().getOutputStream());
        document.open();
        Table table=new Table(7);
        table.setWidth(80);
        table.setBorder(1);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_TOP);
        //设置表格字体
        BaseFont cn=BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",false);
        Font font=new Font(cn,10,Font.NORMAL,Color.BLUE);
        // 写表头
                table.addCell(buildCell("xxx", font));
                table.addCell(buildCell("xxxx", font));
                table.addCell(buildCell("xxx", font));
                table.addCell(buildCell("xxx", font));
                table.addCell(buildCell("xxx", font));
                table.addCell(buildCell("xxx", font));
                table.addCell(buildCell("xxx", font));
                // 写数据
                for (WayBill wayBill : wayBills) {
                    table.addCell(buildCell(xxx, font));
                    table.addCell(buildCell(xx, font));
                    table.addCell(buildCell(xxx, font));
                    table.addCell(buildCell(xxx, font));
                    table.addCell(buildCell(xxx, font));
                    table.addCell(buildCell(xxx, font));
                }
                // 将表格加入文档
                document.add(table);

                document.close();

                return NONE;
    }
    private Cell buildCell(String content, Font font)
            throws BadElementException {
        Phrase phrase = new Phrase(content, font);
        return new Cell(phrase);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在iTextSharp上做了个二次开发,导出PDF更方便,iTextExport.dll是自己做的二次开发库,里面主要有三个类:PdfExport、ExcelExport、ReportPrint,它们分别实现导出Pdf、导出Excel和rdlc报表的直接打印和预览,基本调用方法: 1、PdfExport exp = new PdfExport(fileName, DataTable | DataGridView); exp.ExportTitle = "标题"; exp.[其它属性设置]=; exp.ExecuteExport(); 2、Excel和Pdf类似调用 3、如果要一次导出多个,可以如下: ExportFactory.Instance().ExportList.Add(exp); ExportFactory.Instance().ExportList.Add(exp); ExportFactory.Instance().ExportList.Add(exp); ....... ExportFactory.Instance().DoExport(); 4、多数据源报表打印 ReportPrint rpt = new ReportPrint("报表文件或者嵌入的报表资源名"); //是否是嵌入报表 rpt.IsEmbeddedReport = true; //是否自动调整数据源,对于打印的数据表和报表定义中的字段不同时 rpt.AutoAdjustData = false; //是否报表中含有多个数据源 rpt.IsMultiDataSource = true; rpt.ReportDataSources.Add(new ReportDataSource("T1",dt1)); rpt.ReportDataSources.Add(new ReportDataSource("T2", dt2)); rpt.StartPrint(); rpt.Dispose(); 5、单数据源报表打印 //报表参数 List _paras = new List(); _paras.Add(new ReportParameter("rptLsh", "A1")); _paras.Add(new ReportParameter("rptDjh", "A2")); ReportPrint rpt = new ReportPrint("Test.rdlc", dt); rpt.IsEmbeddedReport = false; rpt.AutoAdjustData = false; rpt.ReportParameters = _paras; rpt.StartPrint();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值