gpt4 book ai didi

java - 如何使用 Apache poi 将文本添加到 word 文件中的特定位置?

转载 作者:行者123 更新时间:2023-11-30 07:55:47 28 4
gpt4 key购买 nike

我有一个在场列表,需要根据学生是否在场进行更改。我需要查找学生姓名,如果学生在场,则输入“确定”;如果学生缺席,则输入“/”。所以我想知道如何在文档文件中搜索文本以及如何将文本放在特定位置。

这是我使用的Word文件

Here is the Word file i use

我可以阅读 docx 文件,但我仍然不知道如何向表格添加一些文本。

public class WordFile {
public static void main(String[] args) {
try {

String FilePath = System.getProperty("user.home") + "\\Desktop\\Admin dokument\\EM.docx";
FileInputStream fis = new FileInputStream(FilePath);
XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(fis));
Iterator < IBodyElement > bodyElementIterator = xdoc.getBodyElementsIterator();

while (bodyElementIterator.hasNext()) {
IBodyElement element = bodyElementIterator.next();

if ("TABLE".equalsIgnoreCase(element.getElementType().name())) {
List < XWPFTable > tableList = element.getBody().getTables();
for (XWPFTable table: tableList) {
System.out.println("Total Number of Rows of Table:" + table.getNumberOfRows());
System.out.println(table.getText());
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

最佳答案

好的,我已经找到我需要的东西了。

 public class TableTest {

public TableTest() throws IOException {
String fileName = System.getProperty("user.home") + "\\Desktop\\Admin dokument\\EM.docx";
InputStream fis = new FileInputStream(fileName);
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();

for (int x=0; x<paragraphs.size();x++)
{
XWPFParagraph paragraph = paragraphs.get(x);
System.out.println(paragraph.getParagraphText());
}
List<XWPFTable> tables = document.getTables();
for (int x=0; x<tables.size();x++) {
XWPFTable table = tables.get(x);
List<XWPFTableRow> tableRows = table.getRows();
tableRows.remove(x);
for (int r=0; r<tableRows.size();r++) {
System.out.println("Row " + (r+1) + ":");
XWPFTableRow tableRow = tableRows.get(r);
List<XWPFTableCell> tableCells = tableRow.getTableCells();
for (int c=0; c<tableCells.size(); c++) {
System.out.print("Column "+ (c+1)+ ": ");
XWPFTableCell tableCell = tableCells.get(c);

String tableCellVal = tableCell.getText();

if(tableCellVal.equals("David Tomasson")) {
String s2 = "/";
tableCell = table.getRow(r).getCell(c);
System.out.print("Column "+ (c+1));
System.out.print("Row "+ (r+1));
tableCell = tableCells.get(c+2);// move one step to the right
if(!tableCellVal.isEmpty()) {
removeParagraphs(tableCell);
}
tableCell.setText(s2);
tableCell = tableCells.get(c+3);

if(!tableCellVal.isEmpty()) {
removeParagraphs(tableCell);//delete old values in the cell
}
tableCell.setText(s2);
}

if ((c+1)==4){
if (!(tableCellVal.equals("Hans Hansson"))) {
//if (tableCellVal.length()>0){
//char c1 = tableCellVal.charAt(0);
String s2 = "OK";
//char c2 = s2.charAt(0);
//String test = tableCell.getText().replace("O"," ");
if(!tableCellVal.isEmpty()) {
removeParagraphs(tableCell);
}
tableCell.setText(s2);
}
//else{
//tableCell.setText("NULL");
// }
//}
}
System.out.println("tableCell.getText(" + (c) + "):" + tableCellVal);
}
}
System.out.println("\n");
}
OutputStream out = new FileOutputStream(fileName);
document.write(out);
out.close();
}

private static void removeParagraphs(XWPFTableCell tableCell) {
int count = tableCell.getParagraphs().size();
for(int i = 0; i < count; i++){
tableCell.removeParagraph(i);
}
}
}

关于java - 如何使用 Apache poi 将文本添加到 word 文件中的特定位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32695482/

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