0

我正在玩jexcel 库

我试图编写一个小程序,它执行以下操作:

  1. 读取xls文件
  2. 在工作表中进行一些计算并将其写入另一个地方

公共类数据处理器 {

    private String inputFile;
    private String outputFile;



private Sheet sheet;
    private Workbook w;

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

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

    public void read() throws IOException {
        File inputWorkbook = new File(inputFile);
        Workbook w;
        try {
            w = Workbook.getWorkbook(inputWorkbook);
            sheet = w.getSheet(0);
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("deprecation")
    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("Lolonator", 0);
        workbook.createSheet("Lolonator123", 1);
        workbook.copy(w);

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

    public static void main(String[] args) throws IOException, WriteException {
        ReadExcel test = new ReadExcel();
        test.setInputFile("C:/Users/Desktop/sheet1.xls");
        test.read();
        System.out.println("####################################################");
        System.out.println("File read!");

//      Write
        System.out.println("####################################################");
        System.out.println("Start to write the file!");
        WriteExcel out = new WriteExcel();
        out.setOutputFile("C:/Users/Desktop/sheet2.xls");
        out.write();
        System.out.println("Please check the result file!");

    }

}

但是,这不起作用。即使我的程序无一例外地运行到最后,我的工作表中也没有任何输出。真的很感谢你的回答!!!

4

1 回答 1

1

在您的 write 函数中,您使用“inputFile”作为 File 构造函数的参数,但在创建 out 对象后并未对其进行初始化。

所以写函数中的以下行

File file = new File(inputFile);

应该

File file = new File(outputFile);

您还确定在运行此代码后没有看到任何错误。它应该抛出一个空指针异常。

希望这可以帮助...

于 2013-10-13T12:52:00.910 回答