gpt4 book ai didi

java - 将新行插入到 XWPFTable 中

转载 作者:行者123 更新时间:2023-12-01 12:11:49 44 4
gpt4 key购买 nike

我尝试制作一个申请,以在其中插入新的“工作经验”以供引用;

public class TestWordPOI {

public static void main(String[] args) throws InvalidFormatException, IOException {
boolean add = false;

XWPFDocument document = new XWPFDocument(OPCPackage.open(new java.io.File("C:\\Users\\luca\\Desktop\\CV_Europass_WvWinden.docx")));

for (XWPFTable tbl : document.getTables()) {
for (XWPFTableRow row : tbl.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
for (XWPFParagraph p : cell.getParagraphs()) {
for (XWPFRun r : p.getRuns()) {
String text = r.getText(0);
System.out.println("riga? :"+text);

if ((text!=null)&&text.contains("Work experience") ) {
//when i find "work experience" i need insert the new work experience
add=true;
}
if( add )
break;
}
if( add )
break;
}
if( add )
break;
}
if( add ) {

break;}
}
if( add ) {

XWPFTableRow row =tbl.createRow(); //
row.addNewTableCell();
XWPFTableCell cell = row.createCell();
cell.setText("New work");

row.addNewTableCell().setText("Code monkey");
break;}


}

File output = new File("output.docx");
document.write(new FileOutputStream(output));

应用程序在页面底部插入新的“工作经验”,我需要将其插入到最后的经验和标题“工作经验”之间。有人知道我的错误吗?

最佳答案

for (XWPFTable tbl : document.getTables()) {
for (XWPFTableRow row : tbl.getRows()) {
y++;
for (XWPFTableCell cell : row.getTableCells()) {
.
.
.
.

if( add ) {
XWPFTableRow row =tbl.createRow();

row.addNewTableCell().setText("some thing");

tbl.addRow(row, y);

break;}

我使用1个计数器来计算行数,当我找到工作经验时,我使用计数器在corret linee处插入新行

关于java - 将新行插入到 XWPFTable 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27227828/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com