我是靠谱客的博主 健壮心锁,最近开发中收集的这篇文章主要介绍java实现Excel下载功能,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

@RequestMapping(value = "/downloadTemplateFile")
public ResponseData downloadTemplateFileFromLocal(@RequestParam(required = false) String name,
HttpServletRequest request, HttpServletResponse response) throws IOException {
String fileName;
if(name!=null && !"".equals(name)){
if (!name.matches("[^\s\\/:\*\?\"<>\|](\x20|[^\s\\/:\*\?\"<>\|])*[^\s\\/:\*\?\"<>\|\.]$")){
return ResponseData.failure("文件名不规范");
}
fileName =name;
}else {
fileName = "2020_contract_search_number_0001.xls";
}
log.info("下载文件模版开始,下载的文件名为:{}",fileName);
HSSFWorkbook workbook = new HSSFWorkbook();
String sheetName = "Sheet1";
HSSFSheet sheet1 = workbook.createSheet(sheetName);
//
FileOutputStream out = null;
//添加表头
String titleRow[] = {"所属主工单","号码","运营商","本地网","号码状态","是否有协议","协议名称及协议到期时间"};
HSSFRow row = workbook.getSheet(sheetName).createRow(0);
//创建第一行
for(short i = 0;i < titleRow.length;i++){
HSSFCell cell = row.createCell(i);
cell.setCellValue(titleRow[i]);
}
String exampleRow[] = {"II_20200317_001","13019877233","联通","北京","在网"};
HSSFRow row_2 = workbook.getSheet(sheetName).createRow(1);
//创建第一行
for(short i = 0;i < exampleRow.length;i++){
HSSFCell cell = row_2.createCell(i);
cell.setCellValue(exampleRow[i]);
}
//
HSSFRow row_3 = workbook.getSheet(sheetName).createRow(2);
//
HSSFCell cell = row_3.createCell(0);
//
cell.setCellValue("备注:");
OutputStream output=response.getOutputStream();
response.reset();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=2020_contract_search_number_0001.xls");
log.info("workbook:"+workbook);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
workbook.write(output);
output.close();
return ResponseData.success("下载成功");
}

最后

以上就是健壮心锁为你收集整理的java实现Excel下载功能的全部内容,希望文章能够帮你解决java实现Excel下载功能所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部