在Java程式設計中,如何使用java從excel表提取內容?
專案的目錄結構如下 -
Tika的工具包可從以下網址下載:http://tika.apache.org/download.html ,只下載:tika-app-1.16.jar 和 tika-server-1.16.jar 。
以下是使用java從excel表提取內容的程式 -
import java.io.File;
import java.io.FileInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.microsoft.ooxml.OOXMLParser;
import org.apache.tika.sax.BodyContentHandler;
public class ExtractContentFromExcel {
public static void main(String args[]) throws Exception {
// detecting the file type
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
FileInputStream inputstream = new FileInputStream(new File("excelExample.xlsx"));
ParseContext pcontext = new ParseContext();
// OOXml parser
OOXMLParser msofficeparser = new OOXMLParser();
msofficeparser.parse(inputstream, handler, metadata, pcontext);
System.out.println("Contents of the document:" + handler.toString());
System.out.println("Metadata of the document:");
String[] metadataNames = metadata.names();
for (String name : metadataNames) {
System.out.println(name + ": " + metadata.get(name));
}
}
}
原ODF檔案:excelExample.xlsx 的內容如下 -
執行上面範例程式碼,得到以下結果 -
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/F:/worksp/javaexamples/libs/tika_libs/tika-app-1.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/F:/worksp/javaexamples/libs/tika_libs/tika-server-1.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
九月 27, 2017 5:15:47 上午 org.apache.tika.config.InitializableProblemHandler$3 handleInitializableProblem
警告: JBIG2ImageReader not loaded. jbig2 files will be ignored
See http://pdfbox.apache.org/2.0/dependencies.html#jai-image-io
for optional dependencies.
TIFFImageWriter not loaded. tiff files will not be processed
See http://pdfbox.apache.org/2.0/dependencies.html#jai-image-io
for optional dependencies.
J2KImageReader not loaded. JPEG2000 files will not be processed.
See http://pdfbox.apache.org/2.0/dependencies.html#jai-image-io
for optional dependencies.
九月 27, 2017 5:15:47 上午 org.apache.tika.config.InitializableProblemHandler$3 handleInitializableProblem
警告: org.xerial's sqlite-jdbc is not loaded.
Please provide the jar on your classpath to parse sqlite files.
See tika-parsers/pom.xml for the correct version.
Contents of the document:Sheet1
編號 姓名 年齡 工作
1001 王傳大 22 Java軟體開發
1002 李小雙 29 專案經理
1020 張在傳 28 人事經理
Sheet2
Sheet3
Metadata of the document:
date: 2017-09-27T09:15:38Z
meta:creation-date: 2017-09-27T09:13:50Z
extended-properties:Application: Microsoft Excel
Creation-Date: 2017-09-27T09:13:50Z
dcterms:created: 2017-09-27T09:13:50Z
Last-Modified: 2017-09-27T09:15:38Z
dcterms:modified: 2017-09-27T09:15:38Z
Last-Save-Date: 2017-09-27T09:15:38Z
protected: false
meta:save-date: 2017-09-27T09:15:38Z
Application-Name: Microsoft Excel
modified: 2017-09-27T09:15:38Z
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
custom:KSOProductBuildVer: 2052-10.1.0.6489