POI实现读写内容到word中
讀取word中的內容
/**
?* 利用POI實現從word中讀取內容
?*/
============================================
將讀取的內容寫入另外一個文件中
package com.cy;
/**
?* WordReader類中readDoc的作用為從word中將數據讀出
?*/
import com.cy.WordWriter;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class WordWriter {
?public static boolean writeDoc(String path, String content) {
??boolean w = false;
??try {
??// byte b[] = content.getBytes("ISO-8859-1");
??byte b[] = content.getBytes();
??ByteArrayInputStream bais = new ByteArrayInputStream(b);
??POIFSFileSystem fs = new POIFSFileSystem();
??DirectoryEntry directory = fs.getRoot();
??DocumentEntry de = directory.createDocument("WordDocument", bais);
??FileOutputStream ostream = new FileOutputStream(path);
??fs.writeFilesystem(ostream);
??bais.close();
??ostream.close();
??} catch (IOException e) {
??e.printStackTrace();
??}
??return w;
??}
??public static void main(String[] args) throws Exception{
??String wr=WordReader.readDoc("D://test.doc");
??boolean b = writeDoc("D://result.doc",wr);
??}
}
?//目前該程序只能實現對簡單的文字的操作,無法實現對表格樣式的操作,繼續改進,請關注!!
?
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hwpf.extractor.WordExtractor;
public class WordReader {
?public static String readDoc(String doc) throws Exception {
???? // 創建輸入流讀取DOC文件
???? FileInputStream in = new FileInputStream(new File(doc));
???? WordExtractor extractor = null;
???? String text = null;
???? // 創建WordExtractor
???? extractor = new WordExtractor(in);
???? // 對DOC文件進行提取
???? text = extractor.getText();
???? return text;
?}
?
?
?public static void main(String[] args) {
??????? try{
?????????? String text = WordReader.readDoc("d://test.doc");
?????????? System.out.println(text);
??????? }catch(Exception e){
??????????? e.printStackTrace();
??????? }
??? }
}
摘自:http://blog.csdn.net/sunrise353/article/details/1762324
?
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
?
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
?
import java.io.File;?
import java.io.FileInputStream;??
import java.io.InputStream;?? ?
import org.apache.poi.POIXMLDocument;??
import org.apache.poi.POIXMLTextExtractor;??
import org.apache.poi.hwpf.extractor.WordExtractor;??
import org.apache.poi.openxml4j.opc.OPCPackage;??
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;?
?
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
?
public class ExportDocImpl { ?
public void testWord(){ ?
?try{ ?????
FileInputStream in = new FileInputStream("D:\\sinye.doc");//載入文檔 ????
POIFSFileSystem pfs = new POIFSFileSystem(in);?? ?????
HWPFDocument hwpf = new HWPFDocument(pfs);?? ?????
Range range = hwpf.getRange();//得到文檔的讀取范圍 ?????
TableIterator it = new TableIterator(range); ????
//迭代文檔中的表格 ?????
while (it.hasNext()) {?? ?????????
Table tb = (Table) it.next();?? ?????????
//迭代行,默認從0開始 ?????????
for (int i = 0; i < tb.numRows(); i++) {?? ?????????????
TableRow tr = tb.getRow(i);?? ?????????????
//迭代列,默認從0開始 ?????????????
for (int j = 0; j < tr.numCells(); j++) {?? ?????????????????
TableCell td = tr.getCell(j);//取得單元格 ?????????????????
//取得單元格的內容 ?????????????????
for(int k=0;k<td.numParagraphs();k++){?? ?????????????????????
Paragraph para =td.getParagraph(k);?? ?????????????????????
String s = para.text();?? ?????????????????????
System.out.println(s); ?????????????????
} //end for??? ????????????
? }?? //end for ????????
? }?? //end for ?????
} //end while ??
}catch(Exception e){ ??
?e.printStackTrace(); ??
} ?
}//end method ? ? ??????????
public void testWord1(){ ??????????
try {?? ???????????
//word 2003: 圖片不會被讀取?? ???????????
InputStream is = new FileInputStream(new File("D:\\sinye.doc"));?? ?????????????????
WordExtractor ex = new WordExtractor(is);?? ?????????????????
String text2003 = ex.getText();?? ?????????????????
System.out.println(text2003);?? ???????????
//word 2007 圖片不會被讀取, 表格中的數據會被放在字符串的最后?? ???????????
OPCPackage opcPackage = POIXMLDocument.openPackage("D:\\sinye.doc");?? ?????????????????
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);?? ?????????????????
String text2007 = extractor.getText();?? ?????????????????
System.out.println(text2007);?? ?????????????? ???????
} catch (Exception e) {?? ?????????????????
e.printStackTrace();?? ???????
} ???
}
}
?
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException;import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Table; import org.apache.poi.hwpf.usermodel.TableCell; import org.apache.poi.hwpf.usermodel.TableIterator; import org.apache.poi.hwpf.usermodel.TableRow;import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import org.apache.poi.POIXMLDocument; import org.apache.poi.POIXMLTextExtractor; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.xwpf.extractor.XWPFWordExtractor; import org.apache.poi.poifs.filesystem.POIFSFileSystem;public class ExportDocImpl {public void testWord(){try{FileInputStream in = new FileInputStream("D:\\sinye.doc");//載入文檔POIFSFileSystem pfs = new POIFSFileSystem(in); HWPFDocument hwpf = new HWPFDocument(pfs); Range range = hwpf.getRange();//得到文檔的讀取范圍TableIterator it = new TableIterator(range);//迭代文檔中的表格while (it.hasNext()) { Table tb = (Table) it.next(); //迭代行,默認從0開始for (int i = 0; i < tb.numRows(); i++) { TableRow tr = tb.getRow(i); //迭代列,默認從0開始for (int j = 0; j < tr.numCells(); j++) { TableCell td = tr.getCell(j);//取得單元格//取得單元格的內容for(int k=0;k<td.numParagraphs();k++){ Paragraph para =td.getParagraph(k); String s = para.text(); System.out.println(s);} //end for } //end for} //end for} //end while}catch(Exception e){e.printStackTrace();}}//end methodpublic void testWord1(){try { //word 2003: 圖片不會被讀取 InputStream is = new FileInputStream(new File("D:\\sinye.doc")); WordExtractor ex = new WordExtractor(is); String text2003 = ex.getText(); System.out.println(text2003); //word 2007 圖片不會被讀取, 表格中的數據會被放在字符串的最后 OPCPackage opcPackage = POIXMLDocument.openPackage("D:\\sinye.doc"); POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage); String text2007 = extractor.getText(); System.out.println(text2007); } catch (Exception e) { e.printStackTrace(); } } }?
?
?
轉載于:https://www.cnblogs.com/wh-king/articles/2490432.html
總結
以上是生活随笔為你收集整理的POI实现读写内容到word中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在线的IDE(compilr)支持图形界
- 下一篇: 房贷常识