jxl中的WritablewWorkbook.write()生成excel文件,该文件为空0b

时间:2013-03-22 15:28:59

标签: java jxl

我在这里做错了什么? JTextField的值有效,代码用于将结果打印到控制台。我在工作区中获取了一个文件,但其大小= 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)

非常感谢。

1 个答案:

答案 0 :(得分:0)

试试这个......

              CellFormat readFormat = c.getCellFormat();
                System.out.println(c.getContents());
                String ifContext = c.getContents().toString();
                Label labIfContext = new Label(1, i + 1, ifContext);
                labIfContext.setCellFormat(readFormat);
                genSheet.addCell(labIfContext);
相关问题