如何使用 jxl.jar 和 while 循环在 excel 中创建几个不同的工作表?
How do I create several different sheets in excel using jxl.jar and a while loop?
我正在尝试编写代码,根据我存储的数组对象的数量创建多个工作表。目前在 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.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.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
请post一个代码响应,因为我已经为这个问题苦苦挣扎了一段时间。
原因。
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);
它说:创建一个新的工作簿和一个新的sheet里面,每次调用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 的 sheet 时,它会引发异常,因为您唯一可以获得的是索引 0.
如何解决?
这是逻辑(我让你写代码)。
- 创建 pour xls 文件,创建一个空工作簿。 (初始化阶段)
- 在您的工作簿上循环创建 sheets,就像您所做的那样。 (你真正想做什么)
- 关闭您需要关闭的所有内容 - 至少是工作簿。 (保持一切干净整洁)
一点代码
主要变化是:
- 构造函数的使用
- 循环进入
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 ");
}
}
我正在尝试编写代码,根据我存储的数组对象的数量创建多个工作表。目前在 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.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.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
请post一个代码响应,因为我已经为这个问题苦苦挣扎了一段时间。
原因。
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);
它说:创建一个新的工作簿和一个新的sheet里面,每次调用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 的 sheet 时,它会引发异常,因为您唯一可以获得的是索引 0.
如何解决?
这是逻辑(我让你写代码)。
- 创建 pour xls 文件,创建一个空工作簿。 (初始化阶段)
- 在您的工作簿上循环创建 sheets,就像您所做的那样。 (你真正想做什么)
- 关闭您需要关闭的所有内容 - 至少是工作簿。 (保持一切干净整洁)
一点代码
主要变化是:
- 构造函数的使用
- 循环进入
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 ");
}
}