首先導入如下jar包:
package poi.zr.com.pojo; import java.awt.Color; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.lowagie.text.BadElementException; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; public class POI { public static void main(String[] args) throws DocumentException, IOException { // 創建Document對象(頁面的大小為A4,左、右、上、下的頁邊距為10) Document document = new Document(PageSize.A4, 10, 10, 10, 10); // 建立書寫器 PdfWriter.getInstance(document, new FileOutputStream("/Users/apple/Desktop/poi.PDF")); // 設置相關的參數 POI.setParameters(document, "開發者測試", "測試", "測試 開發者 調試", "甘雨路", "甘雨路"); // 打開文檔 document.open(); // 使用iTextAsian.jar中的字體 BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font font = new Font(baseFont); List<User> users = new ArrayList<User>(); // 循環添加對象 for (int i = 0; i < 5; i++) { User user = new User(); user.setId(""+i); user.setInfo("開發者測試"+i); user.setName("測試"+i); users.add(user); } Table table = POI.setTable(users); document.add(new Paragraph("用戶信息如下:",POI.setFont())); document.add(table); // 關閉文檔 document.close(); } public static Table setTable(List<User> users) throws BadElementException{ //創建一個有3列的表格 Table table = new Table(3); table.setBorderWidth(1); table.setBorderColor(new Color(0, 0, 255)); table.setPadding(5); table.setSpacing(5); // 創建表頭 Cell cell1 = POI.setTableHeader("編號ID"); Cell cell2 = POI.setTableHeader("基本信息"); Cell cell3 = POI.setTableHeader("姓名"); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); // 添加此代碼后每頁都會顯示表頭 table.endHeaders(); for (int i = 0; i < users.size(); i++) { Cell celli1 = POI.setTableHeader(users.get(i).getId()); Cell celli2 = POI.setTableHeader(users.get(i).getInfo()); Cell celli3 = POI.setTableHeader(users.get(i).getName()); table.addCell(celli1); table.addCell(celli2); table.addCell(celli3); } return table; } /** * 設置字體編碼格式 * @return */ public static Font setFont(){ BaseFont baseFont = null; try { baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Font font = new Font(baseFont, 8, Font.NORMAL,Color.BLUE); return font; } /** * 設置cell * @param name * @return * @throws BadElementException */ public static Cell setTableHeader(String name) throws BadElementException{ Cell cell = new Cell(new Phrase(name,POI.setFont())); //單元格水平對齊方式 cell.setHorizontalAlignment(Element.ALIGN_CENTER); //單元格垂直對齊方式 cell.setVerticalAlignment(Element.ALIGN_CENTER); // cell.setHeader(true); cell.setBackgroundColor(Color.RED); return cell; } /** * 設置相關參數 * @param document * @return */ public static Document setParameters(Document document,String title,String subject,String keywords,String author, String creator){ // 設置標題 document.addTitle(title); // 設置主題 document.addSubject(subject); // 設置作者 document.addKeywords(keywords); // 設置作者 document.addAuthor(author); // 設置創建者 document.addCreator(creator); // 設置生產者 document.addProducer(); // 設置創建日期 document.addCreationDate(); return document; } }
代碼運行后產生的效果如下: