如何使用Jexcel API编辑/修改Java中的现有Excel文件 [英] How to Edit/Modify an existing Excel file in Java with Jexcel API

查看:312
本文介绍了如何使用Jexcel API编辑/修改Java中的现有Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Java编辑一个现有的Excel文件,以便向现有的模板excel文件中添加更多数据.所以我为此目的使用了Jexcel.

I want to edit an existing Excel file with Java, to add some more data to an existing template excel file. So i used Jexcel for this purpose.

根据各地的建议,我尝试了以下方法,

As suggested everywhere, I tried the following,

Workbook existingWorkbook = Workbook.getWorkbook(new File("H://"+file_name));
WritableWorkbook copy = Workbook.createWorkbook(new File("H://"+file_name+"_temp1.xls"));

但是第二行显示了一个例外情况.

But it shows an exception in the second line.

jxl.common.AssertionFailed
    at jxl.common.Assert.verify(Assert.java:37)
    at jxl.read.biff.SheetReader.handleObjectRecord(SheetReader.java:1811)
    at jxl.read.biff.SheetReader.read(SheetReader.java:1059)
    at jxl.read.biff.SheetImpl.readSheet(SheetImpl.java:716)
    at jxl.read.biff.WorkbookParser.getSheet(WorkbookParser.java:257)
    at jxl.write.biff.WritableWorkbookImpl.copyWorkbook(WritableWorkbookImpl.java:969)
    at jxl.write.biff.WritableWorkbookImpl.<init>(WritableWorkbookImpl.java:343)
    at jxl.Workbook.createWorkbook(Workbook.java:339)
    at jxl.Workbook.createWorkbook(Workbook.java:320)
    at run_book.process_input.<init>(process_input.java:83)        <--create workbook stt.
    .........<stack trace goes on>

那么如何编辑一个已经存在的jexcel文件. 我确实收到了另一个警告

So how could one edit an already existing jexcel file. I did get another warning

警告:不支持工作表"sheet2"上的文本对象-省略

Warning: Text Object on sheet "sheet2" not supported - omitting

先谢谢您了:)

推荐答案

找出了问题所在.

我们必须先关闭输入文件,然后再写回(编辑)同一文件.

We have to close the input file before writing back (editing) the same file.

因此可以使用Jexcel编辑现有的Excel文件

so to edit an existing Excel file with Jexcel

File inp = new File("H://"+file_name);
File out = new File("H://"+file_name);
Workbook existingWorkbook = Workbook.getWorkbook(inp);// This opens up a read-only copy of the workbook
WritableWorkbook copy = Workbook.createWorkbook(out,existingWorkbook); // This opens up a writable workbook so that we can edit the copy
//..........Some writes to excel workbook...........
// Now before writing & closing the copy, first close the existing one
existingWorkbook.close();    // Important: Close it before writing the copy with copy.write();
inp.close();
copy.write();
copy.close();

这篇关于如何使用Jexcel API编辑/修改Java中的现有Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆