@RequestMapping(value = "/ucAlarmlog/list/upload", method = RequestMethod.GET)
public String userListUpload(Model model,HttpServletRequest request, HttpServletResponse response){
List listAlarmlogs = ucAlarmlogRepository.findAll();
if(listAlarmlogs!=null && listAlarmlogs.size()>0){
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet hssfSheet = workbook.createSheet("报警信息");
HSSFRow row_t = hssfSheet.createRow(0);
HSSFCell cell_logid = row_t.createCell(0);
cell_logid.setCellValue("logid");
HSSFCell cell_loginst = row_t.createCell(1);
cell_loginst.setCellValue("loginst");
HSSFCell cell_logt = row_t.createCell(2);
cell_logt.setCellValue("logt");
int i=0;
for(UcAlarmlog ua : listAlarmlogs){
HSSFRow row = hssfSheet.createRow(i+1);
HSSFCell v_cell_logid = row.createCell(0);
v_cell_logid.setCellValue(ua.getLogid());
HSSFCell v_cell_loginst = row.createCell(1);
v_cell_loginst.setCellValue(ua.getLoginst());
HSSFCell v_cell_logt = row.createCell(2);
v_cell_logt.setCellValue(ua.getLogt()!=null ? ua.getLogt().toString("yyyy年MM月dd HH时mm分ss秒") : "");
i++;
}
try{
String filename = "报警信息.xls";
filename = encodeFilename(filename, request);
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "p_w_upload; filename="+filename);
OutputStream ouputStream = response.getOutputStream();
workbook.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
return null;
}
public static String encodeFilename(String fileName, HttpServletRequest request) {
try{
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0)
return new String(fileName.getBytes("UTF-8"), "ISO8859-1");//firefox浏览器
else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0)
return URLEncoder.encode(fileName, "UTF-8");//IE浏览器
}catch(Exception e){
e.printStackTrace();
}
return null;
}