课程设计中遇到需要生成报表,于是在网上搜集了相关资料。下面贴上实现后的代码。
首先,项目是maven项目。添加依赖。
<!-- 输出报表依赖 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
controller:
/*
* 根据传递的ID导出对应报表
*/
@RequestMapping("/export/{id}")
public void export(HttpServletRequest request, HttpServletResponse response,@PathVariable("id")Integer id) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=myExcel.xls");
OutputStream ouputStream = null;
HSSFWorkbook wb = exportData(id);
try {
ouputStream = response