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

Java 从HWPFDocument(.doc文件)中提取段落/字符串后,将数据写入excel文件,java,file-io,apache-poi,hssf,hwpf,Java,File Io,Apache Poi,Hssf,Hwpf,在这里,我的代码帮助我将.doc文件中的数据提取到段落和特定的字符串搜索中。我可以使用eclipse运行配置将其手动输出。但是1)我想让它直接输出到excel文件中。doc文件。2)输出必须只指定单元格 public static void readParagraphs(HWPFDocument docx) throws Exception{ we = new WordExtractor(docx); String[] paragraphs = we.getParagraphTe

在这里,我的代码帮助我将.doc文件中的数据提取到段落和特定的字符串搜索中。我可以使用eclipse运行配置将其手动输出。但是1)我想让它直接输出到excel文件中。doc文件。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
在这里,您可以看到我的HSSF工作手册代码

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” 但有些文档没有“类型”,因此,我会选择两个选项,要么从“模式”中查找下一行直到“对齐”,要么从“模式识别”中提取“类型”行。寻求建议

下表在上表之后可用

 -----------------------------------------------------------
|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
-----------------------------------------------------------
|理由|
|-----------------------------------------------------------
|| xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
|| XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX字母数字XXX|
-----------------------------------------------------------
导入java.io.FileOutputStream;
导入java.io.IOException;
导入org.apache.poi.poifs.filesystem.*;
导入org.apache.poi.sl.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.CellStyle;
导入org.apache.poi.ss.usermodel.DataFormat;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.hwpf.hwpf文档;
导入org.apache.poi.hwpf.extractor.WordExtractor;
导入org.apache.poi.hwpf.usermodel.HeaderStories;
导入java.util.ArrayList;
导入java.util.List;
导入java.io.*;
公共类ReadDocFileFromJava{
公共静态int测试=0;
私有静态字提取器;
公共静态void main(字符串[]args)引发IOException{
列表文件名=新的ArrayList();
文件名。添加(“C:\\1200.doc”);
文件名。添加(“C:\\1210.doc”);
文件名。添加(“C:\\1211.doc”);
文件名。添加(“C:\\1212.doc”);
文件名。添加(“C:\\1213.doc”);
//文件2
文件名。添加(“C:\\1214.doc”);
文件名。添加(“C:\\1215.doc”);
文件名。添加(“C:\\1216.doc”);
文件名。添加(“C:\\1217.doc”);

for(inti=0;i稍微修改了您的代码,但它肯定还不能工作,因为我缺少信息。也许您可以尝试编写其余的代码

public class ReadDocFileFromJava {

    public static int test = 0;

    private static WordExtractor ex;

    private static List<String[]> allParagraphs;

    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++) {
            allParagraphs.add(readMyDocument(fileName.get(i)));
        }

    }

    public static String[] readMyDocument(String i) {
        POIFSFileSystem fs = null;
        String[] paragraph;
        try {
            fs = new POIFSFileSystem(new FileInputStream(i));
            HWPFDocument docx = new HWPFDocument(fs);
            ex = new WordExtractor(docx);
            paragraph = ex.getParagraphText();
            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return paragraph;

    }

    public static void readParagraphs(List<String[]> paragraphs) throws Exception {

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

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

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

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

                workbook.close();

            }
        }
    }
}
公共类ReadDocFileFromJava{
公共静态int测试=0;
私有静态字提取器;
私有静态列表所有段落;
公共静态void main(字符串[]args)引发IOException{
列表文件名=新的ArrayList();
文件名。添加(“C:\\1200.doc”);
文件名。添加(“C:\\1210.doc”);
文件名。添加(“C:\\1211.doc”);
文件名。添加(“C:\\1212.doc”);
文件名。添加(“C:\\1213.doc”);
//文件2
文件名。添加(“C:\\1214.doc”);
文件名。添加(“C:\\1215.doc”);
文件名。添加(“C:\\1216.doc”);
文件名。添加(“C:\\1217.doc”);
对于(int i=0;i对于(int i=1;i您必须更清楚地说明您试图实现的目标。1。)
我希望它将输出直接导入excel文件中。doc文件
这是什么意思?2。)
输出必须仅指定单元格
我们如何知道哪个输出和哪个单元格?1)当我们运行代码时,它必须使用FileOutputStreams而不是控制台输出创建excel文件。2)请忽略此项。我不太清楚。是的。我希望它在excel中。我知道Apache POI.sry不支持csv,该注释是错误的选项。如果要创建excel文件,请使用类似以下内容
尝试(OutputStream out=Files.newOutputStream(new Path(),StandardOpenOption.create_new)){this.workBook.write(out);this.workBook.close();return Path.toFile();}
使用if(type.startsWith(“type”))由控制台输出上的HWPFDocument生成的此字符串方法的结果。如何将此字符串方法中的引用变量/创建对象调用到outputstreams中。我确实查看了链接,对于XSSF和HSSF很好。