1

I am trying to append the data in existing excel file, but nothing is written. What am I doing wrong?

    public static void writeSheet(String data) {
    HSSFWorkbook workbook;

    try {
        String outFileName = "filebook.xls";

        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        File outFile = new File(path, outFileName);
        workbook = (HSSFWorkbook) WorkbookFactory.create(outFile);

        Row row = workbook.getSheetAt(0).createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("text");

        OutputStream outputStream = new FileOutputStream(outFile,true);
        workbook.write(outputStream);

        outputStream.close();
        workbook.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
1
  • are uou creating a new file or updating inside existing file ! Commented May 13, 2016 at 8:57

2 Answers 2

3

Instead

File outFile = new File(path, outFileName);
workbook = (HSSFWorkbook) WorkbookFactory.create(outFile);

Try with

FileInputStream outFile = new FileInputStream(new File(path));
workbook = new HSSFWorkbook(outFile);

So your code

public static void writeSheet(String data) {
HSSFWorkbook workbook;

try {
    String outFileName = "filebook.xls";

    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

    FileInputStream file = new FileInputStream(new File(path, outFileName))
    workbook = new HSSFWorkbook(file);

    Row row = workbook.getSheetAt(0).createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("text");

    file.close();

    FileOutputStream outFile =new FileOutputStream(new File(path, outFileName));
    workbook.write(outFile);
    outFile.close();

} catch (Exception e) {
    e.printStackTrace();
}

}

Sign up to request clarification or add additional context in comments.

Comments

0

If you are looking to create Excell file using java

File file = new File(filename);
        WorkbookSettings wbSettings = new WorkbookSettings();
        wbSettings.setLocale(new Locale("en", "EN"));
        WritableWorkbook workbook = Workbook.createWorkbook(file,
                wbSettings);

        workbook.createSheet("My_excell_file", 0);
        WritableSheet excelSheet = workbook.getSheet(0);
         WritableFont ledgerFont = new WritableFont(
        WritableFont.createFont("Arial"),
        WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false,
        UnderlineStyle.NO_UNDERLINE, Colour.GRAY_80);
        Label labelhead1;
        Label labelhead2;
        Label labelhead3;
        private WritableCellFormat headFormat, langFormat;
        labelhead1 = new Label(0, 1, "KEY", this.headFormat);
        labelhead2 = new Label(1, 1, "VALUE", this.headFormat);
        labelhead3 = new Label(2, 1, "PROPERTY", this.headFormat);
        excelSheet.addCell(labelhead1);
        excelSheet.addCell(labelhead2);
        excelSheet.addCell(labelhead3);
        workbook.write();
        workbook.close();
        //           For Update
        HSSFRow row;
        row = sheet.getRow(0);
        cell = row.getCell(0);
        cell.setCellValue("Updated Values");

2 Comments

Thank you for your code, but I am looking the information how to add the data to existing file ( I have the file and I have the data in it), without creating a new file and removing the existing data
@Delphian see updated answer to update your excell file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.