2

There is a simple way of extracting data which I think only fetch it as a text ( using toSting() method), but I want to fetch data according to specified column or row’s name. Following is a sample code which simply print the content of the MS Excel along with its metadata (avoid this part). It uses tika-app-1.13.jar (if you want to run this code add this library)

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.tika.exception.TikaException;
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;

import org.xml.sax.SAXException;

public class MSExcelParse {

    public static void main(final String[] args)
            throws IOException, TikaException, SAXException {

        //detecting the file type
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        FileInputStream inputstream = new FileInputStream(new
                File("C:\\Users\\Username\\IdeaProjects\\Tika\\src\\example.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));
        }
    }
}

example.xlsx contains the data (kindly go through the link to see the data)

What I want to ask if I want to extract / fetch only the data from let say, the column of "age" using Apache Tika in Java, is there any way of doing it?

YasserKaddour
  • 880
  • 11
  • 23

1 Answers1

1

I believe this will answer all you questions http://poi.apache.org/spreadsheet/quick-guide.html#ReadWriteWorkbook

Jithesh Chandra
  • 860
  • 7
  • 12
  • please read the [FAQ] its recommended you inline some of the main points and code from linked article and leave link in answer too. – tgkprog Aug 06 '16 at 19:53