使用itextpdf+itext-asian导出中文表格代码样例:
package exportPdf;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import com.itextpdf.text.Document;import com.itextpdf.text.DocumentException;import com.itextpdf.text.Font;import com.itextpdf.text.PageSize;import com.itextpdf.text.Phrase;import com.itextpdf.text.Rectangle;import com.itextpdf.text.pdf.BaseFont;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;public class ExportPdf {public static void main(String[] args) {//示例数据List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>(){{add(new HashMap<String,Object>(){{put(\"a\",\"aaa\");put(\"b\",\"bbb\");put(\"c\",\"ccc\");put(\"d\",\"ddd\");put(\"e\",\"eee\");put(\"f\",\"fff\");}});add(new HashMap<String,Object>(){{put(\"a\",\"aaa1\");put(\"b\",\"bbb1\");put(\"c\",\"ccc1\");put(\"d\",\"ddd1\");put(\"e\",\"eee1\");put(\"f\",\"fff1\");}});add(new HashMap<String,Object>(){{put(\"a\",\"aaa2\");put(\"b\",\"bbb2\");put(\"c\",\"ccc2\");put(\"d\",\"ddd2\");put(\"e\",\"eee2\");put(\"f\",\"fff2\");}});}};//设置导出路径String File_Path = \"D://attachment/\";//生成文件名String fileName = \"Pdf\"+new Date().getTime() + \".pdf\";;// 第一步,创建document对象Rectangle rectPageSize = new Rectangle(PageSize.A4);//创建document对象Document document = new Document();try{// 第二步,将Document实例和文件输出流用PdfWriter类绑定在一起//从而完成向Document写,即写入PDF文档PdfWriter.getInstance(document,new FileOutputStream(File_Path + fileName));//第3步,打开文档document.open();// 生成表格PdfPTable table = new PdfPTable(6);//设置中文字体FontBaseFont bfChinese = BaseFont.createFont(\"STSong-Light\", \"UniGB-UCS2-H\",BaseFont.NOT_EMBEDDED);Font keyfont = new Font(bfChinese, 10, Font.BOLD);for(int i=0;i<dataList.size()+1;i++){if (i == 0){//第一行写入表格字段PdfPCell cell = new PdfPCell(new Phrase(\"a\", keyfont));table.addCell(cell);cell = new PdfPCell(new Phrase(\"b\", keyfont));table.addCell(cell);cell = new PdfPCell(new Phrase(\"c\", keyfont));table.addCell(cell);cell = new PdfPCell(new Phrase(\"d\", keyfont));table.addCell(cell);cell = new PdfPCell(new Phrase(\"e\", keyfont));table.addCell(cell);cell = new PdfPCell(new Phrase(\"f\", keyfont));table.addCell(cell);}else{Map<String,Object> dataMap = dataList.get(i-1);//字段数据写入cellPdfPCell cell = new PdfPCell(new Phrase(dataMap.get(\"a\")+\"\", keyfont));table.addCell(cell);cell = new PdfPCell(new Phrase(dataMap.get(\"b\")+\"\", keyfont));table.addCell(cell);cell = new PdfPCell(new Phrase(dataMap.get(\"c\")+\"\", keyfont));table.addCell(cell);cell = new PdfPCell(new Phrase(dataMap.get(\"d\")+\"\", keyfont));table.addCell(cell);cell = new PdfPCell(new Phrase(dataMap.get(\"e\")+\"\", keyfont));table.addCell(cell);cell = new PdfPCell(new Phrase(dataMap.get(\"f\")+\"\", keyfont));table.addCell(cell);}}//写入文档document.add(table);}catch (DocumentException de){System.err.println(de.getMessage());}catch (IOException ioe){System.err.println(ioe.getMessage());}//关闭documentdocument.close();System.out.println(\"PDF生成成功!\");}}
导出的文件样式:
另外注意用到itextpdf-5.5.6.jar和itext-asian-5.2.0.jar版本的jar包
itextpdf-5.5.6.jar
itext-asian-5.2.0.jar:
可以去阿里云的maven仓库下载