Apache-Tika解析Excell文档
通常在使用爬虫时,爬取到网上的文章都是各式各样的格式处理起来比较麻烦,这里我们使用Apache-Tika来处理Excell格式的文章,如下:
package com.mengyao.tika.app; 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 ExcellApp { public static void main(final String[] args) throws Exception { // Tika默认是10*1024*1024,这里防止文件过大导致Tika报错 BodyContentHandler handler = new BodyContentHandler(1024 * 1024 * 10); Metadata metadata = new Metadata(); FileInputStream inputstream = new FileInputStream(new File("D:/报价清单.xlsx")); ParseContext pcontext = new ParseContext(); // 解析Excell文档时应由超类AbstractParser的派生类OOXMLParser实现 OOXMLParser msofficeparser = new OOXMLParser(); msofficeparser.parse(inputstream, handler, metadata, pcontext); // 获取Excell文档的内容 System.out.println("Excell文档内容:" + handler.toString()); // 获取Excell文档的元数据 System.out.println("Excell文档元数据:"); String[] metadataNames = metadata.names(); for (String name : metadataNames) { System.out.println(name + " : " + metadata.get(name)); } } }