/*** 导出数据为Excel文件
* 下面注释的代码为直接响应到服务器的
*@paramrequest
*@paramresponse
*@return
*/@GetMapping("/exportExcel")publicString exportExcel(HttpServletRequest request, HttpServletResponse response) {
List list =tzJobSetService.list();
String resultName="";
String[] title= {"ID", "标题", "排序", "创建时间"};//String filename = "jobSet.xls";
String sheetName = "职称设置表";
String[][] content= new String[list.size()][4];try{for (int i = 0; i < list.size(); i++) {
content[i][0] =String.valueOf(list.get(i).getId());
content[i][1] =list.get(i).getTitle();
content[i][2] =String.valueOf(list.get(i).getSortId());
content[i][3] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(list.get(i).getAddTime());
}
}catch(Exception e) {
e.printStackTrace();
}
HSSFWorkbook wb= ImportExcel.getHSSFWorkbook(sheetName, title, content, null);try{
String ctxPath= "D://upFiles";
String name= new SimpleDateFormat("ddHHmmss").format(newDate());
String fileName=name+"jobSet.xlsx";
String bizPath= "files";
String nowday= new SimpleDateFormat("yyyyMMdd").format(newDate());
File file= new File(ctxPath + File.separator + bizPath + File.separator +nowday);if (!file.exists()) {
file.mkdirs();//创建文件根目录
}
String savePath= file.getPath() + File.separator +fileName;
resultName= bizPath + File.separator + nowday+ File.separator +fileName;if (resultName.contains("\\")) {
resultName= resultName.replace("\\", "/");
}
System.out.print(resultName);
System.out.print(savePath);//响应到客户端需要下面注释的代码//this.setResponseHeader(response, filename);//OutputStream os = response.getOutputStream();//响应到服务器
OutputStream os = new FileOutputStream(savePath); //保存到当前路径savePath
wb.write(os);
os.flush();
os.close();
}catch(Exception e) {
e.printStackTrace();
}returnresultName;
}