想系統學習的同志,可以參考 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(); } }
