小编gSr*_*gSr的帖子

在word中查找表并使用java在该表中写入

我有一个word文档,可能有n个表.该表由表名标识,该表名在第一个单元格中写为标题.现在我必须找到带有表名的表,并在该表的一个单元格中写入.我尝试使用apache-poi,但无法弄清楚如何将它用于我的目的.如果我无法解释文档的外观,请参阅随附的屏幕截图.

谢谢 如表格的截图名称所示,是S1和S2

    String fileName = "E:\\a1.doc";  

    if (args.length > 0) {  
        fileName = args[0];  
    }  

    InputStream fis = new FileInputStream(fileName);  
    POIFSFileSystem fs = new POIFSFileSystem(fis);  
    HWPFDocument doc = new HWPFDocument(fs);  

    Range range = doc.getRange(); 
    for (int i=0; i<range.numParagraphs(); i++){ 
       Paragraph tablePar = range.getParagraph(i);

        if (tablePar.isInTable()) {  
            Table table = range.getTable(tablePar);  
            for (int rowIdx=0; rowIdx<table.numRows(); rowIdx++) {  

                for (int colIdx=0; colIdx<row.numCells(); colIdx++) {  
                    TableCell cell = row.getCell(colIdx);  
                    System.out.println("column="+cell.getParagraph(0).text());  
                }  
            }  
        }  
    } 
Run Code Online (Sandbox Code Playgroud)

这是我试过的,但这只能读取第一张表.

java word-automation

6
推荐指数
1
解决办法
6841
查看次数

标签 统计

java ×1

word-automation ×1