How to add Data Type Cells to Excel Spreadsheet
Java Excel API is an open source java library to read, write and modify Excel spread sheets.
This requires the library jxl-2.6.12.jar to be in classpath.
The following example shows how to add different Data Type Cells to Excel Spread sheet.
package com.bethecoder.tutorials.jexcelapi.write;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Boolean;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class AddDataTypeCellsTest {
/**
* @param args
* @throws IOException
* @throws IOException
* @throws WriteException
* @throws BiffException
*/
public static void main ( String [] args ) throws IOException, WriteException {
//Creates a writable workbook with the given file name
WritableWorkbook workbook = Workbook.createWorkbook ( new File ( "C:/JXL/AddTypesSheets.xls" )) ;
WritableSheet sheet = workbook.createSheet ( "My Sheet" , 0 ) ;
sheet.addCell ( new Label ( 0 , 0 , "ABC" )) ;
sheet.addCell ( new Label ( 1 , 0 , "DEF" )) ;
sheet.addCell ( new Boolean ( 0 , 1 , true )) ;
sheet.addCell ( new Boolean ( 1 , 1 , false )) ;
sheet.addCell ( new Number ( 0 , 2 , 1234.56 )) ;
sheet.addCell ( new Number ( 1 , 2 , - 9.87654 )) ;
sheet.addCell ( new DateTime ( 0 , 3 , new Date ())) ;
sheet.addCell ( new DateTime ( 1 , 3 , new Date ( System.currentTimeMillis () + 90000000 ))) ;
//Writes out the data held in this workbook in Excel format
workbook.write () ;
//Close and free allocated memory
workbook.close () ;
}
}
It gives the following output,