Export excel in java:
Import java. io. ByteArrayOutputStream;
Import java. io. File;
Import java. io. IOException;
Import java. text. DateFormat;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import java. util. Vector;
Import javax. servlet. ServletOutputStream;
Import jxl. Workbook;
Import jxl. format. Colour;
Import jxl. format. UnderlineStyle;
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 ExcelBean {
Public ExcelBean (){
}
Public void exportToExcel (String tempFilePath, String filename,
String sheetname, String [] titles, Vector vect, ServletOutputStream outputStream) throws Exception {
Try {
Workbook wb = Workbook. getWorkbook (new File (tempFilePath ));
ByteArrayOutputStream targetFile = new ByteArrayOutputStream ();
// Create a new Excel Workbook
WritableWorkbook workbook = Workbook. createWorkbook (targetFile, wb );
// Create a worksheet in an Excel worksheet named "first page"
WritableSheet wsheet = workbook. createSheet (sheetname, 0); // sheet ();
WritableFont font = new WritableFont (WritableFont. ARIAL, 14,
WritableFont. BOLD, false, UnderlineStyle. NO_UNDERLINE,
Colour. BLACK );
WritableCellFormat format = new WritableCellFormat (font );
For (int I = 0; I <titles. length; I ++ ){
Label wlabel1 = new Label (I, 0, titles [I], format); // text in rows, columns, cells, and text format
Wsheet. addCell (wlabel1 );
}
Font = new WritableFont (WritableFont. createFont (""), 12,
WritableFont. NO_BOLD, false, UnderlineStyle. NO_UNDERLINE,
Colour. BLACK );
Format = new WritableCellFormat (font );
For (int I = 1; I <vect. size () + 1; I ++) {// create a row (the top row) at index 0)
Object [] sdata = (Object []) vect. elementAt (I-1 );
For (int j = 0; j <sdata. length; j ++) {// create a cell at index 0 (upper left)
Label wlabel1 = new Label (j, I, (String) sdata [j], format); // text in rows, columns, cells, and text format
Wsheet. addCell (wlabel1 );
}
}
Workbook. write ();
Workbook. close ();
Wb. close ();
TargetFile. writeTo (outputStream );
TargetFile. close ();
} Catch (WriteException ex1 ){
System. out. println ("WriteException:" + ex1.getMessage ());
} Catch (IOException ex2 ){
System. out. println ("IOException:" + ex2.getMessage ());
}
}
Public static String compare_date (String DATE1, String DATE2 ){
DateFormat df = new SimpleDateFormat ("yyyy-MM-dd ");
Try {
Date dt1 = df. parse (DATE1 );
Date dt2 = df. parse (DATE2 );
If (dt1.getTime ()> dt2.getTime ()){
Return "1 ";
} Else if (dt1.getTime () <dt2.getTime ()){
Return "-1 ";
} Else {
Return "0 ";
}
} Catch (Exception exception ){
Exception. printStackTrace ();
}
Return "0 ";
}
}
Author: ERDP Technical Architecture"