如何使用POI *** 作Word文本框中的内容

如何使用POI *** 作Word文本框中的内容,第1张

步骤

  第一步,使用输入流打开文件,并获得文档的XWPFDocument对象。然后获得文档的所有段落,进而获得要 *** 作的文本所在的段落,具体使用时候,可以通过判断或者print *** 作得知要 *** 作的文本框到底是哪一段。

  FileInputStream fis = newFileInputStream("e:/filedocx");

  XWPFDocument doc = new XWPFDocument(fis);

  List<XWPFParagraph> paragraphList =docgetParagraphs();

  XWPFParagraph paragraph = paragraphListget(10);

  文本框在Word中显示

  第二步,获取XWPFParagraph的XmlObject,然后获得XmlObject对象的游标。可以通过打印XmlObject来得知当前XML的内容,也可以使用XmlCursor的getName方法和getTextValue方法来查看当前游标所在位置的Node及Node的值。

  XmlObject object =paragraphgetCTP()getRArray(1);

  XmlCursor cursor = objectnewCursor();

  第四步,通过移动游标,找到要修改的文本所在位置,然后使用游标的setTextValue来设置其值。

  //修改第一处文本:

  cursortoChild(1); cursortoChild(0);cursortoChild(3); cursortoChild(0); cursortoChild(0); cursortoChild(3);cursortoChild(1); cursorsetTextValue("First");

  // 修改第二处文本

  cursortoParent(); cursortoParent();cursortoChild(1);

  cursortoChild(3); cursortoChild(1);

  cursorsetTextValue("Second");

  第四步,保存文件、关闭输入输出流。

  FileOutputStream fos = newFileOutputStream("e:/exportdocx");

  docwrite(fos);

  fosflush();

  fosclose();

  fisclose();

  修改后的文本框。

for (int j = 0; j < trnumCells(); j++) {

cell = trgetCell(j);//取得单元格

if(j < trnumCells()-1){//获取下一个单元格,用于判断是否合并

nextCell = trgetCell(j+1);

}else{

nextCell = null;

}

int[] leftEdges = getLeftEdges(table,tr);

if(tempIndex< leftEdgeslength-1 && cellgetLeftEdge() != leftEdges[tempIndex+1] ){

htmlTextTblappend(" colspan='");

//如果不相等,继续循环,直到找着相等的为止

int colspan = 1;

for(int edgeIndex = tempIndex+1; edgeIndex < leftEdgeslength; edgeIndex++){

Systemoutprintln(leftEdges[edgeIndex] == nextCellgetLeftEdge());

if(leftEdges[edgeIndex] != nextCellgetLeftEdge() ){

colspan ++;

}else{

break;

}

}

tempIndex += colspan;

htmlTextTblappend(colspan+"'");

}

}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/12172157.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-21
下一篇2023-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存