您的位置:首页 > 编程语言 > Java开发

java输出EXCEL并提供下载 源码

2009-07-01 08:36 274 查看
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() {
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: