最近项目有一个导出报表文件的需求,我脑中闪过第一念头就是导出pdf(产品经理没有硬性规定导出excel还是pdf文件),于是赶紧上网查看相关的资料,直到踩了无数的坑把功能做出来了才知道其实导出excel的api更方便,网上的相关博客也更多,不过坑也踩完了,这一次就来把代码和收获整理和分享一下。
导出pdf模板的局限性
在看到需求之前,我先去网上去搜了一波,发现网上很大一部分博客都是将如何导出pdf模板的,就是先制作一张pdf模板,把固定不变的地方先写好,把需要改变的地方留白并设置参数,然后在代码里为参数赋值就行了,这种方式很简单,代码量也很少,但是!!!这种方式只适合导出格式和内容是固定的文件,而我们的需求是导出一张以产品名称为列,产品属性为行的报表,产品数量不定,这很显然就不能用模板的方式,只好老老实实把报表的数据从头到尾一一导出来。
使用iText导出pdf表格
iText是一种生成PDF报表的Java组件,先把jar包下下来,maven依赖如下:
com.itextpdf
itextpdf
5.0.6
按照惯例,先来生一个“Hello World”的文件,代码如下
public classTestPdf {public static void main(String[] args) throwsException {
TestPdf pdf= newTestPdf();
String filename= "D:/Program Files/pdfTest/testTable3.pdf";
pdf.createPDF(filename);
System.out.println("打印完成");
}public void createPDF(String filename) throwsIOException {
Document document= newDocument(PageSize.A4);try{
PdfWriter.getInstance(document,newFileOutputStream(filename));
document.addTitle("example of PDF");
document.open();
document.add(new Paragraph("Hello World!"));
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(DocumentException e) {
e.printStackTrace();
}finally{
document.close();
}
}
}
这个没什么可说的,照着写就行了,不过我们导出的pdf大多数是以表格的形式,所以我就直接切入主题了,这里要用到一个很关键的类com.itextpdf.text.pdf.PDFPTable,先导出一张两行两列的表格
public static PdfPTable createTable(PdfWriter writer) throwsDocumentException, IOException{
PdfPTable table= new PdfPTable(2);//生成一个两列的表格
PdfPCell cell;int size = 15;
cell= new PdfPCell(new Phrase("one"));
cell.setFixedHeight(size);//设置高度
table.addCell(cell);
cell= new PdfPCell(new Phrase("two"));
cell.setFixedHeight(size);
table.addCell(cell);
cell= new PdfPCell(new Phrase("three"));
cell.setFixedHeight(size);
table.addCell(cell);
cell= new PdfPCell(new Phrase("four"));
cell.setFixedHeight(size);
table.addCell(cell);returntable;
}public void createPDF(String filename) throwsIOException {
Document document= newDocument(PageSize.A4);try{
PdfWriter writer= PdfWriter.getInstance(document, newFileOutputStream(filename));
document.addTitle("example of PDF");
document.open();
PdfPTable table=createTable(writer);
document.add(table);
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(DocumentException e) {
e.printStackTrace();
}finally{
document.close();
}
}
效果如下图
现在把我们的需求变得更苛刻一点,再增加一行,格子数为1,文字水平和垂直居中对齐,占两行高度,先贴代码再做解释
public static PdfPTable createTable(PdfWriter writer) throwsDocumentException, IOException{
PdfPTable table= new PdfPTable(2);//生成一个两列的表格
PdfPCell cell;int size = 15;
ce