从HWPFDocument(.doc文件)中提取段落/字符串后,将数据写入excel文件

时间:2017-04-26 13:01:53

标签: java file-io apache-poi hssf hwpf

这里我的代码帮助我将.doc文件中的数据提取到段落和特定的字符串搜索中。我可以使用eclipse运行配置将其手动输出。但是1)我希望它将输出定向到excel文件,其中.doc file.2)输出必须仅指定单元格。

public static void readParagraphs(HWPFDocument docx) throws Exception{
    we = new WordExtractor(docx);
    String[] paragraphs = we.getParagraphText();     
    // To fetch for mode
    for(String p: paragraphs){
    if(p.startsWith("MODE"))
    System.out.println("       "+p);
    }
    for(String type: paragraphs ){
    if(type.startsWith("TYPE"))
    System.out.format("       "+type);
    }
    }

预期产出:

S.no  | Doc name  | Title    | mode            | type
=====================================================================
1     | laptop    | A12345   | abcd 123456     | efghij A12345/123456
2     | laptop    | A12346   | abcd 123457     | efghij A12345/123457
3     | laptop    | A12347   | abcd 123458     | efghij A12345/123458

在这里你可以看到我的HSSFWorkbook代码。

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("firstsheet");
Row row1 = sheet.createRow((short) 0);
row1.createCell(0).setCellValue("S.NO");
row1.createCell(1).setCellValue("DOC NAME");
row1.createCell(2).setCellValue("TITLE");
row1.createCell(3).setCellValue("MODE");
row1.createCell(4).setCellValue("TYPE");
Row row2 = sheet.createRow(rowNum++);
row2.createCell(3).setCellValue(" "+mode);
row2.createCell(4).setCellValue(" "+type);
Row row3 = sheet.createRow(rowNum++);
row3.createCell(3).setCellValue(" "+mode);
row3.createCell(4).setCellValue(" "+type);

下表中可用表1,头文件。需要只提取'A12345'

 =====================================
 |  xx |       A12345         |xx    |     
 =====================================

下表可用表2或3-6。取决于每个文档。

 --------------------------------------------.--------------------
|MODE :  Abcde 123456 efghit 234567  sddsldjf 232132             |
|----------------------------------------------------------------|  
|INFO   |TYPE : efghij A12345/123456 dsflsdjflsd B22323/&123456  |
|       |xxxxxxxxxxxxxxxxxalphanumericxxxxxxxxxxxxxxxxxxxxxxxxxx |
 -----------------------------------------------------------------

if(p.startsWith(“MODE”))//此方法有助于打印“MODE:Abcde 123456 efghit 234567 sddsldjf 232132” if(type.startsWith(“TYPE”))//此方法有助于打印“TYPE:efghij A12345 / 123456 dsflsdjflsd B22323 /& 123456” 但是有些文档没有'TYPE'所以,我会选择两个选项来查找下一行,直到'MODE'中的'JUSTIFICATION'或模式识别到获取'TYPE'行。寻求建议。

下表后面的表格

 -----------------------------------------------------------
|JUSTIFICATION                                              |   
|-----------------------------------------------------------
|   |   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx    |
|   |   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx alphanumericxxx    |
 -----------------------------------------------------------


import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.HeaderStories;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
public class ReadDocFileFromJava {

    public static int test = 0;

    private static WordExtractor ex;

        public static void main(String[] args) throws IOException  {

            List<String> fileName=new ArrayList<String>();  


        fileName.add("C:\\1200.doc");
        fileName.add("C:\\1210.doc");
        fileName.add("C:\\1211.doc");
        fileName.add("C:\\1212.doc");
        fileName.add("C:\\1213.doc");
                // document 2 
        fileName.add("C:\\1214.doc");
        fileName.add("C:\\1215.doc");
        fileName.add("C:\\1216.doc");
        fileName.add("C:\\1217.doc");


        for(int i=0;i<fileName.size();i++){

               readMyDocument(fileName.get(i));
        }}

    public static void readMyDocument(String i){
        POIFSFileSystem fs = null;
        try {
            fs = new POIFSFileSystem(new FileInputStream(i));

            HWPFDocument docx = new HWPFDocument(fs);
            ex = new WordExtractor(docx);
           readParagraphs(docx);
           fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public static void readParagraphs(HWPFDocument docx) throws Exception{

        int a =0,b=0,c=0,d=0,celIte=0, celIte2=0,link=0;
        ex = new WordExtractor(docx);
        String[] paragraphs = ex.getParagraphText();

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("firstsheet");
        Row row0 = sheet.createRow(0);
        row0.createCell(0).setCellValue("S.NO");
        row0.createCell(1).setCellValue("DOC NAME");
        row0.createCell(2).setCellValue("TITLE");
        row0.createCell(3).setCellValue("MODE");
        row0.createCell(4).setCellValue("TYPE");
          for(int i=1;i<=10;i++){
                        Row row1 = sheet.createRow(i);
                for(int j=0;j<=0;j++){
                    Cell cell_10 =row1.createCell(j);
                    do{
                        cell_10.setCellValue(celIte);
                        celIte++;
                    }while(celIte<1);

                    for(int k=3;k<=3;k++){

                    Cell cell_12 = row1.createCell(k);
                    for(String p: paragraphs){


                          if(p.startsWith("MODE"))  
                         cell_12.setCellValue(""+p);
        }   }
      workbook.write(new FileOutputStream("C:\\output.xls"));

        workbook.close();

}
                }
        }}

S.NO  | DOC NAME | TITLE | MODE | TYPE
==========================================
1                          XXXX
2                          XXXX 
3                          XXXX
4                          XXXX
5                          XXXX
6                          XXXX
7                          XXXX
8                          XXXX
9                          XXXX

0 个答案:

没有答案