通過下面的兩種方法可以從文檔里讀取所有字符性的內容(忽略字符的屬性)。
通過輸出流來寫到文本文件中。
public static void getWordContent(String fileName) throws Exception{
?? FileInputStream in = new FileInputStream(new File(fileName));
?? WordExtractor extractor = new WordExtractor(in);
?? String text = extractor.getText();
?? FileWriter f = new FileWriter(new File("e:\\Test.txt"));
?? f.write(text);
?? f.close();
}
public static void getWordDetail(String fileName) throws Exception{
?? FileInputStream in = new FileInputStream(new File(fileName));
?? FileOutputStream out = new FileOutputStream(new File("e:\\test.txt"));
?? HWPFDocument doc = new HWPFDocument(in);
?? System.out.println("文檔長度:"+doc.characterLength());
?? Range range = doc.getRange();
?? String text = range.text();
?? System.out.println(text);
?? byte[] _inBuf = text.getBytes();
?? out.write(_inBuf);
?? out.close();
}
如果要每個字符的樣式,可以用CharaterRun這個類,它的方法專門用于獲得字符和判斷的樣式。
如:
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.CharacterRun;
public class Angle {
public static void getAwayAngle(String fileName) throws Exception {
?? FileInputStream in = new FileInputStream(new File(fileName));
?? HWPFDocument doc = new HWPFDocument(in);
?? int length = doc.characterLength();
?? StringBuffer sb = new StringBuffer();
?? for (int i = 0; i < length-1; i++) {
?? Range range = new Range(i, i+1, doc);
?????? //之所以用這個構造方法,是因為整篇文章的字符判斷不準確。只好一個字符一個字符的來判斷。
?????? //而且API的說明文字相當的不全。
?? for(int j=0;j<range.numCharacterRuns();j++){
???? CharacterRun cr=range.getCharacterRun(j);
???? if(cr.getSubSuperScriptIndex()==0)//getSubSuperScriptIndex()這個方法來判斷是否是上下角標
???? sb.append(range.text());
?? }
?? }
?? System.out.println(sb.toString());
}
public static void main(String[] args) {
?? try {
?? getAwayAngle("e:\\test1.doc");
?? } catch (Exception e) {
?? e.printStackTrace();
?? }
}
}
在POI中Range這個類是核心類。里面有很多方法用來操作WORD文檔。
還有其它比較重要的類Section和Paragraph等。
POI沒有中文的API,英文的API說明相當不全,方法多數只能靠猜。Apache太不負責任了。
通過輸出流來寫到文本文件中。
public static void getWordContent(String fileName) throws Exception{
?? FileInputStream in = new FileInputStream(new File(fileName));
?? WordExtractor extractor = new WordExtractor(in);
?? String text = extractor.getText();
?? FileWriter f = new FileWriter(new File("e:\\Test.txt"));
?? f.write(text);
?? f.close();
}
public static void getWordDetail(String fileName) throws Exception{
?? FileInputStream in = new FileInputStream(new File(fileName));
?? FileOutputStream out = new FileOutputStream(new File("e:\\test.txt"));
?? HWPFDocument doc = new HWPFDocument(in);
?? System.out.println("文檔長度:"+doc.characterLength());
?? Range range = doc.getRange();
?? String text = range.text();
?? System.out.println(text);
?? byte[] _inBuf = text.getBytes();
?? out.write(_inBuf);
?? out.close();
}
如果要每個字符的樣式,可以用CharaterRun這個類,它的方法專門用于獲得字符和判斷的樣式。
如:
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.CharacterRun;
public class Angle {
public static void getAwayAngle(String fileName) throws Exception {
?? FileInputStream in = new FileInputStream(new File(fileName));
?? HWPFDocument doc = new HWPFDocument(in);
?? int length = doc.characterLength();
?? StringBuffer sb = new StringBuffer();
?? for (int i = 0; i < length-1; i++) {
?? Range range = new Range(i, i+1, doc);
?????? //之所以用這個構造方法,是因為整篇文章的字符判斷不準確。只好一個字符一個字符的來判斷。
?????? //而且API的說明文字相當的不全。
?? for(int j=0;j<range.numCharacterRuns();j++){
???? CharacterRun cr=range.getCharacterRun(j);
???? if(cr.getSubSuperScriptIndex()==0)//getSubSuperScriptIndex()這個方法來判斷是否是上下角標
???? sb.append(range.text());
?? }
?? }
?? System.out.println(sb.toString());
}
public static void main(String[] args) {
?? try {
?? getAwayAngle("e:\\test1.doc");
?? } catch (Exception e) {
?? e.printStackTrace();
?? }
}
}
在POI中Range這個類是核心類。里面有很多方法用來操作WORD文檔。
還有其它比較重要的類Section和Paragraph等。
POI沒有中文的API,英文的API說明相當不全,方法多數只能靠猜。Apache太不負責任了。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
