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; }
解决 无用评论 打赏 举报
悬赏问题
- ¥100 关于使用MATLAB中copularnd函数的问题
- ¥20 在虚拟机的pycharm上
- ¥15 jupyterthemes 设置完毕后没有效果
- ¥15 matlab图像高斯低通滤波
- ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
- ¥15 钢筋实图交点识别,机器视觉代码
- ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
- ¥50 400g qsfp 光模块iphy方案
- ¥15 两块ADC0804用proteus仿真时,出现异常
- ¥15 关于风控系统,如何去选择