JS部分:
新建一个from在影藏的div下,然后提交这个from
function downLoadExcl(){
$("#submitForm").remove();
var supplierInfoIds = "111";
var path = "${ctxPath}/secKill/exportActivityUrl.do";
var html = '<form method="post" id="submitForm" action="' +path+ '" >';
html += '<input name="supplierInfoIds" value="'+supplierInfoIds+'"/>';
html += '</form>';
$("#exportdiv").append(html);
$("#submitForm").submit();
}
java后台处理请求部分:
private static final String ATTACHMENT_FILENAME = "attachment; filename=\"";
private static final String STRING = "\"";
private static final String CONTENT_DISPOSITION = "Content-disposition";
private static final String APPLICATION_OCTET_STREAM = "APPLICATION/OCTET-STREAM";
private static final String ISO_8859_1 = "ISO-8859-1";
private static final String UTF_8 = "UTF-8";
private static final String USER_AGENT = "user-agent";
private static final String MSIE = "msie";
@RequestMapping("exportActivityUrl.do")
public void exportActivityUrl(HttpServletRequest request,
HttpServletResponse response) {
Map<String, Object> paramMap = this.getParamMapFromRequest(request);
// 生成excel
HSSFWorkbook workbook = secKillService
.querysupplierInfoByIdsAndExport(paramMap);
String excelname = "";
excelname = "秒杀活动链接.xls";
String newexcelname = "";
String isIE = request.getHeader(USER_AGENT);
try {
if (isIE.toLowerCase().indexOf(MSIE) < 0) {
// 解决中文excel文件名乱码问题,非IE
newexcelname = new String(excelname.getBytes(UTF_8),
ISO_8859_1);
} else {
// 解决中文excel文件名乱码问题,IE
newexcelname = URLEncoder.encode(excelname, UTF_8);
}
} catch (Exception e) {
logger.logException(e);
}
response.reset();
response.setContentType(APPLICATION_OCTET_STREAM);
response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + newexcelname + STRING); // 实现下载
try {
workbook.write(response.getOutputStream());// 实现输出
response.flushBuffer();
} catch (Exception e) {
logger.logException(e);
}
}
java后台,生成excl部分
/**
* 活动信息
*/
private static final String[] SIGNUP_KEY = { "商品编码", "商品名称", "秒杀价", "活动数量", "运费", "预热开始时间", "秒杀开始时间", "秒杀结束时间",
"秒杀活动链接" };
/**
* 活动信息
*/
private static final String[] SIGNUP_VALUE = { "productCode", "productName", "seckillPrice", "amount",
"mailingPrice", "preheadStartTime", "secKillStartTime", "secKillEndTime", "activityUrl" };
@Override
public HSSFWorkbook querysupplierInfoByIdsAndExport(
Map<String, Object> paramMap) {
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 查询导出信息
List<Map<String, Object>> supplierInfoList = dalClient.queryForList(NAMESPACE + ".querysupplierInfoByIds", paramMap);
// 设置单元格文本格式
HSSFCellStyle cellStyle = workbook.createCellStyle();
HSSFDataFormat format = workbook.createDataFormat();
cellStyle.setDataFormat(format.getFormat("@"));
// 生成一个sheet
HSSFSheet sheet = workbook.createSheet("秒杀活动链接");
HSSFRow row0 = sheet.createRow(0);// 定义第一行
// 由该字段判断应该创建几列
int keylen =SIGNUP_KEY.length;
for (int i = 0; i < keylen; i++) {
HSSFCell cell = row0.createCell(i);// 定义第i列
cell.setCellValue(SIGNUP_KEY[i]);// 给第i列赋值
cell.setCellStyle(cellStyle);// 设置为文本格式
}
if (CollectionUtils.isNotEmpty(supplierInfoList)) {
// 放入值
int len = supplierInfoList.size();
Map<String, Object> productMap;
for (int i = 0; i < len; i++) {
productMap = supplierInfoList.get(i);
HSSFRow newRow = sheet.createRow(i + 1);// 创建新的一行
newRow.setRowStyle(cellStyle); // 设置为文本格式
for (int j = 0; j < keylen; j++) {
HSSFCell newCell = newRow.createCell(j);// 定义第i列
newCell.setCellValue(MapUtils.getString(productMap, SIGNUP_VALUE[j], ""));// 给第i列赋值
newCell.setCellStyle(cellStyle);
}
}
}
return workbook;
}