编程语言
首页 > 编程语言> > java – 在Excel文件中附加数据

java – 在Excel文件中附加数据

作者:互联网

我正在尝试编写一个程序,将数据附加到Java中的Excel文件中.我达到了以下代码.但它会重写Excel文件中的内容,而不是附加到它.请帮我完成这个.

public class jExcel
{
    static WritableWorkbook workbook;
    public static void main(String args[])throws Exception
    {
        workbook = Workbook.createWorkbook((new File("D:\\0077\\my2.xls")));
        WritableSheet sheet = workbook.createSheet("First Sheett",1);
        Label label = new Label(5,2,"ssssssssss");
        sheet.addCell(label);
        workbook.write();
        workbook.close();
    }
}

解决方法:

//写入XLS

    WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\output.xls"));
    WritableSheet sheet = workbook.createSheet("First Sheet", 0);
    Label label = new Label(0, 2, "A label record"); 
    sheet.addCell(label);
    workbook.write(); 
    workbook.close();

    //MODIFY XLS

    Workbook workbook1 = Workbook.getWorkbook(new File("D:\\output.xls"));
    WritableWorkbook copy = Workbook.createWorkbook(new File("D:\\output.xls"), workbook1);
    WritableSheet sheet2 = copy.getSheet(0); 
    //WritableCell cell = sheet2.getWritableCell(5, 2); 

    copy.write();
    copy.close();

标签:jexcelapi,java,excel
来源: https://codeday.me/bug/20190927/1824567.html