Java download excel, javaexcel

Source: Internet
Author: User

Java download excel, javaexcel

Public void doExport_asRunLog (){

Try {

String date = getParameter ("date ");

String title [] = {"Rain", "Tian "};

JSONArray list = new JSONArray ();

List <String> objList = new ArrayList <String> ();

List <String> objList1 = new ArrayList <String> ();

List <String> objList2 = new ArrayList <String> ();

List <String> objList3 = new ArrayList <String> ();

ObjList. add ("one ");

ObjList. add ("two ");

List. add (objList );

ObjList1.add ("1 ");

ObjList1.add ("2 ");

List. add (objList1 );

ObjList2.add ("3 ");

ObjList2.add ("4 ");

List. add (objList2 );

ObjList3.add ("5 ");

ObjList3.add ("6 ");

List. add (objList3 );

// Define the output stream to open the Save dialog box __________________ begin

OutputStream out = getResponse (). getOutputStream (); // get the output stream response. reset (); // clear the output stream

// Set the output file header

GetResponse (). setContentType ("application/msexcel"); // defines the output type.

// GetResponse (). setContentType ("application/x-download; charset = UTF-8 ");

GetResponse (). setHeader ("Content-Disposition", "attachment; filename = AsRunLog" + date + ". xls ");

// Define the output stream to open the Save dialog box ___________________ end

As. export_excel (title, list, out );

} Catch (Exception e ){

E. printStackTrace ();

}

}

 

 

/**

* Export an excel file

* @ Param Title

* @ Param list

* @ Param out

* @ Return

*/

Public int export_excel (String [] Title, List <Object> list, OutputStream out ){

Int res = 0;

// Output the following to EXCEL

Try {

/************* Create a workbook *************/

WritableWorkbook workbook = Workbook. createWorkbook (out );

 

*************/

WritableSheet sheet = workbook. createSheet ("Sheet1", 0 );

 

/************ Set vertical and horizontal printing (vertical by default), print paper ******************/

Jxl. SheetSettings sheetset = sheet. getSettings ();

Sheetset. setProtected (false );

 

/************** Set the cell font ***************/

WritableFont NormalFont = new WritableFont (WritableFont. ARIAL, 10 );

WritableFont BoldFont = new WritableFont (WritableFont. ARIAL, 10, WritableFont. BOLD );

 

/************** Set the following three cell styles and use them flexibly *************/

// Used to center the title

WritableCellFormat wcf_center = new WritableCellFormat (BoldFont );

Wcf_center.setBorder (Border. ALL, BorderLineStyle. THIN); // line

Wcf_center.setVerticalAlignment (VerticalAlignment. CENTRE); // vertical text alignment

Wcf_center.setAlignment (Alignment. CENTRE); // horizontal Alignment of Text

Wcf_center.setWrap (false); // whether the text is wrapped

// The left of the text

WritableCellFormat wcf_left = new WritableCellFormat (NormalFont, NumberFormats. INTEGER );

Wcf_left.setBorder (Border. NONE, BorderLineStyle. THIN); // line

Wcf_left.setVerticalAlignment (VerticalAlignment. CENTRE); // vertical text alignment

Wcf_left.setAlignment (Alignment. LEFT); // horizontal Alignment of Text

Wcf_left.setWrap (false); // whether the text is wrapped

Sheet. getSettings (). setdefacolumcolumnwidth (20); // you can specify the default width of a column.

/***************** The title starting with EXCEL is as follows, **********************/

// Sheet. mergeCells (0, 0, colWidth, 0 );

// Sheet. addCell (new Label (0, 0, "XX Report", wcf_center ));

/**************** The title of the first row and column in EXCEL is **************** ******/

For (int I = 0; I <Title. length; I ++ ){

Sheet. addCell (new Label (I, 0, Title [I], wcf_center ));

}

***************** *****/

For (int I = 0; I <list. size (); I ++ ){

List <Object> objList = list. get (I );

For (int j = 0; j <Title. length; j ++ ){

Object obj = objList. get (j );

If (obj instanceof Integer ){

Sheet. addCell (new jxl. write. Number (j, I + 1, new Integer (objList. get (j). toString (). intValue (), wcf_left ));

} Else if (obj instanceof Double ){

Sheet. addCell (new jxl. write. Number (j, I + 1, new Double (objList. get (j). toString (). doubleValue (), wcf_left ));

} Else if (obj instanceof Float ){

Sheet. addCell (new jxl. write. Number (j, I + 1, new Double (objList. get (j). toString (). floatValue (), wcf_left ));

} Else {

Sheet. addCell (new Label (j, I + 1, new String (objList. get (j). toString (), wcf_left ));

}

}

}

/*********** Write the content in the above cache to the EXCEL file *********/

Workbook. write ();

**************/

Workbook. close ();

} Catch (Exception e ){

Res =-1;

E. printStackTrace ();

}

Return res;

}

Related Article

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.