java输出EXCEL并提供下载 源码

package com.xsofa.huaxun.news.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.xsofa.huaxun.order.bean.CutPage;
import java.util.ResourceBundle;
import jxl.Workbook;
import java.text.SimpleDateFormat;
import jxl.write.*;
import com.xsofa.huaxun.order.bean.MainOrderInfo;
import jxl.format.UnderlineStyle;

public class PutOutExcel
    extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";

//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
   ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    HttpSession session=request.getSession();
    String[] search=(String[])session.getAttribute("search2");
    String agentid=(String)session.getAttribute("agentid");

      ResourceBundle rb=ResourceBundle.getBundle("filePath");
      String filepath=rb.getString("projectPath");
     // String exac
      File f=new File(filepath);
      if(!f.exists())
      f.mkdir();

Calendar c=Calendar.getInstance();
Date d=c.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
String time=sdf.format(d);
ArrayList orders=new com.xsofa.huaxun.order.operation.MyFunction().getExcelOrders(Integer.parseInt(search[0]),Integer.parseInt(search[1]),search[2],agentid);
File downfile=new File(filepath+time+"定单统计.xls");
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(downfile);
jxl.write.WritableSheet ws = wwb.createSheet("定单统计", 0);
jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
jxl.write.Label labelC1 = new jxl.write.Label(0, 0,"定单统计列表",wcfFC);
try {
    ws.addCell(labelC1);
}
catch (WriteException ex2) {
    ex2.printStackTrace();
}
String titles[]=new String[]{"定单编号","定单商品","客户名称","代理商","定单时间","定单金额","提交次数","定单状态"};
for(int i=0;i<titles.length;i++){
   jxl.write.Label labelC = new jxl.write.Label(i, 1,titles[i]);
    try {
      ws.addCell(labelC);
    }
    catch (WriteException ex) {
      ex.printStackTrace();
    }
}
for(int i=0;i<orders.size();i++){
MainOrderInfo orderinfo=(MainOrderInfo)orders.get(i);
try {
    ws.addCell(new jxl.write.Label(0, i + 2, orderinfo.getRealid()));
    ws.addCell(new jxl.write.Label(1, i + 2, orderinfo.getProname()));
    ws.addCell(new jxl.write.Label(2, i + 2, orderinfo.getCustomername()));
    ws.addCell(new jxl.write.Label(3, i + 2, orderinfo.getAgentid()));
    ws.addCell(new jxl.write.Label(4, i + 2, orderinfo.getOrdertime()));
    ws.addCell(new jxl.write.Label(5, i + 2, orderinfo.getAllmoney()));
    ws.addCell(new jxl.write.Label(6, i + 2, orderinfo.getAddcount()));
    ws.addCell(new jxl.write.Label(7, i + 2, orderinfo.getStatus()));

}

catch (WriteException ex1) {
ex1.printStackTrace();
}
}
try{
wwb.write();
    wwb.close();
String filename=toUtf8String("定单统计");
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment;filename="+filename+".xls");
FileInputStream finput = new FileInputStream(downfile);
OutputStream output = response.getOutputStream();
BufferedInputStream buffin = new BufferedInputStream(finput);
BufferedOutputStream buffout = new BufferedOutputStream(output);
byte[] buffer = new byte[4096];
int count = 0;
while ( (count = buffin.read(buffer, 0, buffer.length)) > 0) {
buffout.write(buffer, 0, count);
}
buffin.close();
buffout.close();
finput.close();
output.close();
downfile.delete();
}catch(Exception ex3){
ex3.printStackTrace();
}
}

public String toUtf8String(String s) {
       StringBuffer sb = new StringBuffer();
       for (int i=0;i<s.length();i++) {
           char c = s.charAt(i);
           if (c >= 0 && c <= 255) {
               sb.append(c);
           } else {
               byte[] b;
               try {
                   b = Character.toString(c).getBytes("utf-8");
               } catch (Exception ex) {
                   System.out.println(ex);
                   b = new byte[0];
               }
               for (int j = 0; j < b.length; j++) {
                   int k = b[j];
                   if (k < 0) k += 256;
                   sb.append("%" + Integer.toHexString(k).
                   toUpperCase());
               }
           }
       }
       return sb.toString();
   }
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    doGet(request, response);
}

//Clean up resources
public void destroy() {
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值