当前位置:软件学习 > Excel >>

java action jxl 生成并下载excel

[java] 
/**
 * ReportExportAction.java
 * 版权所有(C) 2012 
 * 创建: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"); 
             //下载 
              response.setHeader("Content-disposition", "attachment;filename=" + excelName+".xls"); //文件名,在这下载了是乱码 
              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("获取打车请求量出错", e); 
        }  
 
    } 
    public String excelSave(List<ReportCallInfo> list,String excelName){ 
         
        String path="D:\\UpLoadFile\\" + excelName + ".xls"; 
        try{ 
             
            WritableWorkbook book = Workbook.createWorkbook(new File( 
                    path)); 
     &nb
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,