POI 如何知道wordptrtable中的table、图属于哪个段落

poi如何读取word文档的目录结构?
[问题点数:40分,结帖人shijing266]
本版专家分:0
结帖率 100%
CSDN今日推荐
本版专家分:14
本版专家分:0
本版专家分:6016
本版专家分:0
匿名用户不能发表回复!
其他相关推荐怎么用poi 在word表格指定位置中插入行_百度知道
怎么用poi 在word表格指定位置中插入行
答题抽奖
首次认真答题后
即可获得3次抽奖机会,100%中奖。
nicelife2014
来自知道合伙人认证行家
nicelife2014
互联网类行家
采纳数:22721
获赞数:83385
从事多年网络方面工作,有丰富的互联网经验。
关键代码如下:FileInputStream fileInputStream = new FileInputStream( soureFile);POIFSFileSystem pfs = new POIFSFileSystem( fileInputStream );HWPFDocument hwpf = new HWPFDocument(pfs);// make a HWPFDocument objectOutputStream output = new FileOutputStream( targetFile );hwpf.write(output);// write to the target fileoutput.close();(2)再word中插入表格。HWPF的情况:Table tcDataTable = range.insertTableBefore( (short)column , row);//column and row列数和行数tcDataTable.getRow(i).getCell(j).getParagraph(0).getCharacterRun(0).insertBefore(&插入i行j列的内容& );XWPF的情况:String outputFile = &D:\\test.doc&;XWPFDocument document = new XWPFDocument();XWPFTable tableOne = document.createTable();XWPFTableRow tableOneRowOne = tableOne.getRow(0);tableOneRowOne.getCell(0).setText(&11&);XWPFTableCell cell12 =
tableOneRowOne.createCell();cell12.setText(&12&);// tableOneRowOne.addNewTableCell().setText(&第1行第2列&);// tableOneRowOne.addNewTableCell().setText(&第1行第3列&);// tableOneRowOne.addNewTableCell().setText(&第1行第4列&);XWPFTableRow tableOneRowTwo = tableOne.createRow();tableOneRowTwo.getCell(0).setText(&21&);tableOneRowTwo.getCell(1).setText(&22&);// tableOneRowTwo.getCell(2).setText(&第2行第3列&);XWPFTableRow tableOneRow3 = tableOne.createRow();tableOneRow3.addNewTableCell().setText(&31&);tableOneRow3.addNewTableCell().setText(&32&);FileOutputStream fOtry {fOut = new FileOutputStream(outputFile);document.write(fOut); fOut.flush();// 操作结束,关闭文件fOut.close();} catch (Exception e) {e.printStackTrace();}
为你推荐:
其他类似问题
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。使用poi读取word2007(.docx)中的复杂表格 - _路上 - 博客园
随笔 - 79, 文章 - 0, 评论 - 6, 引用 - 0
最近工作需要做一个读取word(.docx)中的表格,并以html形式输出。经过上网查询,使用了poi。
对于2007及之后的word文档,需要导入poi-ooxml-xxx.jar及其依赖包,如下图(图中为使用maven):
对于简单表格,可以使用如下方式来获取每个表格的内容:
XWPFDocument document = new XWPFDocument(new FileInputStream("word.docx"));
// 获取所有表格
List&XWPFTable& tables = document.getTables();
for (XWPFTable table : tables) {
// 获取表格的行
List&XWPFTableRow& rows = table.getRows();
for (XWPFTableRow row : rows) {
// 获取表格的每个单元格
List&XWPFTableCell& tableCells = row.getTableCells();
for (XWPFTableCell cell : tableCells) {
// 获取单元格的内容
String text = cell.getText();
但是对于复杂表格(含合并的单元格),则无法正常处理。
于是继续上网查询,在stackoverflow查到如下生成含有合并的单元格的表格:
public class CreateWordTableMerge {
static void mergeCellVertically(XWPFTable table, int col, int fromRow, int toRow) {
for(int rowIndex = fromR rowIndex &= toR rowIndex++){
CTVMerge vmerge = CTVMerge.Factory.newInstance();
if(rowIndex == fromRow){
// The first merged cell is set with RESTART merge value
vmerge.setVal(STMerge.RESTART);
// Cells which join (merge) the first one, are set with CONTINUE
vmerge.setVal(STMerge.CONTINUE);
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
// Try getting the TcPr. Not simply setting an new one every time.
CTTcPr tcPr = cell.getCTTc().getTcPr();
if (tcPr != null) {
tcPr.setVMerge(vmerge);
// only set an new TcPr if there is not one already
tcPr = CTTcPr.Factory.newInstance();
tcPr.setVMerge(vmerge);
cell.getCTTc().setTcPr(tcPr);
static void mergeCellHorizontally(XWPFTable table, int row, int fromCol, int toCol) {
for(int colIndex = fromC colIndex &= toC colIndex++){
CTHMerge hmerge = CTHMerge.Factory.newInstance();
if(colIndex == fromCol){
// The first merged cell is set with RESTART merge value
hmerge.setVal(STMerge.RESTART);
// Cells which join (merge) the first one, are set with CONTINUE
hmerge.setVal(STMerge.CONTINUE);
XWPFTableCell cell = table.getRow(row).getCell(colIndex);
// Try getting the TcPr. Not simply setting an new one every time.
CTTcPr tcPr = cell.getCTTc().getTcPr();
if (tcPr != null) {
tcPr.setHMerge(hmerge);
// only set an new TcPr if there is not one already
tcPr = CTTcPr.Factory.newInstance();
tcPr.setHMerge(hmerge);
cell.getCTTc().setTcPr(tcPr);
public static void main(String[] args) throws Exception {
XWPFDocument document= new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The table:");
//create table
XWPFTable table = document.createTable(3,5);
for (int row = 0; row & 3; row++) {
for (int col = 0; col & 5; col++) {
table.getRow(row).getCell(col).setText("row " + row + ", col " + col);
//create and set column widths for all columns in all rows
//most examples don't set the type of the CTTblWidth but this
//is necessary for working in all office versions
for (int col = 0; col & 5; col++) {
CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
tblWidth.setW(BigInteger.valueOf(1000));
tblWidth.setType(STTblWidth.DXA);
for (int row = 0; row & 3; row++) {
CTTcPr tcPr = table.getRow(row).getCell(col).getCTTc().getTcPr();
if (tcPr != null) {
tcPr.setTcW(tblWidth);
tcPr = CTTcPr.Factory.newInstance();
tcPr.setTcW(tblWidth);
table.getRow(row).getCell(col).getCTTc().setTcPr(tcPr);
//using the merge methods
mergeCellVertically(table, 0, 0, 1);
mergeCellHorizontally(table, 1, 2, 3);
mergeCellHorizontally(table, 2, 1, 4);
paragraph = document.createParagraph();
FileOutputStream out = new FileOutputStream("create_table.docx");
document.write(out);
System.out.println("create_table.docx written successully");
运行一下确实可以实现,不过仍是一头雾水,对于其中的cTTc,tcPr,vMerge等属性仍是不知道是什么。
直到后来知道了&,可以将.docx文件后缀改为.zip,即可以使用解压软件打开,进入后有一个word文件夹,里面的document.xml即为word正文内容。
对于word中的上图行合并表格,对应的xml如下:
&w:tblStyle w:val="a3"/&
&w:tblW w:w="0" w:type="auto"/&
&w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/&
&/w:tblPr&
&w:tblGrid&
&w:gridCol w:w="2765"/&
&w:gridCol w:w="2765"/&
&/w:tblGrid&
&w:tr w:rsidR="00151AA4" w:rsidTr="000249EF"&
&w:tcW w:w="2765" w:type="dxa"/&
&w:vMerge w:val="restart"/&
&w:p w:rsidR="00151AA4" w:rsidRDefault="00151AA4" w:rsidP=""&
&w:rFonts w:hint="eastAsia"/&
&w:t&0,0&/w:t&
&w:tcW w:w="2765" w:type="dxa"/&
&w:p w:rsidR="00151AA4" w:rsidRDefault="00151AA4"&
&w:rFonts w:hint="eastAsia"/&
&w:t&0,1&/w:t&
&w:tr w:rsidR="00151AA4" w:rsidTr="000249EF"&
&w:tcW w:w="2765" w:type="dxa"/&
&w:vMerge/&
&w:p w:rsidR="00151AA4" w:rsidRDefault="00151AA4"/&
&w:tcW w:w="2765" w:type="dxa"/&
&w:p w:rsidR="00151AA4" w:rsidRDefault="00151AA4"&
&w:rFonts w:hint="eastAsia"/&
&w:t&1,1&/w:t&
&w:bookmarkStart w:id="0" w:name="_GoBack"/&
&w:bookmarkEnd w:id="0"/&
看到这里,相信大家会理解了前面的tc,tcPr,vMerge等属性了吧。
其中w:tr表示的是表格的一行,tcPr代表的是一个单元格的属性。
具体可以参考:http://www.datypic.com/sc/ooxml/e-w_tbl-1.html
下面在给大家展示一下列合并的情况,大家也可以用来验证一下:
对应的xml:
&w:tblStyle w:val="a3"/&
&w:tblW w:w="0" w:type="auto"/&
&w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/&
&/w:tblPr&
&w:tblGrid&
&w:gridCol w:w="2765"/&
&w:gridCol w:w="2765"/&
&/w:tblGrid&
&w:tr w:rsidR="006C0A9A" w:rsidTr="006C099A"&
&w:tcW w:w="5530" w:type="dxa"/&
&w:gridSpan w:val="2"/&
&w:p w:rsidR="006C0A9A" w:rsidRDefault="006C0A9A"&
&w:rFonts w:hint="eastAsia"/&
&w:t&0,0&/w:t&
&w:tr w:rsidR="006C0A9A" w:rsidTr="000249EF"&
&w:tcW w:w="2765" w:type="dxa"/&
&w:p w:rsidR="006C0A9A" w:rsidRDefault="006C0A9A"&
&w:rFonts w:hint="eastAsia"/&
&w:t&1,0&/w:t&
&w:tcW w:w="2765" w:type="dxa"/&
&w:p w:rsidR="006C0A9A" w:rsidRDefault="006C0A9A"&
&w:rFonts w:hint="eastAsia"/&
&w:t&1,1&/w:t&
通过观察可以总结如下(使用poi提供的方法):
行合并情况:CTTcPr tcpr = tables.get(0).getRow(2).getCell(0).getCTTc().getTcPr(); // 此属性每个单元格都有,为每个单元格的属性:tableCell.cellProperty如果是行合并的第一行单元格,则: tcpr.getVMerge().getVal().toString() == "restart"如果是行合并的其他行单元格,则: tcpr.getVMerge().getVal() == null如果不是行合并的单元格,则: tcpr.getVMerge() == null
列合并情况:CTTcPr tcpr = tables.get(0).getRow(2).getCell(0).getCTTc().getTcPr();如果是列合并的第一列单元格,则:tcpr.getGridSpan().getVal()可以获取到这列单元格所占的行数其他单元格:tcpr.getGridSpan() == null
有一个获取表格内容转为html的demo供大家参考。用poi实现向word文件中指定位置插入表格
[问题点数:40分]
本版专家分:0
CSDN今日推荐
本版专家分:0
本版专家分:0
本版专家分:0
匿名用户不能发表回复!
其他相关推荐
完整的Java project demo,包含所需的所有jar包,示例文件,使用说明等
using System.Collections.G
using System.IO;
using System.IO.P
using System.L
using System.T
using System.X
using DocumentFormat.OpenX
using DocumentFormat
import java.io.F
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
import org.apache.poi.xwpf.usermodel.ParagraphA
import org.apa
1;apache poi插件链接http://poi.apache.org/ 这个插件主要用于office文件文本内以及富文本(表格,图片)等的提取,还有支持对已知密码的office文件的提取,
其他格式的文件加密以及解密都有 见链接http://poi.apache.org/encryption.html
因为之前做全文检索的时候需要提取word03里面的表格进行分析,使用这个插件对加密
根据word模板
在word中根据表头动态增加表格行,然后把数据中数据中写入表格中。
request.setCharacterEncoding(&UTF-8&);
String templateDocx =request.getSession().getServletContext().getRealPath(&\\&) + &res\\MyTemplate2.docx&;
FileInputStream in = new FileI
poi对word2003或者2007的图片读取和写入操作,包括根据上传的模版文件生成表单和对应的word文档,并能够对word文档中的字段进行修改。在文章中有下载代码的地址
最近用POI实现根据模板导出excel,需要从中间行插入查询到的数据但是用creatRow生成的数据会覆盖后面的模板内容查了API没有找到插入行的方法不过找到shiftRows方法将最后的空行移到需要插入行的位置,再用createRow生成sheet.shiftRows(insertRowNum, sheet.getLastRowNum(), 1,true,false); sheet.create
此程序主要展示了如何把图片插入事先设定好的书签位置之中。
下载之后可以直接运行。java POI 如何读取word的表格中的表格_百度知道
java POI 如何读取word的表格中的表格
答题抽奖
首次认真答题后
即可获得3次抽奖机会,100%中奖。
ni待会再说
ni待会再说
采纳数:84
获赞数:179
poi主要功能是操作excel
,word支持比较差
那你知道有那种第三方的插件,能够读取到word中的表格嵌套表格里的内容?很急啊...
为你推荐:
其他类似问题
您可能关注的内容
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。}

我要回帖

更多关于 wordptrtable 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信