java解析word(POI)

mirzlm 2009-04-09 05:20:27
使用POI解析WORD,能解析到WORD中的表格信息吗
网上大多都是只解析出所有文本,却没取得表格信息

在这里懦懦的问一下前辈们,能不能现实这个需求,还希望给个相关例子,谢谢
...全文
2122 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Y352612845 2012-02-13
  • 打赏
  • 举报
回复
怎样生成一个空白的word文档啊?
mirzlm 2009-04-10
  • 打赏
  • 举报
回复

public void testWord(String fileString,String newPath) {
File file = new File(fileString);
try {
FileInputStream in = new FileInputStream(file);
POIFSFileSystem pfs = new POIFSFileSystem(in);
HWPFDocument hwpf = new HWPFDocument(pfs);
Range range = hwpf.getRange();
StyleSheet styleSheet = hwpf.getStyleSheet();
TableIterator it = new TableIterator(range);
// 遍历一个DOC中的所有表格
while (it.hasNext()) {
Table tb = (Table) it.next();

// 遍历表格的行
for (int i = 0; i < tb.numRows(); i++) {
TableRow tr = tb.getRow(i);
// 遍历表格的列
for (int j = 0; j < tr.numCells(); j++) {
// 往表格中插入数据
TableCell td = tr.getCell(j);
String text = "第" + i + "行第" + j + "列";
int p = td.numParagraphs();
Paragraph para = td.getParagraph(p);
ParagraphProperties pp = new ParagraphProperties();
System.out.println(para.);
//if(para.){
para.insertBefore(text);
//}
}
}
}
// 在表格外面插入内容
CharacterProperties cp = new CharacterProperties();
cp.setBold(true);
cp.setCharacterSpacing(10);
cp.setChse(cp.SPRM_CHARSCALE);
cp.setCapitalized(true);
//int p = range.numParagraphs();
//Paragraph para = range.getParagraph(p);
//para.insertAfter("test poi successful!", cp);
//para.insertAfter("测试成功", cp);
File outputFile = new File(newPath);
OutputStream output = new FileOutputStream(outputFile);
hwpf.write(output);
output.close();

} catch (Exception ex) {
ex.printStackTrace();
}


这种可以,不过有点错误,要解决一下
mirzlm 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 susoft2008 的回复:]
希望这个可以帮你
public String[] getAllText() {
int numP = range.numParagraphs(); //得到range范围的Paragraph的个数
int m = 0; //数组下标
String[] ret = new String[numP];
for (int i = 0; i < numP; ++i) {
// 从每一段落中获取文字,每一段是一个回车
Paragraph p = range.getParagraph(i);
test = p.isInTable(); //判断该Paragraph是否在word的表格中
if (test == true) {
table = range.getTa…
[/Quote]
这个方法取到的,只是里面的表格里面的内容,也不是我想要的
mirzlm 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 susoft2008 的回复:]
希望这个可以帮你
public String[] getAllText() {
int numP = range.numParagraphs(); //得到range范围的Paragraph的个数
int m = 0; //数组下标
String[] ret = new String[numP];
for (int i = 0; i < numP; ++i) {
// 从每一段落中获取文字,每一段是一个回车
Paragraph p = range.getParagraph(i);
test = p.isInTable(); //判断该Paragraph是否在word的表格中
if (test == true) {
table = range.getTa…
[/Quote]
感谢,我去试试
APOLLO_TS 2009-04-10
  • 打赏
  • 举报
回复
官网上应该有
susoft2008 2009-04-09
  • 打赏
  • 举报
回复
希望这个可以帮你
public String[] getAllText() {
int numP = range.numParagraphs(); //得到range范围的Paragraph的个数
int m = 0; //数组下标
String[] ret = new String[numP];
for (int i = 0; i < numP; ++i) {
// 从每一段落中获取文字,每一段是一个回车
Paragraph p = range.getParagraph(i);
test = p.isInTable(); //判断该Paragraph是否在word的表格中
if (test == true) {
table = range.getTable(p); //通过第一个在table中的Paragraph来获取整个table
int numRow = table.numRows(); //获取table中的行数

for (int j = 0; j < numRow; j++) {
tablerow = table.getRow(j); //获得一行
int numbercell = tablerow.numCells(); //通过tablerow获取单元格个数

for (int k = 0; k < numbercell; k++) {

tablexell = tablerow.getCell(k); //获得单元格

int p1 = tablexell.numParagraphs();
//获取单元格中的Paragraph的个数
String str = "";
for (int l = 0; l < p1; l++) {

Paragraph para = tablexell.getParagraph(l);
str = str + para.text().trim(); //得到单元格中的内容

}
for (int n = m; n < numP;) {
ret[n] = str; //将单元格中的内容方入数组元素中
break;
}
m++;
}
}
return ret;
}
}

return ret;
}
susoft2008 2009-04-09
  • 打赏
  • 举报
回复
希望这个可以帮你
public String[] getAllText() {
int numP = range.numParagraphs(); //得到range范围的Paragraph的个数
int m = 0; //数组下标
String[] ret = new String[numP];
for (int i = 0; i < numP; ++i) {
// 从每一段落中获取文字,每一段是一个回车
Paragraph p = range.getParagraph(i);
test = p.isInTable(); //判断该Paragraph是否在word的表格中
if (test == true) {
table = range.getTable(p); //通过第一个在table中的Paragraph来获取整个table
int numRow = table.numRows(); //获取table中的行数

for (int j = 0; j < numRow; j++) {
tablerow = table.getRow(j); //获得一行
int numbercell = tablerow.numCells(); //通过tablerow获取单元格个数

for (int k = 0; k < numbercell; k++) {

tablexell = tablerow.getCell(k); //获得单元格

int p1 = tablexell.numParagraphs();
//获取单元格中的Paragraph的个数
String str = "";
for (int l = 0; l < p1; l++) {

Paragraph para = tablexell.getParagraph(l);
str = str + para.text().trim(); //得到单元格中的内容

}
for (int n = m; n < numP;) {
ret[n] = str; //将单元格中的内容方入数组元素中
break;
}
m++;
}
}
return ret;
}
}

return ret;
}
ZHANGBINFLY 2009-04-09
  • 打赏
  • 举报
回复
关注
「已注销」 2009-04-09
  • 打赏
  • 举报
回复
关注
mirzlm 2009-04-09
  • 打赏
  • 举报
回复
唉,不知道怎么办,心里懦懦的
kingssman 2009-04-09
  • 打赏
  • 举报
回复
不知道,估计有困难吧,表格的形式不同的话怎么弄呢
hbgzg3006 2009-04-09
  • 打赏
  • 举报
回复
顶一下。关注中。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧