Website Logo. Upload to /source/logo.png ; disable in /source/_includes/logo.html

舞乐 VOLER

舞动我人生

Apache POI使用示例

Dec 05, 2015

读取记事本(txt)中的内容时,一直出现乱码,原因在于记事本的默认编码是GBK,不是UTF-8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package me.voler.jechat.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.extractor.ExcelExtractor;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;

/**
 * 支持取.txt、.doc、.docx、.xls、.xlsx,5种格式文档的内容,以字符串的形式返回。
 * {@link http://poi.apache.org}
 * 
 */
public class DocumentExtraction {

    /**
     * 取微软记事本中的内容,以字符串的形式返回。记事本中内容的编码为GBK
     * 
     * @param file
     * @return
     */
    public static String txt2String(File file) {
        StringBuffer buffer = new StringBuffer();
        try {
            InputStreamReader input = new InputStreamReader(new FileInputStream(file), "GBK");
            BufferedReader reader = new BufferedReader(input);

            String line = null;
            while ((line = reader.readLine()) != null) {
                buffer.append(line).append('\n');
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }

    /**
     * 取Excel xlsx中的内容,以字符串的形式返回。{@link http://poi.apache.org}
     * 
     * @param file
     * @return
     */
    @SuppressWarnings("resource")
    public static String xlsx2String(File file) {

        XSSFWorkbook xlsxwb = new XSSFWorkbook();
        try {
            OPCPackage pkg = OPCPackage.open(new FileInputStream(file));
            xlsxwb = new XSSFWorkbook(pkg);
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ExcelExtractor extractor = new XSSFExcelExtractor(xlsxwb);
        extractor.setFormulasNotResults(true);
        extractor.setIncludeSheetNames(false);

        return extractor.getText();
    }

    /**
     * 取Excel xls中的内容,以字符串的形式返回。{@link http://poi.apache.org}
     * 
     * @param file
     * @return
     */
    @SuppressWarnings("resource")
    public static String xls2String(File file) {

        HSSFWorkbook xlswb = new HSSFWorkbook();
        try {
            POIFSFileSystem fileSystem = new POIFSFileSystem(new FileInputStream(file));
            xlswb = new HSSFWorkbook(fileSystem);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ExcelExtractor extractor = new org.apache.poi.hssf.extractor.ExcelExtractor(xlswb);
        extractor.setFormulasNotResults(true);
        extractor.setIncludeSheetNames(false);

        return extractor.getText();
    }

    /**
     * 取Word doc中的内容,以字符串的形式返回。{@link http://poi.apache.org}
     * 
     * @param file
     * @return
     */
    public static String doc2String(File file) {
        StringBuffer buffer = new StringBuffer();

        WordExtractor extractor = null;
        POIFSFileSystem fileSystem;
        try {
            fileSystem = new POIFSFileSystem(new FileInputStream(file));
            extractor = new WordExtractor(fileSystem);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String[] paragraphText = extractor.getParagraphText();
        for (String paragraph : paragraphText) {
            buffer.append(paragraph);
        }

        return buffer.toString();
    }

    /**
     * 取Word docx中的内容,以字符串的形式返回。{@link http://poi.apache.org}
     * 
     * @param file
     * @return
     */
    public static String docx2String(File file) {

        XWPFWordExtractor extractor = null;
        try {
            OPCPackage pkg = OPCPackage.open(new FileInputStream(file));
            extractor = new XWPFWordExtractor(pkg);
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (XmlException e) {
            e.printStackTrace();
        } catch (OpenXML4JException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return extractor.getText();
    }

}

pom.xml中的依赖包配置,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<dependencies>
    ...
  <!-- xls -->
  <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.12</version>
  </dependency>
  <!-- doc -->
  <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-scratchpad</artifactId>
      <version>3.12</version>
  </dependency>
  <!-- xlsx docx -->
  <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.12</version>
  </dependency>
</dependencies>