Java 如何使用ApachePOI将文本添加到word文件中的特定位置?
我有一个状态列表,我需要根据学生是否在场进行更改。我需要查找学生姓名,如果学生在场,我需要输入“OK”,如果学生缺席,我需要输入“/”。所以我想知道如何在doc文件中搜索文本,以及如何将文本放在特定的位置 这是我使用的Word文件 我可以阅读docx文件,但我仍然不知道如何向表中添加一些文本Java 如何使用ApachePOI将文本添加到word文件中的特定位置?,java,ms-word,apache-poi,Java,Ms Word,Apache Poi,我有一个状态列表,我需要根据学生是否在场进行更改。我需要查找学生姓名,如果学生在场,我需要输入“OK”,如果学生缺席,我需要输入“/”。所以我想知道如何在doc文件中搜索文本,以及如何将文本放在特定的位置 这是我使用的Word文件 我可以阅读docx文件,但我仍然不知道如何向表中添加一些文本 public class WordFile { public static void main(String[] args) { try { String
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();
}
}
}
公共类WordFile{
公共静态void main(字符串[]args){
试一试{
字符串FilePath=System.getProperty(“user.home”)+“\\Desktop\\Admin dokument\\EM.docx”;
FileInputStream fis=新的FileInputStream(FilePath);
XWPFDocument xdoc=新的XWPFDocument(OPCPackage.open(fis));
迭代器bodyElementIterator=xdoc.getBodyElementsIterator();
while(bodyElementIterator.hasNext()){
IBodyElement元素=bodyElementIterator.next();
if(“TABLE.equalsIgnoreCase(element.getElementType().name())){
ListtableList=element.getBody().getTables();
用于(XWPFTable表格:表格列表){
System.out.println(“表的总行数:“+Table.getNumberOfRows());
System.out.println(table.getText());
}
}
}
}捕获(例外情况除外){
例如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);
}
}
}
公共类TableTest{
public TableTest()引发IOException{
字符串文件名=System.getProperty(“user.home”)+“\\Desktop\\Admin dokument\\EM.docx”;
InputStream fis=新文件InputStream(文件名);
XWPF文件=新的XWPF文件(fis);
列表段落=document.getPages();
对于(int x=0;x