1. 程式人生 > >java POI匯出Word文件

java POI匯出Word文件

public class TestPOIWordOut {
	public static void main(String[] args) {

        //Blank Document  
        XWPFDocument document= new XWPFDocument();  
  
        //Write the Document in file system  
        FileOutputStream out;
		try {
			out = new FileOutputStream(new File("C:/Users/Administrator/Desktop/create_table.docx"));
				  		  
        //新增標題  
        XWPFParagraph titleParagraph = document.createParagraph();  
        //設定段落居中  
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);  
  
        XWPFRun titleParagraphRun = titleParagraph.createRun();  
        titleParagraphRun.setText("Java PoI");  
        titleParagraphRun.setColor("000000");  
        titleParagraphRun.setFontSize(20);  
  		  
        //段落  
        XWPFParagraph firstParagraph = document.createParagraph();  
        XWPFRun run = firstParagraph.createRun();  
        run.setText("Java POI 生成word檔案。");  
        run.setColor("696969");  
        run.setFontSize(16);  
  
        //設定段落背景顏色  
        CTShd cTShd = run.getCTR().addNewRPr().addNewShd();  
        cTShd.setVal(STShd.CLEAR);  
        cTShd.setFill("97FFFF");  
  		  
        //換行  
        XWPFParagraph paragraph1 = document.createParagraph();  
        XWPFRun paragraphRun1 = paragraph1.createRun();  
        paragraphRun1.setText("\r");  
        
        //基本資訊表格  
        XWPFTable infoTable = document.createTable();  
        //去表格邊框  
        infoTable.getCTTbl().getTblPr().unsetTblBorders();  
  		  
        //列寬自動分割  
        CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();  
        infoTableWidth.setType(STTblWidth.DXA);  
        infoTableWidth.setW(BigInteger.valueOf(9072));  
  	  
        //表格第一行  
        XWPFTableRow infoTableRowOne = infoTable.getRow(0);  
        infoTableRowOne.getCell(0).setText("職位");  
        infoTableRowOne.addNewTableCell().setText(": Java 開發工程師");  
  
        //表格第二行  
        XWPFTableRow infoTableRowTwo = infoTable.createRow();  
        infoTableRowTwo.getCell(0).setText("姓名");  
        infoTableRowTwo.getCell(1).setText(": seawater");  
  
        //表格第三行  
        XWPFTableRow infoTableRowThree = infoTable.createRow();  
        infoTableRowThree.getCell(0).setText("生日");  
        infoTableRowThree.getCell(1).setText(": xxx-xx-xx");  
  
        //表格第四行  
        XWPFTableRow infoTableRowFour = infoTable.createRow();  
        infoTableRowFour.getCell(0).setText("性別");  
        infoTableRowFour.getCell(1).setText(": 男");  
  
        //表格第五行  
        XWPFTableRow infoTableRowFive = infoTable.createRow();  
        infoTableRowFive.getCell(0).setText("現居地");  
        infoTableRowFive.getCell(1).setText(": xx");  
  		  
        //兩個表格之間加個換行  
        XWPFParagraph paragraph = document.createParagraph();  
        XWPFRun paragraphRun = paragraph.createRun();  
        paragraphRun.setText("\r");  
  		  		  
        //工作經歷表格  
        XWPFTable ComTable = document.createTable();  
  		  
        //列寬自動分割  
        CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();  
        comTableWidth.setType(STTblWidth.DXA);  
        comTableWidth.setW(BigInteger.valueOf(9072)); 
        
        //表格第一行  
        XWPFTableRow comTableRowOne = ComTable.getRow(0);  
        comTableRowOne.getCell(0).setText("開始時間");  
        comTableRowOne.addNewTableCell().setText("結束時間");  
        comTableRowOne.addNewTableCell().setText("公司名稱");  
        comTableRowOne.addNewTableCell().setText("title");  
  
        //表格第二行  
        XWPFTableRow comTableRowTwo = ComTable.createRow();  
        comTableRowTwo.getCell(0).setText("2016-09-06");  
        comTableRowTwo.getCell(1).setText("至今");  
        comTableRowTwo.getCell(2).setText("seawater");  
        comTableRowTwo.getCell(3).setText("Java開發工程師");  
  
        //表格第三行  
        XWPFTableRow comTableRowThree = ComTable.createRow();  
        comTableRowThree.getCell(0).setText("2016-09-06");  
        comTableRowThree.getCell(1).setText("至今");  
        comTableRowThree.getCell(2).setText("seawater");  
        comTableRowThree.getCell(3).setText("Java開發工程師");  
			
			document.write(out);  
			out.close();  
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
        System.out.println("create_table document written success."); 
	}
}