package com.***.***.***;
import net.sf.jxls.transformer.XLSTransformer;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
CLASS 省略。。。
/***
*@paramrequest
*@paramresponse
*@paramtempPath 模版的相对路径
*@paramresultMap 结果集
*@throwsException*/
public static voidexcelTempExport(HttpServletRequest request,HttpServletResponse response,String tempPath, Map resultMap) {
String tempFileName= null;
String newFileName= null;
File newFile= null;
XLSTransformer transformer= null;
BufferedInputStream bis= null;
BufferedOutputStream bos= null;try{//获得模版
tempFileName =request.getSession().getServletContext().getRealPath(tempPath);//新的文件名称
newFileName= new SimpleDateFormat("yyyyMMddHHmmss").format(newDate());//文件名称统一编码格式
newFileName = URLEncoder.encode(newFileName, "utf-8");//生成的导出文件
newFile = File.createTempFile(newFileName, ".xls");//transformer转到Excel
transformer = newXLSTransformer();//将数据添加到模版中生成新的文件
transformer.transformXLS(tempFileName, resultMap, newFile.getAbsolutePath());//将文件输入
InputStream inputStream = newFileInputStream(newFile);//设置response参数,可以打开下载页面
response.reset();//设置响应文本格式
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + String.valueOf((newFileName + ".xls").getBytes(), "iso-8859-1"));//将文件输出到页面
ServletOutputStream out =response.getOutputStream();
bis= newBufferedInputStream(inputStream);
bos= newBufferedOutputStream(out);byte[] buff = new byte[2048];intbytesRead;//根据读取并写入
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0, bytesRead);
}
}catch(Exception e) {
e.printStackTrace();
}finally{//使用完成后关闭流
try{if (bis != null) {
bis.close();
}if (bos != null) {
bos.close();
}
}catch(IOException e) {}
}
}//测试类
@RequestMapping(value = "/exportExcelTest", method = RequestMethod.GET)
public void exportExcelTest(HttpServletRequest request, HttpServletResponse response) {
Map map = new HashMap(4);
Map itemMap = null;
//商户
List merchantList = new ArrayList();
itemMap = new HashMap();
itemMap.put("id", 3);
itemMap.put("merchant_name", "张3");
itemMap.put("market_id", 1);
itemMap.put("market_name", "市场1");
merchantList.add(itemMap);
itemMap = new HashMap();
itemMap.put("id", 4);
itemMap.put("merchant_name", "张5");
itemMap.put("market_id", 2);
itemMap.put("market_name", "市场2");
merchantList.add(itemMap);
map.put("merchantList", merchantList);
//品类
List bccList = new ArrayList();
itemMap = new HashMap();
itemMap.put("id", 1);
itemMap.put("category", "苹果");
itemMap.put("sub_category", "红富士苹果");
bccList.add(itemMap);
itemMap = new HashMap();
itemMap.put("id", 2);
itemMap.put("category", "苹果");
itemMap.put("sub_category", "国光苹果");
bccList.add(itemMap);
map.put("bccList", bccList);
//供货商
List merchantSupplierList = new ArrayList();
itemMap = new HashMap();
itemMap.put("id", 1);
itemMap.put("supplierName", "李四");
itemMap.put("supplierShopName", "李四水果店");
merchantSupplierList.add(itemMap);
itemMap = new HashMap();
itemMap.put("id", 2);
itemMap.put("supplierName", "王五");
itemMap.put("supplierShopName", "王五蔬菜店");
merchantSupplierList.add(itemMap);
map.put("merchantSupplierList", merchantSupplierList);
//省市区
List areaList = new ArrayList();
itemMap = new HashMap();
itemMap.put("sheng_id", 610000);
itemMap.put("sheng_name", "陕西省");
itemMap.put("shi_id", 610100);
itemMap.put("shi_name", "西安市");
itemMap.put("qu_id", 610104);
itemMap.put("qu_name", "莲湖区");
areaList.add(itemMap);
itemMap = new HashMap();
itemMap.put("sheng_id", 610000);
itemMap.put("sheng_name", "陕西省");
itemMap.put("shi_id", 610100);
itemMap.put("shi_name", "西安市");
itemMap.put("qu_id", 610113);
itemMap.put("qu_name", "雁塔区");
areaList.add(itemMap);
map.put("areaList", areaList);
ExcelTemplate.exportExcelByTemplate(request, response, map, "excelTemplate/template.xls");
}