Update or edit existing Excel files in Java using Apache POI

Requirement -Programatically update an Excel spreadsheet in Java.

Solutions – Here an example of Apache POI to update a XLS workbook. Apache POI is a very good API , that can be handy in manipulating Excel documents.This tutorial focuses on XLS documents (Office 97 – 2003).

The existing excel file like below –

sdsdsdsdsdsdsd

Below is code to update the excel file

import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.*;
import java.util.Iterator;
public class updateExcels {  

        public static void main(String[] args) throws Exception{
                
                FileInputStream fsIP= new FileInputStream(new File("C:\\TechartifactExcel.xls")); //Read the spreadsheet that needs to be updated
                 
                HSSFWorkbook wb = new HSSFWorkbook(fsIP); //Access the workbook
                 
                HSSFSheet worksheet = wb.getSheetAt(0); //Access the worksheet, so that we can update / modify it.
                 
                Cell cell = null; // declare a Cell object
               
                cell = worksheet.getRow(2).getCell(2);   // Access the second cell in second row to update the value
                 
                cell.setCellValue("OverRide Last Name");  // Get current cell value value and overwrite the value
                 
                fsIP.close(); //Close the InputStream
                
                FileOutputStream output_file =new FileOutputStream(new File("C:\\TechartifactExcel.xls"));  //Open FileOutputStream to write updates
                 
                wb.write(output_file); //write changes
                 
                output_file.close();  //close the stream    
        }
}

after updating excel file would look like below –

1updateexcel2

happy coding with Vinay in techartifact ….

Increase size of your jdeveloper

Jdeveloper tips- when you get out-of-memory crashing Oracle JDeveloper .You can quickly increase size of it like below –

Memory settings are located in: %JDEV_HOME%\jdeveloper\ide\bin\ide.conf
Default is apparantly 640MB and can be found at the line:

AddVMOption -Xmx640M

increase to 1024 for instance:

AddVMOption -Xmx1024M

Sorting of Programmatic or transient view object in Oracle ADF

Tip- to sort a transient view object

When we use transient View Object to store data to represent in UI or manipulate business logic.
We may need to sort these Transient ViewObjects based on certain attribute.Add this below code to represent sort data.

transVo.setSortBy("AttributeName");
transVo.setQueryMode(ViewObject.QUERY_MODE_SCAN_VIEW_ROWS);
transVo.executeQuery();

Happy coding with Vinay in Techartifact …..