Search code examples
javaeclipseapache-poieclipse-neon

Importing a package with an interface and a class


I am trying to create a Java package to interface with Excel, so I am using the org.apache.poi libraries. I've found the documentation on the POI site that shows the following:

Reading and Rewriting Workbooks

(truncated)
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);

When I attempt to create a Workbook object, I get errors that it doesn't know the type. The documentation above doesn't show the import statements, but if I'm reading these POI docs correctly, I should be able to import org.apache.poi.ss.usermodel.Workbook, (and it's distinctly possible that I'm wrong because I'm very new to Java) but trying all of the following import statements all result in errors for the Workbook type.

import org.apache.poi.*;
import org.apache.poi.ss.*;
import org.apache.poi.ss.usermodel.*;

I should note that I'm using Eclipse Neon for this, and the editor is not complaining that it can't find the packages, so I'm not sure where the issue lies.

Workbook wb = WorkbookFactory.create(inp);

The actual error I'm getting in the editor is that "Workbook cannot be resolved to a type", and the same error for WorkbookFactory.

So the TL;DR is that the POI docs show that Workbook is an Interface in org.apache.poi.ss.usermodel and that WorkbookFactory is a class in the same usermodel package, but I can't figure out how to import/use them correctly.

EDIT: The build time error I receive is

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Workbook cannot be resolved to a type
    WorkbookFactory cannot be resolved
    at spreadsheet.conversion.ConvertXLSXtoCSV.main(ConvertXLSXtoCSV.java:31)

The message I posted before was the editor popup... I was able to take a screenshot:

enter image description here

EDIT: Posting the full code, because, why not. (The multiple import statements were just trying things out, mostly in desperation.)

package spreadsheet.conversion; 
import java.text.ParseException;
import org.apache.poi.*;
import org.apache.poi.ss.*;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.InputStream;
public class ConvertXLSXtoCSV {

    private static String inputfilename = "";
    private static String outputfilename = "";
    private static int debug = 0;


    private static void test(String arg) {
        System.out.println(arg);
    }


    public static void main(String[] args) throws ParseException, Exception {
        if (args.length != 1) {
            throw new Exception("Requires 1 parameter. Usage: ConvertXLSXtoCSV <filename>");
        }
        inputfilename = args[0];

        if(debug==1) {
            test(inputfilename);
        }
        InputStream inp = new FileInputStream(inputfilename);

        Workbook wb = WorkbookFactory.create(inp);


    }

Any help is appreciated.


Solution

  • As unhelpful to others as this may be, it's the solution I've come up with so I'm posting it as an answer. Many thanks for the comments about this, but I've always hated Eclipse, and decided to go with NetBeans IDE instead. It was extremely trivial to right click on Libraries, Add JAR/Folder, and select the POI jar files, and it (and the integrated Maven install) took care of the details and it worked perfectly right off the bat and resolved all library dependencies.

    enter image description here