首先要添加EXCEL下载用的jar包:jxl-2.6.12.jar
/*
*参数:resopnse对象;Resuqest对象;
*参数: List sgActivationCodeGmList 是业务中需要下载的内容 这里是将要下载的内容放到了List中了
* 返回类型是 boolean类型:如果下载成功 则返回true 如果返回false 则下载失败;
**/
public boolean exportExcel(HttpServletResponse response,HttpServletRequest request,
List sgActivationCodeGmList) {
try {
//业务要求:获取国际化类型 中文或英文
HttpSession session=request.getSession();
String local=(String)session.getAttribute("local");
ResourceBundle res = null;
if("zh_CN".equals(local)){
res = ResourceBundle.getBundle("applicationMessage",Locale.CHINA);
}else if("en_US".equals(local)){
res = ResourceBundle.getBundle("applicationMessage",Locale.US);
}
// 取得输出流
OutputStream os = response.getOutputStream();
// 清空输出流
response.reset();
// 设定输出文件头 以及文件的名称 这里设置为:activationCode.xls
response.setHeader("Content-disposition", "attachment; filename=activationCode.xls");
// 定义输出类型######这里很重要一定要设置为:application/msexcel
response.setContentType("application/msexcel");
// 建立excel文件
WritableWorkbook wbook = jxl.Workbook.createWorkbook(os);
//定义sheet名称
String tmptitle = "ActivationCode";
// 设置sheet名称 WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // 设置excel中sheet页名称 以及sheet主题中标题以及显示的样式 WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat wcfFC = new WritableCellFormat(wfont); wcfFC.setBackground(Colour.AQUA); wsheet.addCell(new Label(0, 0, tmptitle, wcfFC)); wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); wcfFC = new WritableCellFormat(wfont); // 开始生成主体内容 对应的字段名称 wsheet.addCell(new Label(0, 2, "id")); wsheet.addCell(new Label(1, 2, "code")); wsheet.addCell(new Label(2, 2, "gift_pk_set")); wsheet.addCell(new Label(3, 2, "game")); wsheet.addCell(new Label(4, 2, "channel")); wsheet.addCell(new Label(5, 2, "starttime")); wsheet.addCell(new Label(6, 2, "expiredtime")); wsheet.addCell(new Label(7, 2, "device")); wsheet.addCell(new Label(8, 2, "uid")); wsheet.addCell(new Label(9, 2, "roleId")); wsheet.addCell(new Label(10, 2, "partition")); wsheet.addCell(new Label(11, 2, "times")); wsheet.addCell(new Label(12, 2, "type")); wsheet.addCell(new Label(13, 2, "rule")); wsheet.addCell(new Label(14, 2, "level_min")); wsheet.addCell(new Label(15, 2, "level_max")); wsheet.addCell(new Label(16, 2, "vip_min")); wsheet.addCell(new Label(17, 2, "vip_max")); wsheet.addCell(new Label(18, 2, "batch_name")); wsheet.addCell(new Label(19, 2, "usetime")); wsheet.addCell(new Label(20, 2, "ctime")); //主题内容(多条) if (sgActivationCodeGmList != null) { for (int i = 0; i < sgActivationCodeGmList.size(); i++) { SGActivationCodeGm sgActivation = sgActivationCodeGmList.get(i); //Label方法中参数:param1:一行中列的设定 param2:一个sheet中行的设定(每个sheet页中的行下标是从0开始的) param3:由行列确定一个坐标后要显示的内容(值) wsheet.addCell(new Label(0, i + 3, sgActivation.getId()+"")); wsheet.addCell(new Label(1, i + 3, sgActivation.getCodes())); wsheet.addCell(new Label(2, i + 3, sgActivation.getGift_pk_set())); wsheet.addCell(new Label(3, i + 3, sgActivation.getGame())); wsheet.addCell(new Label(4, i + 3, sgActivation.getChannel())); wsheet.addCell(new Label(5, i + 3, sdf.format(new Date(sgActivation.getStarttime())))); wsheet.addCell(new Label(6, i + 3, sdf.format(new Date(sgActivation.getExpiredtime())))); wsheet.addCell(new Label(7, i + 3, sgActivation.getDevice())); wsheet.addCell(new Label(8, i + 3, sgActivation.getUid())); wsheet.addCell(new Label(9, i + 3, sgActivation.getRoleId())); wsheet.addCell(new Label(10, i + 3, sgActivation.getPpartition())); wsheet.addCell(new Label(11, i + 3, sgActivation.getTimes()+"")); wsheet.addCell(new Label(12, i + 3, sgActivation.getTtype()+"")); wsheet.addCell(new Label(13, i + 3, sgActivation.getRule()+"")); wsheet.addCell(new Label(14, i + 3, sgActivation.getLevel_min()+"")); wsheet.addCell(new Label(15, i + 3, sgActivation.getLevel_max()+"")); wsheet.addCell(new Label(16, i + 3, sgActivation.getVip_min()+"")); wsheet.addCell(new Label(17, i + 3, sgActivation.getVip_max()+"")); wsheet.addCell(new Label(18, i + 3, sgActivation.getBatch_name())); wsheet.addCell(new Label(19, i + 3, sdf.format(new Date(sgActivation.getUsetime())))); wsheet.addCell(new Label(20, i + 3, sdf.format(new Date(sgActivation.getCtime())))); } } wbook.write(); // 写入文件 wbook.close(); os.close(); // 关闭流 return true; } catch (Exception ex) { ex.printStackTrace(); Logs.logger_errorlog.error(ex); return false; } }