免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
使用POI讀寫Word doc文件

使用POI讀寫word doc文件

目錄

1     word doc文件

1.1     通過(guò)WordExtractor讀文件

1.2     通過(guò)HWPFDocument讀文件

2     word doc文件

 

       Apache poihwpf模塊是專門用來(lái)對(duì)word doc文件進(jìn)行讀寫操作的。在hwpf里面我們使用HWPFDocument來(lái)表示一個(gè)word doc文檔。在HWPFDocument里面有這么幾個(gè)概念:

l  Range:它表示一個(gè)范圍,這個(gè)范圍可以是整個(gè)文檔,也可以是里面的某一小節(jié)(Section),也可以是某一個(gè)段落(Paragraph),還可以是擁有共同屬性的一段文本(CharacterRun)。

l  Sectionword文檔的一個(gè)小節(jié),一個(gè)word文檔可以由多個(gè)小節(jié)構(gòu)成。

l  Paragraphword文檔的一個(gè)段落,一個(gè)小節(jié)可以由多個(gè)段落構(gòu)成。

l  CharacterRun:具有相同屬性的一段文本,一個(gè)段落可以由多個(gè)CharacterRun組成。

l  Table:一個(gè)表格。

l  TableRow:表格對(duì)應(yīng)的行。

l  TableCell:表格對(duì)應(yīng)的單元格。

       Section、Paragraph、CharacterRunTable都繼承自Range

1       word doc文件

       在日常應(yīng)用中,我們從word文件里面讀取信息的情況非常少見(jiàn),更多的還是把內(nèi)容寫入到word文件中。使用POIword doc文件讀取數(shù)據(jù)時(shí)主要有兩種方式:通過(guò)WordExtractor讀和通過(guò)HWPFDocument讀。在WordExtractor內(nèi)部進(jìn)行信息讀取時(shí)還是通過(guò)HWPFDocument來(lái)獲取的。

 

1.1     通過(guò)WordExtractor讀文件

       在使用WordExtractor讀文件時(shí)我們只能讀到文件的文本內(nèi)容和基于文檔的一些屬性,至于文檔內(nèi)容的屬性等是無(wú)法讀到的。如果要讀到文檔內(nèi)容的屬性則需要使用HWPFDocument來(lái)讀取了。下面是使用WordExtractor讀取文件的一個(gè)示例:

Java代碼  
  1. public class HwpfTest {  
  2.    
  3.    @SuppressWarnings("deprecation")  
  4.    @Test  
  5.    public void testReadByExtractor() throws Exception {  
  6.       InputStream is = new FileInputStream("D:\\test.doc");  
  7.       WordExtractor extractor = new WordExtractor(is);  
  8.       //輸出word文檔所有的文本  
  9.       System.out.println(extractor.getText());  
  10.       System.out.println(extractor.getTextFromPieces());  
  11.       //輸出頁(yè)眉的內(nèi)容  
  12.       System.out.println("頁(yè)眉:" + extractor.getHeaderText());  
  13.       //輸出頁(yè)腳的內(nèi)容  
  14.       System.out.println("頁(yè)腳:" + extractor.getFooterText());  
  15.       //輸出當(dāng)前word文檔的元數(shù)據(jù)信息,包括作者、文檔的修改時(shí)間等。  
  16.       System.out.println(extractor.getMetadataTextExtractor().getText());  
  17.       //獲取各個(gè)段落的文本  
  18.       String paraTexts[] = extractor.getParagraphText();  
  19.       for (int i=0; i<paraTexts.length; i++) {  
  20.          System.out.println("Paragraph " + (i+1) + " : " + paraTexts[i]);  
  21.       }  
  22.       //輸出當(dāng)前word的一些信息  
  23.       printInfo(extractor.getSummaryInformation());  
  24.       //輸出當(dāng)前word的一些信息  
  25.       this.printInfo(extractor.getDocSummaryInformation());  
  26.       this.closeStream(is);  
  27.    }  
  28.     
  29.    /** 
  30.     * 輸出SummaryInfomation 
  31.     * @param info 
  32.     */  
  33.    private void printInfo(SummaryInformation info) {  
  34.       //作者  
  35.       System.out.println(info.getAuthor());  
  36.       //字符統(tǒng)計(jì)  
  37.       System.out.println(info.getCharCount());  
  38.       //頁(yè)數(shù)  
  39.       System.out.println(info.getPageCount());  
  40.       //標(biāo)題  
  41.       System.out.println(info.getTitle());  
  42.       //主題  
  43.       System.out.println(info.getSubject());  
  44.    }  
  45.     
  46.    /** 
  47.     * 輸出DocumentSummaryInfomation 
  48.     * @param info 
  49.     */  
  50.    private void printInfo(DocumentSummaryInformation info) {  
  51.       //分類  
  52.       System.out.println(info.getCategory());  
  53.       //公司  
  54.       System.out.println(info.getCompany());  
  55.    }  
  56.     
  57.    /** 
  58.     * 關(guān)閉輸入流 
  59.     * @param is 
  60.     */  
  61.    private void closeStream(InputStream is) {  
  62.       if (is != null) {  
  63.          try {  
  64.             is.close();  
  65.          } catch (IOException e) {  
  66.             e.printStackTrace();  
  67.          }  
  68.       }  
  69.    }  
  70.     
  71. }  

 

 

1.2     通過(guò)HWPFDocument讀文件

       HWPFDocument是當(dāng)前Word文檔的代表,它的功能比WordExtractor要強(qiáng)。通過(guò)它我們可以讀取文檔中的表格、列表等,還可以對(duì)文檔的內(nèi)容進(jìn)行新增、修改和刪除操作。只是在進(jìn)行完這些新增、修改和刪除后相關(guān)信息是保存在HWPFDocument中的,也就是說(shuō)我們改變的是HWPFDocument,而不是磁盤上的文件。如果要使這些修改生效的話,我們可以調(diào)用HWPFDocumentwrite方法把修改后的HWPFDocument輸出到指定的輸出流中。這可以是原文件的輸出流,也可以是新文件的輸出流(相當(dāng)于另存為)或其它輸出流。下面是一個(gè)通過(guò)HWPFDocument讀文件的示例:

Java代碼  
  1. public class HwpfTest {  
  2.     
  3.    @Test  
  4.    public void testReadByDoc() throws Exception {  
  5.       InputStream is = new FileInputStream("D:\\test.doc");  
  6.       HWPFDocument doc = new HWPFDocument(is);  
  7.       //輸出書(shū)簽信息  
  8.       this.printInfo(doc.getBookmarks());  
  9.       //輸出文本  
  10.       System.out.println(doc.getDocumentText());  
  11.       Range range = doc.getRange();  
  12. //    this.insertInfo(range);  
  13.       this.printInfo(range);  
  14.       //讀表格  
  15.       this.readTable(range);  
  16.       //讀列表  
  17.       this.readList(range);  
  18.       //刪除range  
  19.       Range r = new Range(2, 5, doc);  
  20.       r.delete();//在內(nèi)存中進(jìn)行刪除,如果需要保存到文件中需要再把它寫回文件  
  21.       //把當(dāng)前HWPFDocument寫到輸出流中  
  22.       doc.write(new FileOutputStream("D:\\test.doc"));  
  23.       this.closeStream(is);  
  24.    }  
  25.     
  26.    /** 
  27.     * 關(guān)閉輸入流 
  28.     * @param is 
  29.     */  
  30.    private void closeStream(InputStream is) {  
  31.       if (is != null) {  
  32.          try {  
  33.             is.close();  
  34.          } catch (IOException e) {  
  35.             e.printStackTrace();  
  36.          }  
  37.       }  
  38.    }  
  39.     
  40.    /** 
  41.     * 輸出書(shū)簽信息 
  42.     * @param bookmarks 
  43.     */  
  44.    private void printInfo(Bookmarks bookmarks) {  
  45.       int count = bookmarks.getBookmarksCount();  
  46.       System.out.println("書(shū)簽數(shù)量:" + count);  
  47.       Bookmark bookmark;  
  48.       for (int i=0; i<count; i++) {  
  49.          bookmark = bookmarks.getBookmark(i);  
  50.          System.out.println("書(shū)簽" + (i+1) + "的名稱是:" + bookmark.getName());  
  51.          System.out.println("開(kāi)始位置:" + bookmark.getStart());  
  52.          System.out.println("結(jié)束位置:" + bookmark.getEnd());  
  53.       }  
  54.    }  
  55.     
  56.    /** 
  57.     * 讀表格 
  58.     * 每一個(gè)回車符代表一個(gè)段落,所以對(duì)于表格而言,每一個(gè)單元格至少包含一個(gè)段落,每行結(jié)束都是一個(gè)段落。 
  59.     * @param range 
  60.     */  
  61.    private void readTable(Range range) {  
  62.       //遍歷range范圍內(nèi)的table。  
  63.       TableIterator tableIter = new TableIterator(range);  
  64.       Table table;  
  65.       TableRow row;  
  66.       TableCell cell;  
  67.       while (tableIter.hasNext()) {  
  68.          table = tableIter.next();  
  69.          int rowNum = table.numRows();  
  70.          for (int j=0; j<rowNum; j++) {  
  71.             row = table.getRow(j);  
  72.             int cellNum = row.numCells();  
  73.             for (int k=0; k<cellNum; k++) {  
  74.                 cell = row.getCell(k);  
  75.                 //輸出單元格的文本  
  76.                 System.out.println(cell.text().trim());  
  77.             }  
  78.          }  
  79.       }  
  80.    }  
  81.     
  82.    /** 
  83.     * 讀列表 
  84.     * @param range 
  85.     */  
  86.    private void readList(Range range) {  
  87.       int num = range.numParagraphs();  
  88.       Paragraph para;  
  89.       for (int i=0; i<num; i++) {  
  90.          para = range.getParagraph(i);  
  91.          if (para.isInList()) {  
  92.             System.out.println("list: " + para.text());  
  93.          }  
  94.       }  
  95.    }  
  96.     
  97.    /** 
  98.     * 輸出Range 
  99.     * @param range 
  100.     */  
  101.    private void printInfo(Range range) {  
  102.       //獲取段落數(shù)  
  103.       int paraNum = range.numParagraphs();  
  104.       System.out.println(paraNum);  
  105.       for (int i=0; i<paraNum; i++) {  
  106. //       this.insertInfo(range.getParagraph(i));  
  107.          System.out.println("段落" + (i+1) + ":" + range.getParagraph(i).text());  
  108.          if (i == (paraNum-1)) {  
  109.             this.insertInfo(range.getParagraph(i));  
  110.          }  
  111.       }  
  112.       int secNum = range.numSections();  
  113.       System.out.println(secNum);  
  114.       Section section;  
  115.       for (int i=0; i<secNum; i++) {  
  116.          section = range.getSection(i);  
  117.          System.out.println(section.getMarginLeft());  
  118.          System.out.println(section.getMarginRight());  
  119.          System.out.println(section.getMarginTop());  
  120.          System.out.println(section.getMarginBottom());  
  121.          System.out.println(section.getPageHeight());  
  122.          System.out.println(section.text());  
  123.       }  
  124.    }  
  125.     
  126.    /** 
  127.     * 插入內(nèi)容到Range,這里只會(huì)寫到內(nèi)存中 
  128.     * @param range 
  129.     */  
  130.    private void insertInfo(Range range) {  
  131.       range.insertAfter("Hello");  
  132.    }  
  133.     
  134. }  

 

 

2       word doc文件

       在使用POIword doc文件的時(shí)候我們必須要先有一個(gè)doc文件才行,因?yàn)槲覀冊(cè)趯?/span>doc文件的時(shí)候是通過(guò)HWPFDocument來(lái)寫的,而HWPFDocument是要依附于一個(gè)doc文件的。所以通常的做法是我們先在硬盤上準(zhǔn)備好一個(gè)內(nèi)容空白的doc文件,然后建立一個(gè)基于該空白文件的HWPFDocument。之后我們就可以往HWPFDocument里面新增內(nèi)容了,然后再把它寫入到另外一個(gè)doc文件中,這樣就相當(dāng)于我們使用POI生成了word doc文件。

       在實(shí)際應(yīng)用中,我們?cè)谏?/span>word文件的時(shí)候都是生成某一類文件,該類文件的格式是固定的,只是某些字段不一樣罷了。所以在實(shí)際應(yīng)用中,我們大可不必將整個(gè)word文件的內(nèi)容都通過(guò)HWPFDocument生成。而是先在磁盤上新建一個(gè)word文檔,其內(nèi)容就是我們需要生成的word文件的內(nèi)容,然后把里面一些屬于變量的內(nèi)容使用類似于“${paramName}”這樣的方式代替。這樣我們?cè)诨谀承┬畔⑸?/span>word文件的時(shí)候,只需要獲取基于該word文件的HWPFDocument,然后調(diào)用RangereplaceText()方法把對(duì)應(yīng)的變量替換為對(duì)應(yīng)的值即可,之后再把當(dāng)前的HWPFDocument寫入到新的輸出流中。這種方式在實(shí)際應(yīng)用中用的比較多,因?yàn)樗坏梢詼p少我們的工作量,還可以讓文本的格式更加的清晰。下面我們就來(lái)基于這種方式做一個(gè)示例。

       假設(shè)我們現(xiàn)在擁有一些變動(dòng)的信息,然后需要通過(guò)這些信息生成如下格式的word doc文件:



 

       那么根據(jù)上面的描述,首先第一步,我們建立一個(gè)對(duì)應(yīng)格式的doc文件作為模板,其內(nèi)容是這樣的:



 

       有了這樣一個(gè)模板之后,我們就可以建立對(duì)應(yīng)的HWPFDocument,然后替換對(duì)應(yīng)的變量為相應(yīng)的值,再把HWPFDocument輸出到對(duì)應(yīng)的輸出流即可。下面是對(duì)應(yīng)的代碼。

Java代碼  
  1. public class HwpfTest {  
  2.     
  3.    @Test  
  4.    public void testWrite() throws Exception {  
  5.       String templatePath = "D:\\word\\template.doc";  
  6.       InputStream is = new FileInputStream(templatePath);  
  7.       HWPFDocument doc = new HWPFDocument(is);  
  8.       Range range = doc.getRange();  
  9.       //把range范圍內(nèi)的${reportDate}替換為當(dāng)前的日期  
  10.       range.replaceText("${reportDate}", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));  
  11.       range.replaceText("${appleAmt}", "100.00");  
  12.       range.replaceText("${bananaAmt}", "200.00");  
  13.       range.replaceText("${totalAmt}", "300.00");  
  14.       OutputStream os = new FileOutputStream("D:\\word\\write.doc");  
  15.       //把doc輸出到輸出流中  
  16.       doc.write(os);  
  17.       this.closeStream(os);  
  18.       this.closeStream(is);  
  19.    }  
  20.     
  21.    /** 
  22.     * 關(guān)閉輸入流 
  23.     * @param is 
  24.     */  
  25.    private void closeStream(InputStream is) {  
  26.       if (is != null) {  
  27.          try {  
  28.             is.close();  
  29.          } catch (IOException e) {  
  30.             e.printStackTrace();  
  31.          }  
  32.       }  
  33.    }  
  34.    
  35.    /** 
  36.     * 關(guān)閉輸出流 
  37.     * @param os 
  38.     */  
  39.    private void closeStream(OutputStream os) {  
  40.       if (os != null) {  
  41.          try {  
  42.             os.close();  
  43.          } catch (IOException e) {  
  44.             e.printStackTrace();  
  45.          }  
  46.       }  
  47.    }  
  48.     
  49.    
  50. }  

 

(注:本文是基于poi3.9所寫)

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
POI 使用 HWPFDocument 和 XWPFDocument 分割 word2003 和 word2010示例
POI抽取word文檔圖片并保存
Java 操作Word表格數(shù)據(jù)
PoiDemo【Android將表單數(shù)據(jù)生成Word文檔的方案之二(基于Poi4.0.0)】
利用word將PDF轉(zhuǎn)換DOC文件的方法 --電腦高手
java poi操作word轉(zhuǎn)pdf
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服