Sometimes we need to generate reports in excel sheet from our application. The Java Excel API allow us to dynamically create reports in excel. It also allow us to read spread sheets. It supports different formatting of cells and fonts as well as colouring and shading of cells.
Below is an example code which creates a spread sheet using JExcel API.
public static void main(String[] args) { //create WorkbookSettings object WorkbookSettings ws = new WorkbookSettings(); try{ //create work book WritableWorkbook workbook = Workbook.createWorkbook(new File("E:/Tips/JExcelTip/TestReport.xls"), ws); //create work sheet WritableSheet workSheet = null; workSheet = workbook.createSheet("Test Report" ,0); SheetSettings sh = workSheet.getSettings(); //Creating Writable font to be used in the report WritableFont normalFont = new WritableFont(WritableFont.createFont("MS Sans Serif"), WritableFont.DEFAULT_POINT_SIZE, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE); //creating plain format to write data in excel sheet WritableCellFormat normalFormat = new WritableCellFormat(normalFont); normalFormat.setWrap(true); normalFormat.setAlignment(jxl.format.Alignment.CENTRE); normalFormat.setVerticalAlignment(VerticalAlignment.CENTRE); normalFormat.setWrap(true); normalFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK); //write to datasheet workSheet.addCell(new jxl.write.Label(0,0,"User Name",normalFormat)); //write to the excel sheet workbook.write(); //close the workbook workbook.close(); }catch(Exception e){ e.printStackTrace(); }