@RequestMapping(value = "/ucAlarmlog/list/upload", method = RequestMethod.GET)
public String userListUpload(Model model,HttpServletRequest request, HttpServletResponse response){
List<UcAlarmlog> 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;
}
java 下载Excel文件
原创zhangtao1002 ©著作权
文章标签 POI下载Excel java下载Exc 文章分类 办公效率
上一篇:jstl获取静态变量
-
java和vue实现文件下载
java和vue实现文件下载
java vue -
文件下载 excel下载 jxl下载excel pio下载excel
excel下载代码示例:使用两种方式进行excel文件下载,1、使用jxl方式; 需要jxl.jar。2、使用pio方进行扩展。...
文件下载 excel下载 jxl下载excel pio下载excel jar