Controller:
public void exportPcrList(HttpServletRequest request,HttpServletResponse response,Integer status,String date){
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
List<Map<String,Object>> lists=new ArrayList<Map<String,Object>>();
lists =service.exportNewPcrList(status,date);
ExportExcel<Primer> ex = new ExportExcel<Primer>();
String[] headers = {"样本名称","引物F","引物R","片段长度","退火温度","酶","测序引物","日期","溶解类型"};
String[] columNames = {"sampleName","fFrimer","rFrimer","length","temper","enzyme","frimer","handleTime","orderType"};
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
workbook = ex.exportPcr(workbook,0,"新引物工单",headers,columNames,lists,"yyyy-MM-dd");
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
Timestamp tmp = DateUtils.stringToTimestamp(date);
String dateString = DateUtils.getDate2All(tmp);
String fileName = dateNowStr+"导出的数据.xls";
response.reset();
response.setHeader("Content-disposition", dateString);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
//中文乱码下载解决方法
fileName = new String(fileName.getBytes("utf-8"),"iso-8859-1");
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
ServletOutputStream out = response.getOutputStream();
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final Exception e) {
e.printStackTrace();
}finally {
try {
if (bis != null){
bis.close();
}
if (bos != null){
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
spring mvc 导出下载Excel
最新推荐文章于 2024-08-03 14:12:41 发布