原创

Android Java JXL 在 2-3 次保存 excel 文件后被损坏

温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I'm working on a little project where I want to save things in Excel on Android phone but after a few saves the file gets corrupted and can't be opened. Any idea what is the problem? I don't really know what's the problem or what am I missing. Thanks for any help

public static void appendData(Context context, String date, String string1, int intValue, String string2, String car, int ID) {
        try {
            File directory = context.getFilesDir();
            File file = new File(directory, "Mentés.xls");

            if (!file.exists()) {
                // Create a new workbook if the file doesn't exist
                WritableWorkbook workbook = Workbook.createWorkbook(file);

                WritableSheet sheet = workbook.createSheet(car, 0);

                sheet.addCell(new Label(0, 0, "Dátum"));
                sheet.addCell(new Label(1, 0, "Szervíz"));
                sheet.addCell(new Label(2, 0, "Km óra állás"));
                sheet.addCell(new Label(3, 0, "Megjegyzés"));

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

            }
            Workbook existingWorkbook = Workbook.getWorkbook(file);

            WritableWorkbook workbookCopy = Workbook.createWorkbook(file, existingWorkbook);

            WritableSheet sheet = workbookCopy.getSheet(ID); // Assume we are appending to the first sheet

            int nextRow = sheet.getRows();

            Label labelDate = new Label(0, nextRow, date);
            Label labelString1 = new Label(1, nextRow, string1);
            Label labelInt = new Label(2, nextRow, String.valueOf(intValue));
            Label labelString2 = new Label(3, nextRow, string2);

            sheet.addCell(labelDate);
            sheet.addCell(labelString1);
            sheet.addCell(labelInt);
            sheet.addCell(labelString2);

            workbookCopy.write();
            workbookCopy.close();

            existingWorkbook.close();

            System.out.println("Data appended successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    } 
正文到此结束
热门推荐
本文目录