poi 操作word 2007 (如何删除word中的某一个表格) 求解
package re.word;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
public class TRD {
public static void main(String[] args) {
try {
OPCPackage pkg = POIXMLDocument.openPackage("c:\\wordtest.docx");
XWPFDocument doc = new XWPFDocument(pkg);
Iterator<XWPFTable> it = doc.getTablesIterator();
while(it.hasNext()){
XWPFTable table = it.next();
List<XWPFTableRow> rows = table.getRows();
for(int i=rows.size();i>=0;i--){
System.out.println("table.getRows() " +table.getRows().size());
table.removeRow(i);
}
}
FileOutputStream fos = new FileOutputStream("C:\\tes.docx");
doc.write(fos);
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上是我自己写删除word 2007 表格的源代码。 问题是表格总是有一行无法删除 (删除前表格5行 删除后表格还有一行未删除),求删除整个表格的代码,谢谢
[解决办法]
sheet.shiftRows(1,2,-1)