简体   繁体   English

导入具有接口和类的包

[英]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. 我正在尝试创建一个Java包以与Excel交互,因此我正在使用org.apache.poi库。 I've found the documentation on the POI site that shows the following: 我在POI网站上找到了显示以下内容的文档:

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. 尝试创建Workbook对象时,出现错误,提示它不知道类型。 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语句,但是,如果我正确阅读了这些POI文档 ,则应该能够导入org.apache.poi.ss.usermodel.Workbook ,(而且很可能我错了(因为我是Java的新手),但是尝试以下所有import语句都会导致Workbook类型的错误。

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. 我应该注意,我为此使用Eclipse Neon,并且编辑器没有抱怨它找不到软件包,所以我不确定问题出在哪里。

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. 我在编辑器中遇到的实际错误是"Workbook cannot be resolved to a type" ,而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. 因此,TL; DR就是POI文档显示了Workbook是org.apache.poi.ss.usermodel的接口,而WorkbookFactory是同一usermodel包中的类,但是我不知道如何导入/使用他们正确。

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: 我之前发布的消息是编辑器弹出窗口...我可以截屏:

在此处输入图片说明

EDIT: Posting the full code, because, why not. 编辑:发布完整的代码,因为,为什么不呢。 (The multiple import statements were just trying things out, mostly in desperation.) (多个import语句只是在尝试,主要是在绝望中。)

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. 任何帮助表示赞赏。

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. 非常感谢您对此发表的评论,但是我一直讨厌Eclipse,因此决定改用NetBeans IDE。 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. 右键单击库,添加JAR / Folder,然后选择POI jar文件,这非常简单,它(和集成的Maven安装)负责细节,并且可以立即很好地工作并解决了所有库依赖性。

在此处输入图片说明

I believe this due to jar file corrupted ( most probably during download for the first time). 我相信这是由于jar文件损坏(最有可能是在首次下载过程中)。 Here what you can do. 在这里您可以做什么。

You can delete all poi folder in maven repository .m2/repository/org/apache/poi then you can update maven by Right Click on the project > Maven > Update Project. 您可以删除Maven存储库.m2/repository/org/apache/poi所有poi文件夹,然后可以通过右键单击项目> Maven>更新项目来更新maven。 Eclipse will automatically download the dependencies and auto rebuild the workspace. Eclipse将自动下载依赖关系并自动重建工作空间。

im facing the same thing like yours and manage to get it clean. 我正面临着像你一样的事情,并设法使它变得干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM