我正在尝试编写代码,根据我存储的数组对象的数量创建多个工作表。目前正在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 ");
}
这是stacktrace
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)
请发布代码回复,因为我一直在努力解决这个问题。
答案 0 :(得分:2)
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的值。它还说明了:WritableSheet excelSheet = workbook.getSheet(sheetCounter);
函数的write()
行。
之前的两行是:
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet(finalGui.ExperimentCodeList.get(sheetCounter).toString(), sheetCounter);
它说:为write()
的每次调用创建一个新工作簿和一个新工作表。因此,createSheet(String name, int index)
文档说:
创建并返回具有指定名称的指定位置的工作表如果指定的索引小于或等于零,则在工作簿的开头创建新工作表。如果索引大于工作表数,则在工作簿的末尾创建工作表。
此处的结尾将始终位于索引0,因为工作簿始终是全新的。在下一条指令中,当您尝试访问索引30时,它会引发异常,因为您可以获得的唯一一个是索引0。
这是逻辑(我会让你编写代码)。
主要变化是:
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 ");
}
}