Jxl export excel

Source: Internet
Author: User
Tags date1

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.