使用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);
//设置中文字体Font
BaseFont 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);
//字段数据写入cell
PdfPCell 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());
}
//关闭document
document.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仓库下载