如何使用POI解决word2007中字符替换问题

    技术2022-05-19  26

    POI ==word07

    近日在工作中遇到如何使用POI导出word07的问题,在各大网站上对此问题的解决几乎为空白,通过努力,本人有幸解决了此类问题,仅供参考。

       首先就是要明白解决的方案是:先删后添。就是利用Paragraph创建run在创建完毕后循环遍历tableàrowàcell,获得到cell里的值,即cell.getText();在这里注意cellvalue的长度,建议打出来看看已在后面于替换后的cell的值做对比。同时我们应该多多关注底层文件和源码,这样可以更加方便的了解他的底层构造。可以将word的后缀名改为zip然后打开找到word/document.xml文件,查看构造尤其是表的结构(比如说多关注关注

    -<w:tc>

    - <w:tcPr>

        <w:tcW w:w="852" w:type="dxa" />

      </w:tcPr>

    - <w:p w:rsidR="00B2466A" w:rsidRDefault="004E26BC">

    - <w:r>

    - <w:rPr>

         <w:rFonts w:hint="eastAsia" />

      </w:rPr>

         <w:t>02</w:t>

      </w:r>

      </w:p>

      </w:tc>

    )这样有助于思维的扩展;

     

     

    下面是我的一个小测试,大家可以交流交流,有不同意见可以卡到我的邮箱:c736059315@12;

     

    (现在d盘建立一个word,里面要有表格的哦,切记word文档是2007版的,如果2003版可以使用range对象就能轻松解决哦,网上很多的)

     

    package test;

     

    import java.io.FileOutputStream;

    import java.util.Iterator;

    import java.util.List;

     

     

    import org.apache.poi.POIXMLDocument;

    import org.apache.poi.openxml4j.opc.OPCPackage;

    import org.apache.poi.xwpf.usermodel.XWPFDocument;

    import org.apache.poi.xwpf.usermodel.XWPFParagraph;

    import org.apache.poi.xwpf.usermodel.XWPFRun;

    import org.apache.poi.xwpf.usermodel.XWPFTable;

    import org.apache.poi.xwpf.usermodel.XWPFTableCell;

    import org.apache.poi.xwpf.usermodel.XWPFTableRow;

     

    public class Test {

        /**

         * @param args

         */

        public static void main(String[] args) {

           try {

                OPCPackage pack = POIXMLDocument.openPackage("d:/c.docx");

                XWPFDocument doc = new XWPFDocument(pack);

                Iterator<XWPFTable> it = doc.getTablesIterator();

               while(it.hasNext()){

                  XWPFTable table = it.next();

                  List<XWPFTableRow> rows=table.getRows();

                  for(XWPFTableRow row : rows){

                       List<XWPFTableCell> cells = row.getTableCells();

                        for(XWPFTableCell cell: cells){

                           /*System.out.println(cell.getText());

                           System.out.println(cell.getText().length()+"length");*/

    //                     System.out.println(cell.addParagraph().getText().length()+":--------------");//xx

                         //   获得传过来的字符串的长度

                             //这里是判断替换的条件,

                        if(cell.getText().equals("00")){

    //关键的一步,删掉原有的段落

                           cell.removeParagraph(0);

    //给段落重新赋值,不用担心要重新创建段落,他会自动创建的

                               cell.setText("爱爱");

                              /* System.out.println(cell.getText().length()+"====================");

                               */

                              

                           } 

                           //在这里,主要问题不是判断的问题。而是在判断后如何把原有的之用心之替换掉的问题,

                          /* if(cell.getText().equals("00")){

                               System.out.println();

                               cell.setText("啊啊"); 

                              // System.out.println(cell.getText().length()+"====================");

                           } */ 

                         //===========================段落可以不要==================================== 

                         List<XWPFParagraph> pars = cell.getParagraphs();

                              for(XWPFParagraph par:pars){

                              List<XWPFRun> runs = par.getRuns();

                              for(XWPFRun run:runs){

                                  run.removeBreak(); 

                              }

                               /* System.out.println(par.getText());

                                System.out.println(par.getFootnoteText()+"NO1");

                                System.out.println(par.getParagraphText()+"NO2");

                                System.out.println(par.getPictureText()+"NO3");

                                System.out.println(par.getStyle()+"NO4");

                                System.out.println(par.getStyleID()+"NO5");

                                System.out.println(par.getAlignment()+"NO6");

                                System.out.println(par.getCTP()+"NO7");

                                System.out.println(par.getDocument()+"NO8");

                                System.out.println(par.getElementType()+"NO9");*/

                               }

                        }

                  }

               }

               FileOutputStream fos = new FileOutputStream("d:/bbb.docx");

               doc.write(fos);

               fos.flush();

               fos.close();

           } catch (Exception e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

           }

     

        }

     

    }

     


    最新回复(0)