Java action jxl generate and download excel

Source: Internet
Author: User
Tags log log

[Java]
/**
* ReportExportAction. java
* Copyright (C) 2012
* Creation: cuiran 2012-08-14 10:38:21
*/
Package com. wpn. web. action. report;
 
Import java. io. BufferedInputStream;
Import java. io. BufferedOutputStream;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. OutputStream;
Import java. io. PrintWriter;
Import java. util. List;
 
Import javax. servlet. ServletOutputStream;
Import javax. servlet. http. HttpServletResponse;
 
Import jxl. Workbook;
Import jxl. format. Colour;
Import jxl. write. Border;
Import jxl. write. Label;
Import jxl. write. WritableSheet;
Import jxl. write. WritableWorkbook;
 
Import org. apache. commons. logging. Log;
Import org. apache. commons. logging. LogFactory;
 
Import com. alibaba. fastjson. JSON;
Import com. wpn. web. dao. union. ICallTaxiDao;
Import com. wpn. web. domain. report. ReportCallInfo;
Import com. wpn. web. dto. report. ReportCallDto;
Import com. wpn. web. processor. IProcessor;
Import com. wpn. web. processor. requestVo. mytmq. BaseRequestVo;
Import com. wpn. web. processor. requestVo. report. ReportCallVo;
Import com. wpn. web. util. JsonCallback;
Import com. wpn. web. util. UtilDate;
Import com. wpn. web. util. VeDate;
 
/**
* TODO
* @ Author cuiran
* @ Version TODO
*/
Public class ReportExportAction implements IProcessor {

Private static Log log = LogFactory. getLog (ReportExportAction. class. getName ());

Private ICallTaxiDao dao;

/**
* @ Return the dao
*/
Public ICallTaxiDao getDao (){
Return dao;
}
 
/**
* @ Param dao the dao to set
*/
Public void setDao (ICallTaxiDao dao ){
This. dao = dao;
}
 
/* (Non-Javadoc)
* @ See com. wpn. web. processor. IProcessor # processMessage (com. wpn. web. processor. requestVo. mytmq. BaseRequestVo, javax. servlet. http. HttpServletResponse)
*/
@ Override
Public void processMessage (BaseRequestVo model, HttpServletResponse response ){
ReportCallDto dto = new ReportCallDto ();

PrintWriter out = null;
ReportCallVo vo = (ReportCallVo) model;
Try {
Response. reset ();

Out = response. getWriter ();

Response. setCharacterEncoding ("UTF-8 ");
Response. reset ();
 

String strFromTime = "";
String strEndTime = "";

List <ReportCallInfo> list = dao. getReportCallInfo (strFromTime, strEndTime );
String excelName = UtilDate. getFirstOrderNum (6 );

String path = this. excelSave (list, excelName );
Response. setContentType ("application/octet-stream; charset = UTF-8 ");
// Download
Response. setHeader ("Content-disposition", "attachment; filename =" + excelName + ". xls"); // file name, which is garbled here
BufferedInputStream bis = new BufferedInputStream (new FileInputStream (path ));

ServletOutputStream sos = response. getOutputStream ();
BufferedOutputStream bos = new BufferedOutputStream (sos );
Byte [] buffer = new byte [1024];
Int len =-1;
While (len = bis. read (buffer ))! =-1 ){
Bos. write (buffer, 0, len );
}
Bos. close ();
Sos. close ();
Bis. close ();
 
} Catch (Exception e ){
Log. error ("An error occurred while obtaining the taxi Request volume", e );
}
 
}
Public String excelSave (List <ReportCallInfo> list, String excelName ){

String path = "D :\\ UpLoadFile \" + excelName + ". xls ";
Try {

WritableWorkbook book = Workbook. createWorkbook (new File (
Path ));

WritableSheet sheet = book. createSheet ("sheet_1", 0 );
Jxl. write. WritableFont font1 = new jxl. write. WritableFont (
Jxl. write. WritableFont. TIMES, 16,
Jxl. write. WritableFont. BOLD );
Jxl. write. WritableFont font3 = new jxl. write. WritableFont (
Jxl. write. WritableFont. TIMES, 10,
Jxl. write. WritableFont. BOLD );
Jxl. write. WritableCellFormat CBwcfF1 = new jxl. write. WritableCellFormat (
Font1 );
Jxl. write. WritableCellFormat CBwcfF2 = new jxl. write. WritableCellFormat ();
Jxl. write. WritableCellFormat CBwcfF3 = new jxl. write. WritableCellFormat (
Font3 );
Jxl. write. WritableCellFormat CBwcfF4 = new jxl. write. WritableCellFormat ();
CBwcfF1.setAlignment (jxl. write. Alignment. CENTRE );
CBwcfF2.setAlignment (jxl. write. Alignment. RIGHT );
CBwcfF3.setAlignment (jxl. write. Alignment. CENTRE );
CBwcfF3.setBorder (Border. ALL, jxl. format. BorderLineStyle. THIN,
Colour. BLACK );
CBwcfF4.setBorder (Border. ALL, jxl. format. BorderLineStyle. THIN,
Colour. BLACK );
Label labelhead16 = new Label (0, 0, "transparent ball statistics info table", CBwcfF1 );

Sheet. addCell (labelhead16 );


Sheet. mergeCells (0, 0, 12, 0 );
Sheet. mergeCells (0, 1, 12, 1 );

Sheet. setColumnView (0, 5 );
Sheet. setColumnView (1, 25 );
Sheet. setColumnView (2, 25 );
Label labelhead0 = new Label (0, 2, "Serial Number", CBwcfF3 );
Label labelhead1 = new Label (1, 2, "date", CBwcfF3 );
Label labelhead2 = new Label (2, 2, "number of calls", CBwcfF3 );
Sheet. addCell (labelhead0 );
Sheet. addCell (labelhead1 );
Sheet. addCell (labelhead2 );

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

Label label0 = new Label (0, I + 3, I + 1 + "", CBwcfF4 );
Label label1 = new Label (1, I + 3, formatString (data
. GetNowday (), CBwcfF4 );
Label label2 = new Label (2, I + 3, formatString (data
. GetCallnum (), CBwcfF4 );


Sheet. addCell (label0 );
Sheet. addCell (label1 );
Sheet. addCell (label2 );
}
Log. debug ("excel exported ");


Book. write ();
Book. close ();

} Catch (Exception e ){
E. printStackTrace ();
}

Return path;
}

Public String formatString (String str ){
If (str = null ){
Str = "";
}
Return str;
}
}

Author: cuiran

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.