java 下载excel文件_java 下载Excel文件

@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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值