Java JExcel API 中的 Workbook.getWorkbook() 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21113767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:40:45  来源:igfitidea点击:

Workbook.getWorkbook() error in JExcel API

javaeclipseexcel

提问by Devang Sinha

I am using JExcel API for reading a .xls file in Eclipse. Here is my code:

我正在使用 JExcel API 在 Eclipse 中读取 .xls 文件。这是我的代码:

import jxl.*;
import java.io.File;

public class JExcelDemoClass 
{
    public static void main(String[] args)
    {
        try
        {
            Workbook wrk1=new Workbook.getWorkbook("C:\Test.xls");
            Sheet sheet1=wrk1.getSheet(0);
            Cell col1row1=sheet1.getCell(0,0);
            Cell col1row2=sheet1.getCell(0,1);
            Cell col1row3=sheet1.getCell(0,2);
            String get_col1row1=col1row1.getContents();
            String get_col1row2=col1row2.getContents();
            String get_col1row3=col1row3.getContents();
            System.out.println(get_col1row1);
            System.out.println(get_col1row2);
            System.out.println(get_col1row3);
        }
        catch(Exception e)
        {
            System.out.println("Error");
        }

    }
}

When i try to run this code, Eclipse gives me error

当我尝试运行此代码时,Eclipse 出现错误

Workbook.getWorkbook cannot be resolved to a type
Any idea why I am getting this error ?

Workbook.getWorkbook 无法解析为类型
知道为什么我会收到此错误吗?

采纳答案by greg-449

The syntax of this line is wrong:

这一行的语法是错误的:

Workbook wrk1=new Workbook.getWorkbook("C:\Test.xls");

you are trying to use newon a static method of the Workbookclass. The correct syntax is:

您正在尝试newWorkbook类的静态方法上使用。正确的语法是:

Workbook wrk1 = Workbook.getWorkbook("C:\Test.xls");

(no newkeyword)

(无new关键字)

回答by Pureferret

There are two ways of creating workbooks in JExcel. Both require Fileobjects.

在 JExcel 中有两种创建工作簿的方法。两者都需要File对象。

A non-writable workbook:

不可写的工作簿:

Workbook workbook = Workbook.getWorkbook(new File("D:\file2.xls"));

or by creating a writable workbook with

或者通过创建一个可写的工作簿

WritableWorkbook writableWorkbook = Workbook.createWorkbook(new File("D:\file1.xls"));

Workbook itself is an abstract class that allows different method with different return types (WritableWorkbooks and Workbooks).

Workbook 本身是一个抽象类,它允许具有不同返回类型(WritableWorkbooks 和 Workbooks)的不同方法。

getWorkbookand createWorkbookare use the Factory Pattern

getWorkbook并且createWorkbook使用工厂模式

Check out the Java Docs for more information on JExcel

查看 Java 文档以获取有关 JExcel 的更多信息