Quote

"Between stimulus and response there is a space. In that space is our power to choose our response.
In our response lies our growth and freedom"


“The only way to discover the limits of the possible is to go beyond them into the impossible.”


Thursday, 6 February 2014

Using JXL to Write and Read Microsoft Excel Data




You can read, write, and modify Excel spreadsheets dynamically using Java Excel API. It is an open source java API enabling developers to read and modify them. The following program demonstrates the use of JXL api for writing and reading excel files.

import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.Number;


public class ExcelOperations {

     
      public static void main (String[] args) throws WriteException, IOException, BiffException{
           
            excelWriter();
            excelReader();
      }
/* The method  excelWriter creates a new formatted excel sheet named 'satyam.xls.'*/


      private static void excelWriter() throws IOException, WriteException  {
            // TODO Auto-generated method stub
            WritableWorkbook wworkbook;
//Create workbook
          
wworkbook = Workbook.createWorkbook(new File("satyam.xls"));

//Create sheet in workbook         

 WritableSheet wsheet = wworkbook.createSheet("RecordSheet", 0);

//Create a font style for cell text
          WritableFont myFont = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, true,UnderlineStyle.DOUBLE, Colour.WHITE);
           
//Create WritableCellFormat with the created font style

WritableCellFormat cf = new WritableCellFormat(myFont);

//Change the background color of the cell
          
 cf.setBackground(Colour.RED);
         
 cf.setShrinkToFit(true);
         
 Label header0 = new Label(1, 3, "SNo");
         
 //Write to excel sheet cell
 wsheet.addCell(header0);
         
 Label header1 = new Label(2, 3, "Header1");

//Format the contents of the cell

 header1.setCellFormat(cf);

 //Write to excel sheet cell
 wsheet.addCell(header1);
         
 Label header2 = new Label(3, 3, "Header2");
 

 //Write to excel sheet cell

 wsheet.addCell(header2);
         
 Number number1 = new Number(2, 4, 28.1981);
 

 //Write to excel sheet cell
 wsheet.addCell(number1);
         
 Number number2 = new Number(3, 4, 13.1983);

 //Write to excel sheet cell 
 wsheet.addCell(number2);
         
 wworkbook.write();
         
 wworkbook.close();
           
      }


/* The method  excelReader reads an existing excel sheet named 'satyam.xls.'*/
       
private static void excelReader() throws BiffException, IOException {
                       
//Get the workbook to read           
 Workbook workbook = Workbook.getWorkbook(new File("satyam.xls"));

//Get the sheet to read          
Sheet sheet = workbook.getSheet(0);
           
//Read the excel sheet cell
Cell cell1 = sheet.getCell(2, 3);
           
System.out.println(cell1.getContents());
           
//Read the excel sheet cell
Cell cell2 = sheet.getCell(3, 3);

System.out.println(cell2.getContents());

           
//Read the excel sheet cell
Cell cell3 = sheet.getCell(2, 4);
        
System.out.println(cell3.getContents());
           
//Read the excel sheet cell
Cell cell4 = sheet.getCell(3, 4);
           
System.out.println(cell4.getContents());

           
workbook.close();
           
      }

}

No comments:

Post a Comment