SpringMVC+POI下载文件模板和导出Excel
2016-12-12 18:45
841 查看
如果文件模板是固定的,直接放到服务目录下,然后可以用下面的代码下载
<a target="_self" href="/template/template.xlsx" >下载office模版</a> 如果是需要从数据库查询出来导出的,则参考下面的代码
@RequestMapping(value = "/exportMatrixLeaderSheet")
public String exportMatrixLeaderSheet(HttpServletResponse response) throws IOException{
response.setHeader("Content-Disposition","attachment; filename="+new String(("联络人清单").getBytes("gb2312"),"ISO-8859-1")+".xls");
OutputStream out = response.getOutputStream();
mtxExportService.exportMatrixLeaderSheet(out);
out.close();
return null;
}
<a target="_self" href="/template/template.xlsx" >下载office模版</a> 如果是需要从数据库查询出来导出的,则参考下面的代码
@RequestMapping(value = "/exportMatrixLeaderSheet")
public String exportMatrixLeaderSheet(HttpServletResponse response) throws IOException{
response.setHeader("Content-Disposition","attachment; filename="+new String(("联络人清单").getBytes("gb2312"),"ISO-8859-1")+".xls");
OutputStream out = response.getOutputStream();
mtxExportService.exportMatrixLeaderSheet(out);
out.close();
return null;
}
/** * 把查询出的内控领导清单写入工作簿 * @param out */ public void exportMatrixLeaderSheet(OutputStream out){ HSSFWorkbook workbook = new HSSFWorkbook(); // 声明一个工作薄 HSSFSheet sheet = workbook.createSheet("联络人清单"); // 生成一个表格 sheet.setDefaultColumnWidth(20);// 设置表格默认列宽度为30个字节 HSSFRow row = sheet.createRow(0); row.setHeight((short)(15.625*40)); HSSFCell cell = row.createCell(0); cell.setCellStyle(getHeadCellStyle(workbook)); cell.setCellValue("单位"); cell = row.createCell(1); cell.setCellStyle(getHeadCellStyle(workbook)); cell.setCellValue("部门(三级单位)"); cell = row.createCell(2); cell.setCellStyle(getHeadCellStyle(workbook)); cell.setCellValue("管理员"); cell = row.createCell(3); cell.setCellStyle(getHeadCellStyle(workbook)); cell.setCellValue("自评工作责任部门及部门经理"); cell = row.createCell(4); cell.setCellStyle(getHeadCellStyle(workbook)); cell.setCellValue("自评工作分管领导"); HSSFCellStyle style = workbook.createCellStyle(); style.setWrapText(true); List<MatrixLeaderSheet> mtxLeaderSheets = userInitService.getAllMtxLeaderSheet(); if(mtxLeaderSheets != null && mtxLeaderSheets.size() > 0){ int i = 1; for(MatrixLeaderSheet mtxLeaderSheet : mtxLeaderSheets){ row = sheet.createRow(i); row.setHeight((short)(15.625*30)); cell = row.createCell(0); cell.setCellStyle(style); cell.setCellValue(mtxLeaderSheet.getUnitName()); cell = row.createCell(1); cell.setCellStyle(style); cell.setCellValue(mtxLeaderSheet.getDepartmentName()); cell = row.createCell(2); cell.setCellStyle(style); cell.setCellValue(mtxLeaderSheet.getPostAdminUserName()); cell = row.createCell(3); cell.setCellStyle(style); cell.setCellValue(mtxLeaderSheet.getPostManagerUserName()); cell = row.createCell(4); cell.setCellStyle(style); cell.setCellValue(mtxLeaderSheet.getPostLeaderUserName()); i++; } } try { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } }
相关文章推荐
- Springboot/SpringMVC+POI 实现Excel导出功能(点击下载方式实现)
- Spring Boot--POI导出excel文件下载
- SpringMVC+POI 实现Excel按模板方式简单导出功能
- 《SpringMvc+POI 处理Excel的导出操作》
- springmvc + mybatis + poi导出Excel实例
- 关于springMVC+poi上传Excel文件问题解决
- springMVC+poi导出excel
- SpringMVC+Hibernate +MySql+ EasyUI实现POI导出Excel(二)
- spring+springmvc+hibernate利用poi实现导出Excel功能
- POI通过模板导出EXCEL文件的实例
- poi导入excel文件!(spring+springmvc+mybatis)
- SpringMVC/SpringBoot使用easypoi实现Excel文件导入导出功能实现
- 【poi excel导出】不同浏览器的编码方式,解决下载文件名称乱码
- springboot+poi导出指定格式Excel模板详解+Demo
- poi操作excel导出是否修改模板文件内容的问题
- asp.net mvc导出excel文件先保存到服务器某个目录,然后再导出到客户端供客户下载功能的实现
- Spring+SpringMVC+MyBatis +apche poi 实现excel 导出工具类封装
- springmvc+excel多表头模板下载功能
- SpringMVC+Hibernate +MySql+ EasyUI实现POI导出Excel(二)
- 【POI】导出excel文件,不生成中间文件,直接将内存中的数据创建对象下载到浏览器