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

Java操作Excel之Excel文件的下载

2017-04-09 17:00 344 查看
1、有的时候在Web应用会有下载Excel的需求,现分享下后台实现下载Excel文件的代码

@RequestMapping(value = "/exportExcel",  method = {RequestMethod.GET})
public void exportExcel(CorplutionComparisonGroupCustomerDO corplutionComparisonGroupCustomerDO, HttpServletRequest request, HttpServletResponse response){
// 下载的Excel的文件名称
String fileName = "客户名单";
response.setContentType("application/vnd.ms-excel");
String codedFileName = null;
OutputStream ops = null;
List<CorplutionComparisonGroupCustomerDO> corplutionContactList = new ArrayList<CorplutionComparisonGroupCustomerDO>();
try{
// 进行转码,使其支持中文文件名
codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
// 产生工作簿对象
//产生工作表对象
corplutionContactList = corplutionComparisonGroupCustomerFacade.findList(corplutionComparisonGroupCustomerDO);
Map<String, String> headMap = new LinkedHashMap<String, String>();
headMap.put("CID", "customerId");
headMap.put("客户公司全称", "companyFullName");
Map<String, Object> excelData = ExcelUtils.fillExcelData(headMap, corplutionContactList);
List<String> heads = (List<String>) excelData.get("heads");
List<List<String>> dataList = (List<List<String>>)excelData.get("dataList");
HSSFWorkbook workbook = ExcelUtils.createExcelFile(fileName, heads , dataList);
ops = response.getOutputStream();
workbook.write(ops);
}catch(Exception e){
if (logger.isInfoEnabled()){
logger.error("导出excel异常", e);
}
}
}

2、有关ExcelUtils.java的实现,可以参考上一篇博客的博客Java操作Excel之POI的常用用法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POI Java Excel