@GetMapping("/export")
public void exportExcel(HttpServletResponse response) throws Exception {
String filePathName ="E:/gitpro/*****/upload/temp";
String fileName ="/cccc.xls";
String exportExcelName="bbbbb.xls";
List<CompanyExcelBO> list = companyService.exortExcel();
ByteArrayOutputStream os = new ByteArrayOutputStream();
exportExcel(list, os,filePathName,fileName);
try {
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(exportExcelName.getBytes("gb2312"), "ISO8859-1"));
response.setContentLength(content.length);
ServletOutputStream outputStream = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private String exportExcel(List<CompanyExcelBO> list, OutputStream out,String filePathName,String fileName ) {
int widthCellNumber=10;
try{
// 读进一个excel模版
FileInputStream fos = new FileInputStream(filePathName +fileName );
POIFSFileSystem fs = new POIFSFileSystem(fos);
// 第一步,创建一个webbook,对应一个Excel文件
Boolean is03Excel = ExcelVaildateUtils.isExcel(filePathName+fileName);
//1.创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook(fs);
//1.1创建合并单元格对象
CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,widthCellNumber-1);//起始行,结束行,起始列,结束列
//1.2头标题样式
//2.创建工作表
HSSFSheet sheet = workbook.getSheetAt(0);
//4.操作单元格;将用户列表写入excel
if (list != null) {
for (int j = 0; j < list.size(); j++) {
//创建数据行,前面有两行,头标题行和列标题行
HSSFRow row3 = sheet.createRow(j + 3);
row3.createCell(0).setCellValue(list.get(j).getxxx());
row3.createCell(1).setCellValue(list.get(j).getxxx());
row3.createCell(2).setCellValue(list.get(j).getxxx());
}
}
workbook.write(out);
workbook.close();
} catch (Exception e) {
logger.info("exportExcel==>{}",e.getMessage());
e.printStackTrace();
}
return EXPORT_STATE_OK;
}