通過(guò)下面的兩種方法可以從文檔里讀取所有字符性的內(nèi)容(忽略字符的屬性)。
通過(guò)輸出流來(lái)寫(xiě)到文本文件中。
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("文檔長(zhǎng)度:"+doc.characterLength());
?? Range range = doc.getRange();
?? String text = range.text();
?? System.out.println(text);
?? byte[] _inBuf = text.getBytes();
?? out.write(_inBuf);
?? out.close();
}
如果要每個(gè)字符的樣式,可以用CharaterRun這個(gè)類,它的方法專門(mén)用于獲得字符和判斷的樣式。
如:
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);
?????? //之所以用這個(gè)構(gòu)造方法,是因?yàn)檎恼碌淖址袛嗖粶?zhǔn)確。只好一個(gè)字符一個(gè)字符的來(lái)判斷。
?????? //而且API的說(shuō)明文字相當(dāng)?shù)牟蝗?
?? for(int j=0;j<range.numCharacterRuns();j++){
???? CharacterRun cr=range.getCharacterRun(j);
???? if(cr.getSubSuperScriptIndex()==0)//getSubSuperScriptIndex()這個(gè)方法來(lái)判斷是否是上下角標(biāo)
???? 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這個(gè)類是核心類。里面有很多方法用來(lái)操作WORD文檔。
還有其它比較重要的類Section和Paragraph等。
POI沒(méi)有中文的API,英文的API說(shuō)明相當(dāng)不全,方法多數(shù)只能靠猜。Apache太不負(fù)責(zé)任了。
通過(guò)輸出流來(lái)寫(xiě)到文本文件中。
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("文檔長(zhǎng)度:"+doc.characterLength());
?? Range range = doc.getRange();
?? String text = range.text();
?? System.out.println(text);
?? byte[] _inBuf = text.getBytes();
?? out.write(_inBuf);
?? out.close();
}
如果要每個(gè)字符的樣式,可以用CharaterRun這個(gè)類,它的方法專門(mén)用于獲得字符和判斷的樣式。
如:
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);
?????? //之所以用這個(gè)構(gòu)造方法,是因?yàn)檎恼碌淖址袛嗖粶?zhǔn)確。只好一個(gè)字符一個(gè)字符的來(lái)判斷。
?????? //而且API的說(shuō)明文字相當(dāng)?shù)牟蝗?
?? for(int j=0;j<range.numCharacterRuns();j++){
???? CharacterRun cr=range.getCharacterRun(j);
???? if(cr.getSubSuperScriptIndex()==0)//getSubSuperScriptIndex()這個(gè)方法來(lái)判斷是否是上下角標(biāo)
???? 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這個(gè)類是核心類。里面有很多方法用來(lái)操作WORD文檔。
還有其它比較重要的類Section和Paragraph等。
POI沒(méi)有中文的API,英文的API說(shuō)明相當(dāng)不全,方法多數(shù)只能靠猜。Apache太不負(fù)責(zé)任了。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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