1. 程式人生 > 其它 >POI XWPFDocument 實現word匯出功能

POI XWPFDocument 實現word匯出功能

想系統學習的同志,可以參考 POI官方

1、引用依賴

  <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency> <!-- 操作File好用 可選 -->
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4
</version> </dependency> </dependencies>

2、編寫程式碼

import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.List;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;

public class ExportDocTest { public static void main(String[] args) throws Exception { XWPFDocument doc = new XWPFDocument(); XWPFParagraph title = doc.createParagraph();//設定活動標題 title.setAlignment(ParagraphAlignment.CENTER); XWPFRun r1 = title.createRun(); r1.setBold(
true); r1.setFontFamily("宋體"); r1.setText("20元優惠劵活動");//活動名稱 r1.setFontSize(22); XWPFParagraph actTheme = doc.createParagraph();//設定活動主題 actTheme.setAlignment(ParagraphAlignment.LEFT); XWPFRun runText1=actTheme.createRun(); runText1.setText("1、活動主題:20劵優惠劵活動"); runText1.setFontSize(13); XWPFParagraph actType = doc.createParagraph();//設定活動型別 XWPFRun runText2=actType.createRun(); runText2.setText("2、活動型別:系統發劵型別"); runText2.setFontSize(13); XWPFParagraph actDate = doc.createParagraph();//設定活動日期 XWPFRun actDaterun=actDate.createRun(); actDaterun.setText("3、活動日期:2015-06-08至2015-06-10"); actDaterun.setFontSize(13); XWPFParagraph actText = doc.createParagraph();//設定活動內容 XWPFRun runText3=actText.createRun(); runText3.setFontSize(13); runText3.setText("4、活動內容:哈哈哈士大夫士大夫立刻絕對是方路即可大水井坊路可絕對是弗蘭克家第三方立刻幾點睡了罰款絕對是路客服絕對是路客服絕對是路客服幾點睡了罰款家第三方立刻幾點睡了罰款記錄可定時 "); XWPFParagraph actRemark = doc.createParagraph();//設定活動備註 XWPFRun runText4=actRemark.createRun(); runText4.setText("5、活動備註: "); runText4.setFontSize(13); runText4.setBold(true); XWPFRun runText5=actRemark.createRun(); runText5.setText("我是活動備註哦........................ "); runText5.setFontSize(12); XWPFParagraph actRule = doc.createParagraph();//設定活動備註 XWPFRun rule=actRule.createRun(); rule.setText("6、活動規則: "); rule.setFontSize(13); rule.setBold(true); XWPFTable table=actRule.getDocument().createTable(2,2);//建立表格 table.setWidth(500); table.setCellMargins(20, 20, 20, 20); System.out.println(table.getWidth()); //表格屬性 CTTblPr tablePr = table.getCTTbl().addNewTblPr(); //表格寬度 CTTblWidth width = tablePr.addNewTblW(); width.setW(BigInteger.valueOf(8000)); List<XWPFTableCell> tableCells = table.getRow(0).getTableCells(); tableCells.get(0).setText("第一行第一列的資料:規則型別名稱"); tableCells.get(1).setText("第一行第二列的資料:規則描述"); List<XWPFTableCell> tableCellsq = table.getRow(1).getTableCells(); tableCellsq.get(0).setText("第二行第一列的資料:A發劵規則"); tableCellsq.get(1).setText("第二行第二列的資料:A發劵規則針對5星級使用者"); XWPFParagraph text9 = doc.createParagraph(); XWPFRun runText9=text9.createRun(); runText9.addBreak(BreakType.TEXT_WRAPPING);//換行 runText9.setBold(true); runText9.setText("7、請現在你喜歡運動?"); runText9.setFontSize(13); XWPFParagraph text10 = doc.createParagraph(); XWPFRun runText10=text10.createRun(); runText10.setFontSize(12); runText10.setText("□a.排球 "); runText10.setText("☑b.羽毛球"); XWPFParagraph text3 = doc.createParagraph(); XWPFRun runText7=text3.createRun(); runText7.setText("負責人:zhangsan"); runText7.setFontSize(13); XWPFParagraph text8 = doc.createParagraph(); XWPFRun runText8=text8.createRun(); runText8.setText("負責人電話:12345678921"); runText8.setFontSize(13); FileOutputStream out = new FileOutputStream("E:\\test.docx"); doc.write(out); System.out.println(1); out.close(); } }