1
我在這裏做錯了什麼? JTextFields的值是有效的,代碼用於將結果打印到控制檯。我在我的工作區中獲得一個文件,但其大小= 0b並顯示一個空的電子表格。jxl中的WritablewWorkbook.write()會生成空的excel文件0b
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JTextField;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.CellFormat;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class convertFileAction implements ActionListener {
File _file;
JTextField _filePath;
JTextField _whichColumn;
String _path;
int _column;
public convertFileAction(JTextField tfFilePath, JTextField tfWhichColumn) {
_filePath = tfFilePath;
_whichColumn = tfWhichColumn;
}
@Override
public void actionPerformed(ActionEvent e) {
_path = _filePath.getText();
_file = new File(_path);
_column = Integer.parseInt(_whichColumn.getText());
_column = _column - 1;
try {
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setUseTemporaryFileDuringWrite(true);
Workbook workbook = Workbook.getWorkbook(_file);
// System.out.println(""+_file.exists());
Sheet sheet = workbook.getSheet(0);
WritableWorkbook genWorkbook = Workbook.createWorkbook(new File(
"output.xls"));
WritableSheet genSheet = genWorkbook.createSheet("Ad IDs", 0);
Label headerIc = new Label(1, 1, "ifContexts");
genSheet.addCell(headerIc);
for (int i = 0; sheet.getCell(_column, i) != null; i++) {
Cell c = sheet.getCell(_column, i);
CellFormat readFormat = c.getCellFormat();
WritableCellFormat writeFormat = new WritableCellFormat(
readFormat);
System.out.println(c.getContents());
String ifContext = c.getContents().toString();
Label labIfContext = new Label(1, i + 1, ifContext);
labIfContext.setCellFormat(writeFormat);
genSheet.addCell(labIfContext);
}
genWorkbook.write();
workbook.close();
genWorkbook.close();
} catch (BiffException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (RowsExceededException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (WriteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
我確實收到控制檯中的錯誤,但我不確定它是否有關。在這裏它是反正:
Exception in thread "AWT-EventQueue-1" jxl.common.AssertionFailed
at jxl.common.Assert.verify(Assert.java:37)
at jxl.biff.XFRecord.<init>(XFRecord.java:521)
at jxl.write.biff.CellXFRecord.<init>(CellXFRecord.java:69)
at jxl.write.WritableCellFormat.<init>(WritableCellFormat.java:96)
at convertFileAction.actionPerformed(convertFileAction.java:54)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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)
非常讚賞。