工作报表Java到出PDF

public String exprotData(){

// 工作单数据
List<Region> list =   facedService.getRegionService().pageQueryByRedis(getSpecification());
// pdf 下载...
// itext 报表 下载
try {
Document document = new Document();
// response
HttpServletResponse response = ServletActionContext.getResponse();
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
writer.setEncryption("itcast".getBytes(), "tangjiejin".getBytes(), PdfWriter.ALLOW_SCREENREADERS, PdfWriter.STANDARD_ENCRYPTION_128);
// 浏览器下载 ...两个头
String filename = new Date(System.currentTimeMillis()).toLocaleString() + "_区域报表.pdf";
response.setContentType(ServletActionContext.getServletContext().getMimeType(filename));// mime 类型
response.setHeader("Content-Disposition", "attachment;filename=" + DownLoadUtils.getAttachmentFileName(filename, ServletActionContext.getRequest().getHeader("user-agent")));
// 打开文档
document.open();
Table table = new Table(5, list.size() + 1);// 5列 行号 0 开始
table.setBorderWidth(1f);
table.setAlignment(1);// // 其中1为居中对齐,2为右对齐,3为左对齐
// table.setBorder(1); // 边框
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); // 水平对齐方式
table.getDefaultCell().setVerticalAlignment(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("工作单编号", font));
table.addCell(buildCell("省", font));
table.addCell(buildCell("市", font));
table.addCell(buildCell("区", font));
table.addCell(buildCell("邮编", font));


// 表格数据
for (Region r : list) {
// table.addCell(buildCell(workOrderManage.getId(), font));
table.addCell(buildCell(r.getProvince(), font));
table.addCell(buildCell(r.getCity(), font));
table.addCell(buildCell(r.getDistrict(), font));
table.addCell(buildCell(r.getPostcode(), font));
}


// 向文档添加表格
document.add(table);
document.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return NONE;
}

private Cell buildCell(String content, Font font) throws BadElementException {
Phrase phrase = new Phrase(content, font);
Cell cell = new Cell(phrase);
// 设置垂直居中
cell.setVerticalAlignment(1);
// 设置水平居中
cell.setHorizontalAlignment(1);
return cell;
}
Java使用com.lowagie.text.pdf插件编写的PDF报表工具类,支持动态报表创建,使用简单,附件中包含了测试类和生成的报表文件。附件中的代码需要修改相关的保存路径后可以直接使用。创建一张报表例子: private JsFileExportResult createRowReport() { String condition = "开始时间:2018-02-02 14:00:30 结束时间:2018-02-06 16:00:30"; PDFGridReport pdfReport = new PDFGridReport("报表创建测试", GridReportTestModel.getModels()); pdfReport.addCondition(condition); pdfReport.header("字段名称1", "item1", 0, 0).width(160); pdfReport.header("字段名称3", "item3", 0, 2).getCell().backgroundColor(Color.ORANGE); pdfReport.header("字段名称4", "item4", 0, 3); pdfReport.header("字段名称5", "item5", 0, 4); pdfReport.header("字段名称2", "item2", 0, 1); pdfReport.header("值", "value", 0, 5).alignH(HAlign.ALIGN_CENTER).getCell().alignH(HAlign.ALIGN_RIGHT); pdfReport.header("时间", "time", 0, 6); pdfReport.header("图片", "image", 0, 7).width(60).alignH(HAlign.ALIGN_CENTER).getCell() .alignH(HAlign.ALIGN_CENTER).alignV(VAlign.ALIGN_MIDDLE); // 横向打印 pdfReport.getPageSetting().setPrintHorizontal(); pdfReport.group("item1").childGroup("item2"); pdfReport.setCellFormat(new PDFCellValueFormat() { @Override public String format(String fieldName, String oriValue) { if ("value".equals(fieldName)) { return String.format("%.2f", Double.parseDouble(oriValue)).toString(); } else { return oriValue; } } }); JsFileExportResult fileResult = pdfReport.createReport(); return fileResult; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值