java - 如何使用 jxl.jar 和 while 循环在 Excel 中创建多个不同的工作表?

标签 java jxl

我正在尝试编写代码,根据我存储的数组对象的数量创建多个工作表。目前在 WritableSheet excelSheet = Work.getSheeet(sheetCounter) 处中断。给我一个 IndexOutOfBoundsException。它将运行一次,但在第二次运行时会中断。

public class DailyFile {
int i = 0; 
static int sheetCounter = 0;

private WritableCellFormat arialBold;
private WritableCellFormat arial;
private String inputFile;

public void setOutputFile(String inputFile) {
    this.inputFile = inputFile;
}

public void write() throws IOException, WriteException {
    File file = new File(inputFile);
    WorkbookSettings wbSettings = new WorkbookSettings();

    wbSettings.setLocale(new Locale("en", "EN"));

    WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
    workbook.createSheet(finalGui.ExperimentCodeList.get(sheetCounter).toString(), sheetCounter);
    WritableSheet excelSheet = workbook.getSheet(sheetCounter);
    createLabel(excelSheet);
    // createContent(excelSheet);

    workbook.write();
    workbook.close();
}

这是调用函数进行写入的地方。

public static void main(String[] args) throws WriteException, IOException {
    DailyFile test = new DailyFile();
    test.setOutputFile("c:/Users/sbutler/Desktop/CERES_Daily-BLIND_TEST.xls");
    while (sheetCounter <= finalGui.ExperimentCodeList.size()){
        System.out.println("I RAN!");
        test.write();
        sheetCounter++;
        System.out.println(sheetCounter);
    }
    System.out.println("Please check the result file under c:/Users/sbutler/Desktop/CERES_Daily-BLIND_TEST.xls ");
}

这是堆栈跟踪

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 30, Size: 1
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at jxl.write.biff.WritableWorkbookImpl.getSheet(WritableWorkbookImpl.java:408)
at gui.DailyFile.write(DailyFile.java:47)
at gui.DailyFile.main(DailyFile.java:293)
at gui.finalGui$4.mouseClicked(finalGui.java:127)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

请发布代码响应,因为我已经在这个问题上苦苦挣扎了一段时间。

最佳答案

原因。

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 30, Size: 1
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at jxl.write.biff.WritableWorkbookImpl.getSheet(WritableWorkbookImpl.java:408)

堆栈跟踪的前 4 行表示您正在尝试访问大小为 1 的数组中索引 30 的值。它还说明了位置:在 write() 函数的 WritableSheet excelSheet = workbook.getSheet(sheetCounter); 行。

在此之前的两行是:

WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet(finalGui.ExperimentCodeList.get(sheetCounter).toString(), sheetCounter);

它表示:每次调用 write() 时,都会在其中创建一个新工作簿和一个新工作表。因此,createSheet(String name, int index) 文档说:

Creates, and returns a worksheet at the specified position with the specified name If the index specified is less than or equal to zero, the new sheet is created at the beginning of the workbook. If the index is greater than the number of sheet, then the sheet is created at the end of the workbook.

此处的结尾始终位于索引 0,因为工作簿始终是全新的。在下一条指令中,当您尝试访问索引为 30 的工作表时,它会引发异常,因为您唯一可以获得的工作表位于索引 0 处。

如何修复它?

这是逻辑(我会让你编写代码)。

  1. 创建xls文件,创建一个空工作簿。 (初始化阶段)
  2. 按照您的方式循环使用工作簿来创建工作表。 (你真正想做的事情)
  3. 关闭所有需要关闭的内容 - 至少是工作簿。 (保持一切干净整洁)

一些代码

主要变化是:

  • 构造函数的使用
  • 循环移至write()
  • 新的 write() 参数(用于循环)
  • 使用workbook.createSheet的返回值

我无法编译它,但它应该看起来像这样。

public class DailyFile {
    int i = 0;

    private WritableCellFormat arialBold;
    private WritableCellFormat arial;
    private String inputFile;

    public DailyFile(String inputFile) {
        this.inputFile = inputFile;
    }

    public void write(int sheetCount) throws IOException, WriteException {
        // Step 1: Initialization
        File file = new File(inputFile);

        WorkbookSettings wbSettings = new WorkbookSettings();
        wbSettings.setLocale(new Locale("en", "EN"));

        WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);

        // Step 2: Main logic
        for (int index = 0; index < sheetCount; index++) {
            WritableSheet excelSheet = workbook.createSheet(finalGui.ExperimentCodeList.get(index).toString(), index);
            createLabel(excelSheet);
            // createContent(excelSheet);
        }

        // Step 3: Closing
        workbook.write(); // <-- maybe in the loop?
        workbook.close();
    }

    public static void main(String[] args) throws WriteException, IOException {
        DailyFile test = new DailyFile("c:/Users/sbutler/Desktop/CERES_Daily-BLIND_TEST.xls");
        System.out.println("I RAN!");
        test.write(finalGui.ExperimentCodeList.size());
        System.out.println(sheetCounter);
        System.out.println("Please check the result file under c:/Users/sbutler/Desktop/CERES_Daily-BLIND_TEST.xls ");
    }
}

关于java - 如何使用 jxl.jar 和 while 循环在 Excel 中创建多个不同的工作表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29540229/

相关文章:

java - IWizardPage 关闭下一页并打开完成

java - 如何将 unix 纪元微秒转换为 Java 时间戳

java - 打印出在字符串中找到字符的行号

java - 在 Java 中解析大的十六进制数

JXL 柱跨度

java - Excel 中 Java 中的 Selenium WebDriver 中的 2 个接口(interface)之间如何进行向下转型/向上转型

java - 非托管扩展 Neo4j 上的密码查询

java - 在服务器上创建 XLS 文件并返回给客户端而不用 java 在服务器上保存文件

java - 将 Excel 单元格内容附加到新的 Excel 文件

java - 与 Selenium 和 JXL 比较值