http://www.technicalpage.net/search/label/SQL

>> Write to an Excel file using Apache POI

Write to an Excel file using Apache POI

       
public static void main(String[] args) throws Exception {
               
        String fileName = "Path of Excel file\\WriteExcel.xls";
        String sheetName= "FirstSheet";
               
        //for below codes, you will need jars from Apache POI.
        //download latest release of Apache POI which will be available in zip file , from https://poi.apache.org
        //unzip the folder
        //add the jar files inside poi folder and lib folder(lib folder is inside poi folder) and other jar files if present in other folder.
         
        //If the excel file is .xls, use below statement.                              
        HSSFWorkbook workbook = new HSSFWorkbook(); //import "org.apache.poi.hssf.usermodel.HSSFWorkbook" at this point.
        HSSFSheet excel_Sheet = workbook.createSheet(sheetName) ; //import "org.apache.poi.hssf.usermodel.HSSFSheet" at this point.
               
        //If the excel file is .xlsx, use below statement.
        //XSSFWorkbook wb = new XSSFWorkbook();//import "org.apache.poi.xssf.usermodel.XSSFWorkbook" at this point.
        //XSSFSheet excel_Sheet = wb.createSheet(sheetName) ;//import "org.apache.poi.xssf.usermodel.XSSFSheet" at this point.
                               
        //Enter a value to a cell(2,3)
        HSSFRow row = excel_Sheet.createRow(2); //import "org.apache.poi.hssf.usermodel.HSSFRow" at this point.
        HSSFCell cell = row.createCell(3); //import "org.apache.poi.hssf.usermodel.HSSFCell" at this point.
        cell.setCellValue("Cell23");
                              
        FileOutputStream fos = new FileOutputStream(fileName);//import "java.io.FileOutputStream" at this point.
        workbook.write(fos);
        fos.close();               

        }

Write to an Excel File - writing to a selected cell.



Write a set of data to Excel file:
       public static void main(String[] args) throws Exception {
               
        String fileName = "C:\\Users\\MunnaBhai.MunnaBhai-PC\\Desktop\\Selenium\\WriteToExcel.xls";
        String sheetName= "DataSheet";

        //If the excel file is .xls, use below statement.                              
        HSSFWorkbook workbook = new HSSFWorkbook(); //import "org.apache.poi.hssf.usermodel.HSSFWorkbook" at this point.
        HSSFSheet excel_Sheet = workbook.createSheet(sheetName) ; //import "org.apache.poi.hssf.usermodel.HSSFSheet" at this point.
               
        //If the excel file is .xlsx, use below statement.
        //XSSFWorkbook wb = new XSSFWorkbook();//import "org.apache.poi.xssf.usermodel.XSSFWorkbook" at this point.
        //XSSFSheet excel_Sheet = wb.createSheet(sheetName) ;//import "org.apache.poi.xssf.usermodel.XSSFSheet" at this point.
               
        //Enter set of data from cell(0,0) to  cell(4,6)
        for (int r=0; r<=4;r++) {
                HSSFRow row = excel_Sheet.createRow(r);
                for(int c=0; c<=6;c++) {
                        HSSFCell cell = row.createCell(c);
                        cell.setCellValue("Cell"+r+c);
                }
        }
       
        FileOutputStream fos = new FileOutputStream(fileName);//import "java.io.FileOutputStream" at this point.
        workbook.write(fos);
        fos.close();
               
        }
Writing to an Excel File - write a set of data .

No comments:

Post a Comment