Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache POI -- inserting data into specific column/rows and sheets EXCEL w/ Java

Tags:

java

excel

apache

I have a document in excel that I want to edit or input a data on it using java..I have try it but all I can do is just delete the data in my excel and replace it the data which I just input.I'm using Netbeans for my compiler. My output is GUI/JFrame how to Insert data into a specific column, row and sheet?? example I want to insert it in C12, E11, Sheet1 etc.

Thank you...

I'm doing self study only in Java Coding that's why I don't know much..

like image 516
davex4k9 Avatar asked Feb 24 '14 01:02

davex4k9


People also ask

How do I read a specific column in Excel using Java POI?

getLastRowNum(); rowIndex++) { row = sheet. getRow(rowIndex); if (row != null) { Cell cell = row. getCell(colIndex); if (cell !=


2 Answers

This is just example for xls not xlsx. You can refer here. if you want to edit/input data in xlsx (you can using XSSFWorkbook and XSSFSheet).

    try {
        //Get the excel file.
        FileInputStream file = new FileInputStream(new File("(which sheet you want to modify or edit(put the path here)"));

        //Get workbook for XLS file.
        HSSFWorkbook yourworkbook = new HSSFWorkbook(file);

        //Get first sheet from the workbook.
        //If there have >1 sheet in your workbook, you can change it here IF you want to edit other sheets.
        HSSFSheet sheet1 = yourworkbook.getSheetAt(0);

        // Get the row of your desired cell.
        // Let's say that your desired cell is at row 2.
        Row row = sheet1.getRow(1);
        // Get the column of your desired cell in your selected row.
        // Let's say that your desired cell is at column 2.
        Cell column = row.getCell(1);
        // If the cell is String type.If double or else you can change it.
        String updatename = column.getStringCellValue();
        //New content for desired cell.
        updatename="Lala";
        //Print out the updated content.
        System.out.println(updatename);
        //Set the new content to your desired cell(column).
        column.setCellValue(updatename); 
        //Close the excel file.
        file.close();
        //Where you want to save the updated sheet.
        FileOutputStream out = 
            new FileOutputStream(new File("(where you want to save?.Put the path here)"));
        yourworkbook.write(out);
        out.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
like image 92
Nor Sakinah Abdullah Avatar answered Oct 13 '22 19:10

Nor Sakinah Abdullah


Here is snippet to help you:

public void write() throws IOException, WriteException {
    File file = new File("file_location");
    WorkbookSettings wbSettings = new WorkbookSettings();
    wbSettings.setLocale(new Locale("en", "EN"));
    WritableWorkbook wb= Workbook.createWorkbook(file, wbSettings);
    wb.createSheet("My Spreadsheet", 0);
    WritableSheet excel = wb.getSheet(0);
    createLabel(excel);
    createContent(excel);
    wb.write();
    wb.close();
}
like image 37
CKuharski Avatar answered Oct 13 '22 19:10

CKuharski