1.引入pom依赖
<!--导出Excel-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
<!--导出Excel end-->
2.Controller代码
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import sun.tools.jar.resources.jar;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/export")
public class ExportToExcelController {
@Autowired
private ReportFormImpl reportFormImpl;
@ResponseBody
@RequestMapping(value = "/exportStatisticsNum/{excelName}")
public jar exportWhiteList(@PathVariable("excelName") String excelName,
HttpServletResponse response) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow nRow = sheet.createRow(0);
HSSFCell nCell = nRow.createCell(0);
int rowNo = 0;
int colNo = 0;
String[] title;
title = new String[]{"业务名称", "项目版本", "重要程度", "风险需求数", "风险需求修复数", "安全漏洞数", "安全漏洞修复数", "修复率"};
nRow = sheet.createRow(rowNo++);
for (int i = 0; i < title.length; i++) {
nCell = nRow.createCell(i);
nCell.setCellValue(title[i]);
}
try {
List<ReportFormQuerySecurityProject> statisticsNumList = reportFormImpl.getStatisticsNum(new QuerySecurityProject());
for (ReportFormQuerySecurityProject dto : statisticsNumList) {
colNo = 0;
nRow = sheet.createRow(rowNo++);
nCell = nRow.createCell(colNo++);
nCell.setCellValue(dto.getJobName());
nCell = nRow.createCell(colNo++);
nCell.setCellValue(dto.getProjectVersion());
nCell = nRow.createCell(colNo++);
Byte degree = dto.getImportanceDegree();
String importanceDegree = "";
if (degree == 1) {
importanceDegree = "严重";
} else if (degree == 2) {
importanceDegree = "高危";
} else if (degree == 3) {
importanceDegree = "中危";
} else if (degree == 4) {
importanceDegree = "低危";
}
nCell.setCellValue(importanceDegree);
nCell = nRow.createCell(colNo++);
nCell.setCellValue(dto.getRiskDemandNum());
nCell = nRow.createCell(colNo++);
nCell.setCellValue(dto.getRiskDemandRepairNum());
nCell = nRow.createCell(colNo++);
nCell.setCellValue(dto.getBugNum());
nCell = nRow.createCell(colNo++);
nCell.setCellValue(dto.getBugRepairNum());
nCell = nRow.createCell(colNo++);
nCell.setCellValue(String.format("%.2f", dto.getRepairRate()) + "%");
}
loadResponse(excelName, response, wb);
} catch (Exception e) {
}
return null;
}
private void loadResponse(String excelName, HttpServletResponse response, HSSFWorkbook wb) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
wb.write(byteArrayOutputStream);
String dateTime = DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
String outFile = excelName + dateTime + ".xls";
response.setContentType("application/vnd.ms-excel;charset=utf-8");
outFile = response.encodeURL(new String(outFile.getBytes("gb2312"), "iso8859-1"));
response.addHeader("Content-Disposition", "attachment;filename=" + outFile);
response.setContentLength(byteArrayOutputStream.size());
Cookie cookie = new Cookie("fileDownload", "true");
cookie.setPath("/");
response.addCookie(cookie);
ServletOutputStream outputstream = response.getOutputStream();
byteArrayOutputStream.writeTo(outputstream);
byteArrayOutputStream.close();
outputstream.flush();
}
}
然后直接在浏览器访问就行