java:浏览器下载excel模板
1.枚举类
/**
*
* @return
* @author ZHD-xinwei.Fan
* @creed: 下载模板枚举类
* @date 2022/10/24 15:07
*/public enum DownloadTemplate {PRODUCT_IMPORT("40287c5d748a904a01748b716a630101","商品导入","商品名称, 图片地址, 商品价格, 商品分类, 促销开始, 促销结束, 上架日期");private String tid;private String name;private String title;DownloadTemplate(String tid, String name, String title) {this.tid = tid;this.name = name;this.title = title;}public String getTid() {return tid;}public void setTid(String tid) {this.tid = tid;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
2.工具类
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;public class Write2007Excel {// 标题字体private XSSFFont titleFont = null; //2007格式// 标题样式private XSSFCellStyle titleStyle = null;//2007格式// 行信息内容样式private XSSFCellStyle contentStyle = null;//2007格式public XSSFWorkbook addSheet(XSSFWorkbook wb, String[] titleStrs,String sheetName) {System.out.println("sheetName"+sheetName);setExcelStyle(wb);// 执行样式初始化XSSFSheet sheet = wb.createSheet(sheetName);//2007格式XSSFRow titleRow = sheet.createRow((short) 0);//2007格式titleRow.setHeightInPoints(20);// 20像素int titleCount = titleStrs.length;// 列数// 写标题for (int k = 0; k < titleCount; k++) {XSSFCell cell = titleRow.createCell((short) k); //2007格式cell.setCellStyle(titleStyle);// 设置标题样式cell.setCellType(CellType.STRING);cell.setCellValue(titleStrs[k]);sheet.setColumnWidth((short) k, (short) 5000);// 设置列宽}return wb;}/** 样式初始化 */public void setExcelStyle(XSSFWorkbook workBook) {// 设置列标题字体,样式titleFont = workBook.createFont();titleFont.setBold(true);// 标题列样式titleStyle = workBook.createCellStyle();titleStyle.setBorderTop(BorderStyle.THIN); // 设置边框titleStyle.setBorderBottom(BorderStyle.THIN);titleStyle.setBorderLeft(BorderStyle.THIN);titleStyle.setBorderRight(BorderStyle.THIN);titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);titleStyle.setAlignment(HorizontalAlignment.CENTER);titleStyle.setFont(titleFont);// 内容列样式contentStyle = workBook.createCellStyle();contentStyle.setBorderTop(BorderStyle.THIN);contentStyle.setBorderBottom(BorderStyle.THIN);contentStyle.setBorderLeft(BorderStyle.THIN);contentStyle.setBorderRight(BorderStyle.THIN);titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);titleStyle.setAlignment(HorizontalAlignment.LEFT);}}
import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;public class RequestUtil {public static String getFileName(HttpServletRequest request, String outfilename)throws UnsupportedEncodingException {String agent = request.getHeader("USER-AGENT");String filename = null;if (null != agent) {if (-1 != agent.indexOf("MSIE") ||-1 != agent.indexOf("Trident") || -1 != agent.indexOf("Edge")) {// ie浏览器及Edge浏览器filename = java.net.URLEncoder.encode(outfilename, "UTF-8");filename = StringUtils.replace(filename, "+", "%20");// 替换空格}else if (null != agent && -1 != agent.indexOf("Mozilla")) {// 火狐,Chrome等浏览器filename = new String(outfilename.getBytes("UTF-8"), "iso-8859-1");}else {// IE7+filename = java.net.URLEncoder.encode(outfilename, "UTF-8");filename = StringUtils.replace(filename, "+", "%20");// 替换空格}} else {filename = outfilename;}return filename;}}
3.controller
@RequestMapping(value = "/downloadExcelFileMb", method = {RequestMethod.GET, RequestMethod.POST})public void downloadExcelFileMb(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = true) String tid) {XSSFWorkbook wb = new XSSFWorkbook();Write2007Excel we = new Write2007Excel();if (tid.equals(DownloadTemplate.PRODUCT_IMPORT.getTid())){String[] tops = DownloadTemplate.PRODUCT_IMPORT.getTitle().split(",");wb = we.addSheet(wb, tops, DownloadTemplate.PRODUCT_IMPORT.getName());OutputStream os = null;try {os = response.getOutputStream();response.setContentType("application/vnd.ms-excel");response.setHeader("Content-Disposition", "attachment; filename=\"" + RequestUtil.getFileName(request, DownloadTemplate.PRODUCT_IMPORT.getName()+"_模板.xlsx") + "\"");wb.write(os);os.flush();os.close();} catch (IOException e) {e.printStackTrace();}}}
4.前端调用
注:不要使用ajax调用。
function downTem(){location.href='/product/downloadExcelFileMb?tid=40287c5d748a904a01748b716a630101'}
5.测试
java:浏览器下载excel模板相关推荐
- Java Web下载Excel模板
在java web项目中常常会遇到下载本地模板的要求,这里以下载Excel模板为例进行说明. 一.首先要在自己的项目下存放一个Excel文件,如图: 这个文件的存放位置不是固定的,我们可以通过获取真实 ...
- Java用于下载Excel模板的接口(小白篇)
下载Excel模板的接口(最无脑的小白篇) try {//获取要下载的模板名称String fileName = "TemplateDate.xlsx";//设置头文件respon ...
- JAVA 浏览器下载excel,自定义样式:合并单元格,设置多种背景填充颜色,冻结窗格
添加依赖: <!-- POI --><dependency><groupId>org.apache.poi</groupId><artifactI ...
- Java后端下载EXCEL模板
Service层 package com.cbb.service; import java.util.Map; import javax.servlet.http.HttpServletRequest ...
- Java后台下载Excel模板并输出到浏览器上的样例
上代码! /*** 下载模板** @throws IOException*/@RequestMapping("/exportExcel")@ResponseBodypublic O ...
- java实现复制excel模板(多个工作表)并下载的代码
java实现复制excel模板(多个工作表)并下载的代码 项目心得 最近在开发项目的过程遇到一个需求需要把一个档案的信息导入到excel中,并实现下载功能,经过借鉴别人的代码,拍坑,历经磨难终于把这个 ...
- Java下载Excel模板文件的实现
在项目中经常会用到文件下载的功能,比如下载excel模板,这里简单记录一下实现过程 1.将模板文件放到项目资源文件目录中,也可以自定义其他位置,只要通过路径能找到该文件就行: 2.controller ...
- java 导入excel表格(批量导入),下载excel模板,导出表格
1.导入excel表格(批量导入) 如图,给id=83和id=84的老师导入工作时间. 导入的excel模板如下 注意导入excel表格时关于日期时间类的的数据要设置对应的格式. 后台接口 /*** ...
- springmvc下载excel模板示例代码
以下是通过springmvc下载项目中的excel模板文件示例代码: @ApiOperation("下载excel模板")@RequestMapping(value = " ...
最新文章
- 引用与传递——内存分析
- 利用BP神经网络教计算机识别语音特征信号(代码部分SS)
- 设计模式 — 创建型模式 — 建造者模式
- Springboot集成ES启动报错
- mysql数据库新浪博客_4.MySQL数据库类的定义
- WiFi穿透能力甩对手两堵墙 荣耀9X新特性令人侧目
- 二分法(三):采用二分法解决“最大化最小值问题”
- ffmpeg系列-视频旋转角度实现
- python1e2_Python-1 数据类型
- 【HTML5 4】《HTML5与CSS3权威指南》 step1 导读
- 计算机组成原理:计算机内负数二进制求得方式
- Gcc编译选项 -E
- linux go missing git command,go: missing Git command的解决办法
- C++ 中 ifstream读取txt文件内容
- Altium Designer
- 【用户端】家庭医生高保真Axure原型模板
- Java 提取PDF文档中的图片
- 软件测试运维工程师面试题,运维工程师笔试题目
- larvel html转pdf文件,如何在Laravel中使用TCPDF从HTML生成PDF
- RHCE 考试经验总结