Google

Tuesday, March 11, 2008

JAVA - jsp to Excel - jxl API

jxl is a library that support jsp to export data from jsp to excel.
You need to import the jxl jar file into ur own project library..

------------------------------------------------------------------------------------

//Import the class
import javax.servlet.http.*;
import java.util.*;
import java.lang.Integer;
import java.io.File;
import jxl.Workbook;
import jxl.write.WritableWorkbook;
import jxl.write.WritableSheet;
import jxl.write.WritableFont;
import jxl.write.WritableCellFormat;
import jxl.write.Label;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;

//generate output data type == Excel
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=StudentReport.csv");

//create a Excel file
WritableWorkbook w = Workbook.createWorkbook(response.getOutputStream());
//create Excel sheet (sheet's name, sequence of sheet)
WritableSheet sh = w.createSheet("ClassSize", 0);
//declare font type
WritableFont boldFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
//declare font type and color
WritableFont redFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD);
redFont.setColour(Colour.RED);
//declare font alignment
WritableCellFormat wcf_red = new WritableCellFormat(redFont);
wcf_red.setAlignment(Alignment.LEFT);

WritableCellFormat wcf_left = new WritableCellFormat();
wcf_left.setAlignment(Alignment.LEFT);

int excelColCount = 1;
int excelRowCount = 8;

// add and write into cells
sh.addCell(new Label(0, 1, "Institution:",new WritableCellFormat(boldFont)));
sh.addCell(new Label(1, 1, "School Of Multimedia Information Technology"));

sh.addCell(new Label(0,2,"Creation Date : " ,new WritableCellFormat(boldFont)));
sh.addCell(new Label(1,2,getDayOfCreation() ));

sh.addCell(new Label(0,3,""));

sh.addCell(new Label(0,4, "Student : ",new WritableCellFormat(boldFont)));
sh.addCell(new Label(1,4, student));

sh.addCell(new Label(0,5,""));

sh.addCell(new Label(0,6,"Year/Semester :",new WritableCellFormat(boldFont)));
sh.addCell(new Label(1,6,year + "/" + sem));

--------------------------------------------------------------------------------

No comments: