用poi读取word文件 要用到的包有哪些

用poi读取word文件 要用到的包有哪些,第1张

import javaio;

import orgapachepoihwpfHWPFDocument;

import orgapachepoihwpfextractorWordExtractor;

public class ReadDocFile {

public static void main(String[] args) {

File file = null;

WordExtractor extractor = null ;

try {

file = new File("c:\\Newdoc");

FileInputStream fis=new FileInputStream(filegetAbsolutePath());

HWPFDocument document=new HWPFDocument(fis);

extractor = new WordExtractor(document);

String [] fileData = extractorgetParagraphText();

for(int i=0;i<fileDatalength;i++){

if(fileData[i] != null)

Systemoutprintln(fileData[i]);

}

}

catch(Exception exep){}

}

}

怎么样能让poi读取的word按原来的格式显示在页面

因为poi读取word 没法读取到空格和回车这个问题要如何解决呢

poi java

------解决方案--------------------

public static void main(String[] args) {

File file = new File("D:/testdoc");

try {

FileInputStream fis = new FileInputStream(file);

HWPFDocument hwpfd = new HWPFDocument(fis);

WordExtractor wordExtractor = new WordExtractor(hwpfd);

String[] paragraph = wordExtractorgetParagraphText();

for (int i = 0; i < paragraphlength; i++) {

Systemoutprintln(paragraph[i]);

}

} catch (FileNotFoundException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

}

}

关键代码如下:

FileInputStream fileInputStream = new FileInputStream( soureFile);

POIFSFileSystem pfs = new POIFSFileSystem( fileInputStream );

HWPFDocument hwpf = new HWPFDocument(pfs);// make a HWPFDocument object

OutputStream output = new FileOutputStream( targetFile );

hwpfwrite(output);// write to the target file

outputclose();

(2)再word中插入表格。HWPF的情况:

Table tcDataTable = rangeinsertTableBefore( (short)column , row);//column and row列数和行数

tcDataTablegetRow(i)getCell(j)getParagraph(0)getCharacterRun(0)insertBefore("插入i行j列的内容" );

XWPF的情况:

String outputFile = "D:\\testdoc";

XWPFDocument document = new XWPFDocument();

XWPFTable tableOne = documentcreateTable();

XWPFTableRow tableOneRowOne = tableOnegetRow(0);

tableOneRowOnegetCell(0)setText("11");

XWPFTableCell cell12 = tableOneRowOnecreateCell();

cell12setText("12");

// tableOneRowOneaddNewTableCell()setText("第1行第2列");

// tableOneRowOneaddNewTableCell()setText("第1行第3列");

// tableOneRowOneaddNewTableCell()setText("第1行第4列");

XWPFTableRow tableOneRowTwo = tableOnecreateRow();

tableOneRowTwogetCell(0)setText("21");

tableOneRowTwogetCell(1)setText("22");

// tableOneRowTwogetCell(2)setText("第2行第3列");

XWPFTableRow tableOneRow3 = tableOnecreateRow();

tableOneRow3addNewTableCell()setText("31");

tableOneRow3addNewTableCell()setText("32");

FileOutputStream fOut;

try {

fOut = new FileOutputStream(outputFile);

documentwrite(fOut);

fOutflush();

// *** 作结束,关闭文件

fOutclose();

} catch (Exception e) {

eprintStackTrace();

}

public void printWord(>

以上就是关于用poi读取word文件 要用到的包有哪些全部的内容,包括:用poi读取word文件 要用到的包有哪些、如何样能让poi读取的word按原来的格式显示在页面、使用poi *** 作word时如何在有多个表格的word中定位到其中一个表格等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9562878.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存