Excel file handling using java

I have been working on writing text files with Java. But now I want to know how to read and modify Excel files. The reading part seems to be quite easy with JExcel API but I am stuck on modifying an Excel file. Any other methods are also welcome!!

3 Answers

  1. Mike- Reply

    2019-11-14

    Sample code for Writing excel using Jexcel is

    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("Report", 0);
        WritableSheet excelSheet = workbook.getSheet(0);
        createLabel(excelSheet);
        createContent(excelSheet);
    
        workbook.write();
        workbook.close();
    }
    

    For complete code for both reading and writing excel see here. Its very startup example for your requirement. And hope many example exists on this. Please do google on it further... Happy coding... :)

    Updated:

    For getting existing excel sheet replace three lines of new worksheet with below code: (Leaving above code as is, for information.)

    Workbook existingWorkbook = Workbook.getWorkbook(new File(fileToEdit.getAbsolutePath()));
    WritableWorkbook workbookCopy = Workbook.createWorkbook(new File("output.xls"), existingWorkbook);
    WritableSheet sheetToEdit = workbookCopy.getSheet(sheetName);
    

    Please note it, this code is for reference, not compiled and tested. Provided as you asked for more information.... :)

  2. Neil- Reply

    2019-11-14

    JExcel is designed for increased read efficiency (since this is the primary use of the API). In order to improve performance, data which relates to output information (eg. all the formatting information such as fonts) is not interpreted when the spreadsheet is read, since this is superfluous when interrogating the raw data values.

    However, if we need to modify this spreadsheet a handle to the various write interfaces is needed, which can be obtained using the copy method.

    The example is available here. Refer the same.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>