CRM开发: javaweb输出Excel表格.

本文介绍了一种批量导出Excel数据的方法,通过判断用户选择,可以实现导出所有记录或特定ID记录的功能,并通过设置HTTP响应头实现了文件的自动下载。

前台回到后台的数据 就是一串已逗号隔开的ids.. 当这个ids 等于 "all" 时 输出全部数据.

我这里直接输出一个网址,并且自动下载到本地.

其实就是插一根管到对方浏览器,再将本地的缓存内容输出出去!

public String exportCustomerExcel(){
OutputStream output = null;
WritableWorkbook wwb = null;
try {
//设置头文件
response.setCharacterEncoding("UTF-8"); //返回的网页编码设定为utf.
response.setContentType("application/vnd.ms-excel");           // 网页的内容格式,让浏览器知道俺这是什么文件
response.setHeader("Content-Disposition", "attachment; filename="      //http的消息头,告诉浏览器指定文件的拓展名字
+ new String("客户资料.xls".getBytes("GB2312"), "iso-8859-1"));


//判断用户选择的是全部,还是选中的
String isAll = this.jsonObj.getIds();
//如果isAll值为1表示全部导出,否则只导出选中部分
List customerList = null;
if(isAll.equals("all")){
customerList = this.customerManager.getAllCustomer();
}else{
customerList = this.customerManager.getCustomerByIDS(jsonObj.getIds());
}
output = response.getOutputStream(); //一根管子插进对方浏览器里

//创建工作簿
wwb = Workbook.createWorkbook(output); //讲管子的另一头插进我们本地的workBook里
//创建工作表
WritableSheet ws = wwb.createSheet("客户资料", 0);
//设置工作表中的格式
//标题文字格式
WritableFont font1 = new WritableFont(WritableFont.createFont("华文新魏"),10,WritableFont.BOLD);
WritableCellFormat wf = new WritableCellFormat(font1); //这里给单元设置字体格式,就excel格子里一格一格的东西
wf.setAlignment(Alignment.LEFT);   //单元格做对齐...
wf.setVerticalAlignment(VerticalAlignment.CENTRE); //设置垂直对齐方式
//设置标题,单元格的标题
ws.addCell(new Label(0,0,"客户名称",wf)); 
ws.addCell(new Label(1,0,"客户等级",wf));
ws.addCell(new Label(2,0,"客户来源",wf));
ws.addCell(new Label(3,0,"电话一",wf));
ws.addCell(new Label(4,0,"下次联系时间",wf));
ws.addCell(new Label(5,0,"经营范围",wf));
ws.addCell(new Label(6,0,"电子邮件",wf));

//设置正文内容的格式(样式,水平居中,垂直居中,自动换行)
WritableFont font_con = new WritableFont(WritableFont.createFont("华文新魏"),10,WritableFont.NO_BOLD);
WritableCellFormat wf_con = new WritableCellFormat(font_con);
wf_con.setAlignment(Alignment.CENTRE);
wf_con.setWrap(true);
wf_con.setVerticalAlignment(VerticalAlignment.CENTRE);

int cIndex = 1;//行下标
//循环显示正文内容
for(Iterator it = customerList.iterator();it.hasNext();){
Map map = (Map)it.next();
int i = 0;
ws.addCell(new Label(i++,cIndex,(String)map.get("name"),wf_con));
ws.addCell(new Label(i++,cIndex,(String)map.get("grade"),wf_con));
ws.addCell(new Label(i++,cIndex,(String)map.get("source"),wf_con));
ws.addCell(new Label(i++,cIndex,(String)map.get("tel1"),wf_con));

Date nowDate = (Date)map.get("nextvisittime");
if(nowDate != null){
jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-MM-dd");
WritableCellFormat wf_date = new WritableCellFormat(df);
ws.addCell(new DateTime(i++,cIndex,nowDate,wf_date));
}else{
ws.addCell(new Label(i++,cIndex,"尚未设定",wf_con));
}
ws.addCell(new Label(i++,cIndex,(String)map.get("busscope"),wf_con));
ws.addCell(new Label(i++,cIndex,(String)map.get("email"),wf_con));
cIndex++;

}
wwb.write();
wwb.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(output != null){
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
 
return null;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值