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
Workbook.getWorkbook() error in JExcel API
提问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 new
on a static method of the Workbook
class. The correct syntax is:
您正在尝试new
在Workbook
类的静态方法上使用。正确的语法是:
Workbook wrk1 = Workbook.getWorkbook("C:\Test.xls");
(no new
keyword)
(无new
关键字)
回答by Pureferret
There are two ways of creating workbooks in JExcel. Both require File
objects.
在 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)的不同方法。
getWorkbook
and createWorkbook
are use the Factory Pattern
getWorkbook
并且createWorkbook
使用工厂模式
Check out the Java Docs for more information on JExcel
查看 Java 文档以获取有关 JExcel 的更多信息