XWPFTable table1=new XWPFTable(cttbl, docx);
这里的cttbl是什么?如何设置一个表格的cttbl?
使用poi操作word时如何在有多个表格的word中定位到其中一个表格。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答
- BeansG 2014-12-05 03:51关注
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; }
解决 无用评论 打赏 举报
悬赏问题
- ¥35 平滑拟合曲线该如何生成
- ¥100 c语言,请帮蒟蒻写一个题的范例作参考
- ¥15 名为“Product”的列已属于此 DataTable
- ¥15 安卓adb backup备份应用数据失败
- ¥15 eclipse运行项目时遇到的问题
- ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
- ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
- ¥15 自己瞎改改,结果现在又运行不了了
- ¥15 链式存储应该如何解决
- ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站