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);
}
//获取数据
//下载导出
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);
}