How to Fill a Form in 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 fill a form by copying an existing Excel Workbook template.
package com.bethecoder.tutorials.jexcelapi.modify;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
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;
public class FillAFormTest {
/**
* @param args
* @throws IOException
* @throws WriteException
* @throws BiffException
*/
public static void main ( String [] args ) throws IOException, WriteException, BiffException {
//Creates a writable workbook with the given file name
Workbook template = Workbook.getWorkbook ( new File ( "C:/JXL/Form-template.xls" )) ;
WritableWorkbook wbCopy = Workbook.createWorkbook (
new File ( "C:/JXL/Form-result.xls" ) , template ) ;
WritableSheet sheet = wbCopy.getSheet ( "Form" ) ;
String candidateName = "Venkata Sudhakar" ;
String interviewType = "Telephonic" ;
String interviewerName = "Sriram" ;
/**
* Create cell formats
*/
WritableFont cellFont = new WritableFont ( WritableFont.TAHOMA, 10 ) ;
WritableCellFormat labelFormat = new WritableCellFormat ( cellFont ) ;
labelFormat.setAlignment ( Alignment.LEFT ) ;
labelFormat.setVerticalAlignment ( VerticalAlignment.CENTRE ) ;
labelFormat.setBorder ( Border.ALL, BorderLineStyle.THIN ) ;
/**
* Add cells
*/
sheet.addCell ( new Label ( 1 , 1 , candidateName, labelFormat )) ;
sheet.addCell ( new Label ( 1 , 3 , interviewType, labelFormat )) ;
sheet.addCell ( new Label ( 1 , 5 , interviewerName, labelFormat )) ;
//Writes out the data held in this workbook in Excel format
wbCopy.write () ;
//Close and free allocated memory
wbCopy.close () ;
}
}
It gives the following output,