XWPFTable table1=new XWPFTable(cttbl, docx);
这里的cttbl是什么?如何设置一个表格的cttbl?
public static void getWordXAndStyle(InputStream in,String fileName,String path,String type) throws Exception {
XWPFDocument docx = (XWPFDocument) getDocument(in,type);
Iterator<IBodyElement> iBody = docx.getBodyElementsIterator();
int curT = 0;//当前操作对象的索引
int curP = 0;//当前操作对象的索引
//htmlText = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title></title></head><body>";
htmlText = "";
while(iBody.hasNext()){
IBodyElement body = iBody.next();
if(BodyElementType.TABLE.equals(body.getElementType())){//处理表格
XWPFTable table = body.getBody().getTableArray(curT);
List<XWPFTable> tables = body.getBody().getTables();
table = tables.get(curT);
if(table != null){
htmlText = htmlText+readTableX(table);
curT++;
}
}else if(BodyElementType.PARAGRAPH.equals(body.getElementType())){//处理段落
XWPFParagraph ph = body.getBody().getParagraphArray(curP);
if(ph != null){
htmlText = htmlText+readParagraphX(ph);
curP++;
}
}
}
//htmlText = htmlText + "</body></html>";
writeFile(htmlText,fileName,path,type);
}
public static String readTableX(XWPFTable tb) throws Exception {
tblExist=true;
htmlTextTbl="";
List<XWPFTableRow> rows = tb.getRows();
//遍历行
for(XWPFTableRow row:rows){
//int rowHight = row.getHeight();
String tr = "";
List<XWPFTableCell> cells = row.getTableCells();
//遍历列
for(XWPFTableCell cell:cells){
String text = "";
List<XWPFParagraph> graphs = cell.getParagraphs();
//遍历段落
for(XWPFParagraph pg:graphs){
text = text+pg.getText()+"<br/>";
}
tr += "<td>"+text+"</td>";
}
htmlTextTbl += "<tr>"+tr+"</tr>";
}
htmlTextTbl = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"tbl2\">"+htmlTextTbl+"</table><br/>";
return htmlTextTbl;
}
public static String readParagraphX(XWPFParagraph p) throws Exception {
String tempStr = "";
String text = p.getText();
if(StringUtil.isEmpty(text)){
tempStr = tempStr + "<br/>";
}else{
tempStr = tempStr+"<span>"+text+"</span><br/>";
}
return tempStr;
}
你是怎么定位到其中一个table的,我并不知道我的table在word中是第几个
操作word中多个表格的话,插件PageOffice也是可以实现的,动态创建表格 DataRegion drTable2= doc.createDataRegion("PO_table2", DataRegionInsertType.After, "PO_table1");是在"PO_table1"后面动态创建一个新的数据区域"PO_table2",用于创建新的一个5行5列的表格table2。如果要定位表格的话,可以直接根据数据区域名称进行定位的。可以了解了解的