java的excel表格的导出与下载

时间:2025-04-29 10:38:02

今天做一个java对excel表格的导出和下载的时候,从网络上搜寻了下载的模板,代码如下:

控制层:

@RequestMapping(value = "excelOut_identifier")
public void excelOutIdentifier(HttpServletRequest request, HttpServletResponse response, Long id, ActivationCodeSearchForm searchForm, ExhibitorInfoVenueBranchRelEditForm editForm,
HttpSession session) {
try {
//获取数据
List<ActivationCodeDto> list = organizerService.ActivationCodeById(id,searchForm).getRows(); //excel标题
String[] title = {"激活码","主办方","创建时间","失效时间"}; //excel文件名
String fileName = "激活码信息表"+System.currentTimeMillis()+".xls"; //sheet名
String sheetName = "激活码信息表"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String [][] content = new String[list.size()][];
for (int i = ; i < list.size(); i++) {
content[i] = new String[list.size();
ActivationCodeDto obj = list.get(i);
content[i][] = obj.getActivationCode();
content[i][] = obj.getExhibitorInfoName();
content[i][] = sdf.format(obj.getCreateTime());
content[i][] = sdf.format(obj.getDisabledTime());
}
//创建HSSFWorkbook
HSSFWorkbook wb = organizerService.excelOutIdentifier(sheetName, title, content, null);
this.organizerService.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
}catch(IOException e1) {
e1.printStackTrace();
}
}

service层:

public  HSSFWorkbook excelOutIdentifier(String sheetName,String []title,String [][]values, HSSFWorkbook wb){
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if(wb == null){
wb = new HSSFWorkbook();
} // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(); // 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 //声明列对象
HSSFCell cell = null; //创建标题
for(int i=;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
} //创建内容
for(int i=;i<values.length;i++){
row = sheet.createRow(i + );
for(int j=;j<values[i].length;j++){
//将内容按顺序赋给对应的列对象
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
} public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}

前端:

<button id="js-export" type="button" class="btn btn-primary">导出Excel</button>

jsp:

$('#js-export').click(function(){
window.location.href="${ctxRoot}/admin/organizer/excelOut_identifier.do?id=${id}"
});

注意事项:

  一开始我的前端页面时这样写的,是写的一个ajax请求:

  

<a class="btn btn-success es-ajax"
href="${ctxRoot}/admin/organizer/excelOut_identifier.do?id=${id}">导出excel</a>

  在后端程序都实现可以跑的通的情况下,也没有什么日志错误的打印,前端页面并没有弹出下载的页面,然后再网络上搜索了方法,测试了一遍,都是不行,最后再一篇文章中看到了一个答案,说下载的情况下,使用ajax请求时,是不会弹出下载的页面出来的,随即将其修改成window.location.href的请求方式了。果然就解决了这个问题