case代码
@RequestMapping(value = "/downloadTempate2")
public void downloadTempate2(HttpServletResponse response,HttpServletRequest request) throws IOException {
List<Device> list = deviceService.findAll()
// 在内存中创建一个Excel文件,通过输出流写到客户端提供下载
HSSFWorkbook workbook = new HSSFWorkbook()
// 创建一个sheet页
HSSFSheet sheet = workbook.createSheet("设备数据")
// 创建标题行
HSSFRow headRow = sheet.createRow(0)
headRow.createCell(0).setCellValue("ID")
headRow.createCell(1).setCellValue("设备名称")
headRow.createCell(2).setCellValue("型号")
headRow.createCell(3).setCellValue("购买日期")
headRow.createCell(4).setCellValue("生产厂家")
headRow.createCell(5).setCellValue("数量")
headRow.createCell(6).setCellValue("单价")
for (Device device : list) {
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1)
dataRow.createCell(0).setCellValue(device.getId())
dataRow.createCell(1).setCellValue(device.getDeviceName())
dataRow.createCell(2).setCellValue(device.getDeviceSize())
dataRow.createCell(3).setCellValue(device.getDeviceBuydate())
dataRow.createCell(4).setCellValue(device.getDeviceFactory())
dataRow.createCell(5).setCellValue(device.getDeviceNum())
dataRow.createCell(6).setCellValue(device.getDevicePay())
}
String filename = "设备数据.xls"
String agent = request.getHeader("User-Agent")
filename = FileUtils.encodeDownloadFilename(filename, agent)
//一个流两个头
ServletOutputStream out = response.getOutputStream()
response.addHeader("Content-Disposition", "attachment; filename="+filename)
response.setContentType("application/octet-stream;charset=UTF-8")
workbook.write(out)
}