




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
XWPFDocument创建和读取OfficeWord⽂档基础篇(⼀)建议⼤家使⽤officeword来创建⽂档。(wps和word结构有些不⼀样)IBodyElement-------------------迭代器(段落和表格)XWPFComment-------------------评论(个⼈理解应该是批注)XWPFSDTXWPFFooter-------------------页脚XWPFFootnotes-------------------脚注XWPFHeader-------------------页眉XWPFHyperlink-------------------超链接XWPFNumbering-------------------编号(我也不知是啥...)XWPFParagraph-------------------段落XWPFPictureData-------------------图⽚XWPFStyles-------------------样式(设置多级标题的时候⽤)XWPFTable-------------------表格1、正⽂段落⼀个⽂档包含多个段落,⼀个段落包含多个Runs,⼀个Runs包含多个Run,Run是⽂档的最⼩单元获取所有段落:List<XWPFParagraph>paragraphs=word.getParagraphs();获取⼀个段落中的所有Runs:List<XWPFRun>xwpfRuns=xwpfParagraph.getRuns();获取⼀个Runs中的⼀个Run:XWPFRunrun=xwpfRuns.get(index);XWPFRun--代表具有相同属性的⼀段⽂本2、正⽂表格⼀个⽂档包含多个表格,⼀个表格包含多⾏,⼀⾏包含多列(格),每⼀格的内容相当于⼀个完整的⽂档获取所有表格:List<XWPFTable>xwpfTables=doc.getTables();获取⼀个表格中的所有⾏:List<XWPFTableRow>xwpfTableRows=xwpfTable.getRows();获取⼀⾏中的所有列:List<XWPFTableCell>xwpfTableCells=xwpfTableRow.getTableCells();获取⼀格⾥的内容:List<XWPFParagraph>paragraphs=xwpfTableCell.getParagraphs();之后和正⽂段落⼀样注:1.表格的⼀格相当于⼀个完整的docx⽂档,只是没有页眉和页脚。⾥⾯可以有表格,使⽤xwpfTableCell.getTables()获取,andsoon2.在poi⽂档中段落和表格是完全分开的,如果在两个段落中有⼀个表格,在poi中是没办法确定表格在段落中间的。(当然除⾮你本来知道了,这句是废话)。只有⽂档的格式固定,才能正确的得到⽂档的结构个⼈理解:我不能确定表格所处的位置(第⼀个段落后⾯,还是第⼆个段落后⾯...)3、页眉:⼀个⽂档可以有多个页眉,页眉⾥⾯可以包含段落和表格获取⽂档的页眉:List<XWPFHeader>headerList=doc.getHeaderList();获取页眉⾥的所有段落:List<XWPFParagraph>paras=header.getParagraphs();获取页眉⾥的所有表格:List<XWPFTable>tables=header.getTables();之后就⼀样了4、页脚:页脚和页眉基本类似,可以获取表⽰页数的⾓标⾔归正传-------⼲货:1、通过XWPFDocument读:段落+表格a、获取⽂档的所有段落InputStreamis=newFileInputStream("D:\\table.docx");XWPFDocumentdoc=newXWPFDocument(is);List<XWPFParagraph>paras=doc.getParagraphs();获取段落内容for(XWPFParagraphpara:paras){//当前段落的属性//CTPPrpr=para.getCTP().getPPr();System.out.println(para.getText());}b、获取⽂档中所有的表格List<XWPFTable>tables=doc.getTables();List<XWPFTableRow>rows;List<XWPFTableCell>cells;for(XWPFTabletable:tables){//表格属性CTTblPrpr=table.getCTTbl().getTblPr();//获取表格对应的⾏rows=table.getRows();for(XWPFTableRowrow:rows){//获取⾏对应的单元格cells=row.getTableCells();for(XWPFTableCellcell:cells){System.out.println(cell.getText());;}}}2、XWPFDocument⽣成word直接new⼀个空的XWPFDocument,之后再往这个XWPFDocument⾥⾯填充内容,然后再把它写⼊到对应的输出流中。新建⼀个⽂档XWPFDocumentdoc=newXWPFDocument();//创建⼀个段落XWPFParagraphpara=doc.createParagraph();//⼀个XWPFRun代表具有相同属性的⼀个区域:⼀段⽂本XWPFRunrun=para.createRun();run.setBold(true);//加粗run.setText("加粗的内容");run=para.createRun();run.setColor("FF0000");run.setText("红⾊的字。");OutputStreamos=newFileOutputStream("D:\\simpleWrite.docx");//把doc输出到输出流doc.write(os);this.close(os);新建⼀个表格//XWPFDocumentdoc=newXWPFDocument();//创建⼀个5⾏5列的表格XWPFTabletable=doc.createTable(5,5);//这⾥增加的列原本初始化创建的那5⾏在通过getTableCells()⽅法获取时获取不到,但通过row新增的就可以。//table.addNewCol();//给表格增加⼀列,变成6列table.createRow();//给表格新增⼀⾏,变成6⾏List<XWPFTableRow>rows=table.getRows();//表格属性CTTblPrtablePr=table.getCTTbl().addNewTblPr();//表格宽度CTTblWidthwidth=tablePr.addNewTblW();width.setW(BigInteger.valueOf(8000));XWPFTableRowrow;List<XWPFTableCell>cells;XWPFTableCellcell;introwSize=rows.size();intcellSize;for(inti=0;i<rowSize;i++){row=rows.get(i);//新增单元格row.addNewTableCell();//设置⾏的⾼度row.setHeight(500);//⾏属性//CTTrPrrowPr=row.getCtRow().addNewTrPr();//这种⽅式是可以获取到新增的cell的。//List<CTTc>list=row.getCtRow().getTcList();cells=row.getTableCells();cellSize=cells.size();for(intj=0;j<cellSize;j++){cell=cells.get(j);if((i+j)%2==0){//设置单元格的颜⾊cell.setColor("ff0000");//红⾊}else{cell.setColor("0000ff");//蓝⾊}//单元格属性CTTcPrcellPr=cell.getCTTc().addNewTcPr();cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);if(j==3){//设置宽度cellPr.addNewTcW().setW(BigInteger.valueOf(3000));}cell.setText(i+","+j);}}//⽂件不存在时会⾃动创建OutputStreamos=newFileOutputStream("D:\\table.docx");//写⼊⽂件doc.write(os);this.close(os);段落内容替换/***替换段落⾥⾯的变量*@parampara要替换的段落*@paramparams参数*/privatevoidreplaceInPara(XWPFParagraphpara,Map<String,Object>params){List<XWPFRun>runs;Matchermatcher;if(this.matcher(para.getParagraphText()).find()){runs=para.getRuns();for(inti=0;i<runs.size();i++){XWPFRunrun=runs.get(i);StringrunText=run.toString();matcher=this.matcher(runText);if(matcher.find()){while((matcher=this.matcher(runText)).find()){runText=matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));}//直接调⽤XWPFRun的setText()⽅法设置⽂本时,在底层会重新创建⼀个XWPFRun,把⽂本附加在当前⽂本后⾯,//所以我们不能直接设值,需要先删除当前run,然后再⾃⼰⼿动插⼊⼀个新的run。para.removeRun(i);para.insertNewRun(i).setText(runText);}}}}直接调⽤XWPFRun的setText()⽅法设置⽂本时,在底层会重新创建⼀个XWPFRun,把⽂本附加在当前⽂本后⾯,所以我们不能直接设值,需要先删除当前run,然后再⾃⼰⼿动插⼊⼀个新的run。//抽取worddocx⽂件中的图⽚Stringpath="D://abc.docx";Filefile=newFile(path);try{FileInputStreamfis=newFileInputStream(file);XWPFDocumentdocument=newXWPFDocument(fis);XWPFWordExtractorxwpfWordExtractor=newXWPFWordExtractor(document);Stringtext=xwpfWordExtractor.getText();System.out.println(text);List<XWPFPictureData>picList=document.getAllPictures();for(XWPFPictureDatapic:picList){System.out.println(pic.getPictureType()+file.separator+pic.suggestFileExtension()+file.separator+pic.getFileName());byte[]bytev=pic.getData();FileOutputStreamfos=newFileOutputStream("D:\\abc\\docxImage\\"+pic.getFileName());fos.write(bytev);}fis.close();}catch(IOExceptione){e.printStackTrace();}}多级标题结构/***⾃定义样式⽅式写word,参考statckoverflow的源码**@throwsIOException*/publicstaticvoidwriteSimpleDocxFile()throwsIOException{publicstaticvoidwriteSimpleDocxFile()throwsIOException{XWPFDocumentdocxDocument=newXWPFDocument();//⽼外⾃定义了⼀个名字,中⽂版的最好还是按照word给的标题名来,否则级别上可能会乱addCustomHeadingStyle(docxDocument,"标题1",1);addCustomHeadingStyle(docxDocument,"标题2",2);//标题1XWPFParagraphparagraph=docxDocument.createParagraph();XWPFRunrun=paragraph.createRun();run.setText("标题1");paragraph.setStyle("标题1");//标题2XWPFParagraphparagraph2=docxDocument.createParagraph();XWPFRunrun2=paragraph2.createRun();run2.setText("标题2");paragraph2.setStyle("标题2");//正⽂XWPFParagraphparagraphX=docxDocument.createParagraph();XWPFRunrunX=paragraphX.createRun();runX.setText("正⽂");//word写⼊到⽂件FileOutputStreamfos=newFileOutputStream("D:/myDoc2.docx");docxDocument.write(fos);fos.close();}/***增加⾃定义标题样式。这⾥⽤的是stackoverflow的源码**@paramdocxDocument⽬标⽂档*@paramstrStyleId样式名称*@paramheadingLevel样式级别*/privatestaticvoidaddCustomHeadingStyle(XWPFDocumentdocxDocument,StringstrStyleId,intheadingLevel){CTStylectStyle=CTStyle.Factory.newInstance();ctStyle.setStyleId(strStyleId);CTStringstyleName=CTString.Factory.newInstance();styleName.setVal(strStyleId);ctStyle.set
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度个人仓库租赁及仓储设施维护服务合同
- 2025年度文化广场场地安全租赁合同
- 2025年度临时工聘用与管理合作协议(智慧城市建设)
- 2025年高级广告礼品伞行业深度研究分析报告
- 公共建筑外墙清洗协议
- 2025年医学生培养定向就业协议书:医疗技术人才培养与服务合同
- 咖啡厅装潢资金合同专用
- 2025年度城市防汛安全保卫人工成本协议
- 住房保洁服务合同范本
- 2025年度债务清偿与债务转移合同模板
- 工厂用电安全培训课件(课件)
- 风电项目施工进度计划
- 急性呼吸窘迫综合征-课件
- DB14∕T 1319-2016 公路工程标准工程量清单及计量规范
- 2024年吉林省中考语文真题版有答案
- 如何在小学语文教学中落实单元语文要素
- 《人类起源的演化过程》阅读测试题及答案
- 2024年知识竞赛-竞彩知识笔试参考题库含答案
- 医院DRG付费知识培训课件
- 高考语文一轮复习:文学类文本阅读练习
- (2024年)保安培训图文课件
评论
0/150
提交评论