Tuesday, 16 February 2016

How to write data to a excel using JExcel API

How to write data to a excel using JExcel API:
===================================
JExcel is free API to work with Excel files;
Basically api is an collection of predefined classes and interfaces...
Following are the regularly used classes and interfaces to read data
Workbook (Class) – Will handle workbook.
Sheet (Interface) – Which will handle sheets
Cell (Interface) – Which will handle cell

Download JExcel API:
-------------------------------
OPEN GOOGLE-->DOWNLOAD JXCEL API

OR

http://mirrors.ibiblio.org/pub/mirrors/maven/net.sourceforge.jexcelapi/jars/jxl-2.6.jar

AFTER DOWNLOADING ADD THAT JXCEL JAR FILE IN TO YOUR PROJECT.


//Sample program on how to write data from jexcel
-------------------------------------------------------------------


How to write:
==============
import java.io.IOException;
import java.util.Date;

import jxl.*;
import jxl.write.*;
import jxl.write.Boolean;
import jxl.write.Number;
import java.io.File;

import org.testng.annotations.Test;

public class JexcelWriteData {

@Test
public void writeData() {

try {
           File exlFile = new File("c:/ramesh/testdata.xls");
           WritableWorkbook writableWorkbook = Workbook
                   .createWorkbook(exlFile);
 
           WritableSheet writableSheet = writableWorkbook.createSheet(
                   "Sheet1", 0);
           Label label = new Label(0, 0, "Label (String)");
           DateTime date = new DateTime(1, 0, new Date());
           Boolean bool = new Boolean(2, 0, true);
           Number num = new Number(3, 0, 9.99);
 
           //Add the created Cells to the sheet
           writableSheet.addCell(label);
           writableSheet.addCell(date);
           writableSheet.addCell(bool);
           writableSheet.addCell(num);
 
           //Write and close the workbook
           writableWorkbook.write();
           writableWorkbook.close();
 
       } catch (IOException e) {
           e.printStackTrace();
       } catch (RowsExceededException e) {
           e.printStackTrace();
       } catch (WriteException e) {
           e.printStackTrace();
       }
   }
}
}
Note:For APACHE API visit our posts in blog

No comments:

Post a Comment