技术标签: java jxls http springBoot
模板放到指定目录(resources资源目录下)
导出数据:
代码如下(示例):
pom.xml
<!--jxls导出依赖jar包-->
<dependency>
<groupId>net.sf.jxls</groupId>
<artifactId>jxls-core</artifactId>
<version>1.0.6</version>
<scope>compile</scope>
</dependency>
<!--poi依赖jar包 使用3.17本版的包就对了相信我-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
ExcelUtiles
package com.src.common.excel;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.ResourceUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;
/**
* @ProjectName: Student
* @Package: cn.utils
*/
public class ExcelUtiles {
/**
* 输出表格
* @param map 表格中数据
* @param response 响应
* @param excelName 表格名称
* @param excelPath 表格模板保存的路径
*/
public static void outExcel(Map<String,Object> map,HttpServletResponse response,String excelName,String excelPath){
File file=null;
try {
file= ResourceUtils.getFile(excelPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//配置下载路径
String path = StaticUrl.getFileUrl()+"down\\";
createDir(new File(path));
//根据模板生成新的excel
File excelFile = createNewFile(map, file, path,excelName);
//浏览器端下载文件
try {
downloadFile(response, excelFile,excelName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//删除服务器生成文件
deleteFile(excelFile);
}
/**
* 根据excel模板生成新的excel
* @param beans 表格中的数据
* @param file 文件
* @param path 生成文件的位置
* @param excelName 文件名称
* @return
*/
private static File createNewFile(Map<String, Object> beans, File file, String path,String excelName) {
XLSTransformer transformer = new XLSTransformer();
File newFile = new File(path + excelName+".xls");
try (InputStream in = new BufferedInputStream(new FileInputStream(file));
OutputStream out = new FileOutputStream(newFile)) {
Workbook workbook = transformer.transformXLS(in, beans);
workbook.write(out);
out.flush();
return newFile;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return newFile;
}
/**
* 将服务器新生成的excel从浏览器下载
* @param response 响应
* @param excelFile 表格文件
* @param excelName 表格名称
* @throws UnsupportedEncodingException
*/
private static void downloadFile(HttpServletResponse response, File excelFile,String excelName) throws UnsupportedEncodingException {
/* 设置文件头:最后一个参数是设置下载文件名 */
response.setHeader("Content-type","application/vnd.ms-excel");
// 解决导出文件名中文乱码
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+new String(excelName.getBytes("UTF-8"),"ISO-8859-1")+".xls");
try (
InputStream ins = new FileInputStream(excelFile);
OutputStream os = response.getOutputStream()
) {
byte[] b = new byte[1024];
int len;
while ((len = ins.read(b)) > 0) {
os.write(b, 0, len);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* 浏览器下载完成之后删除服务器生成的文件
* 也可以设置定时任务去删除服务器文件
*
* @param excelFile
*/
private static void deleteFile(File excelFile) {
excelFile.delete();
}
//如果目录不存在创建目录 存在则不创建
private static void createDir(File file) {
if (!file.exists()) {
file.mkdirs();
}
}
}
StaticUrl
package com.src.common.excel;
import org.springframework.util.ResourceUtils;
import java.io.FileNotFoundException;
/**
* @ProjectName: ycbdqn
* @Package: cn.utils 上传文件路径
* @Author: huat
* @Date: 2020/3/5 13:39
* @Version: 1.0
*/
public class StaticUrl {
public static String getFileUrl(){
String systemPath =null;
try {
systemPath = ResourceUtils.getURL("classpath:").getPath().replace("%20", " ").replace('/', '\\').substring(1);;
/*//从路径字符串中取出工程路径
systemPath=systemPath.substring(0,systemPath.lastIndexOf("Student"));*/
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int result=systemPath.indexOf("\\");
if(-1!=result){
systemPath=systemPath.substring(result).replace("/","\\");
}
return systemPath+"static.excel\\"; //和模板resources下的文件名称一致
// return "E:\IDEA\file\";
}
}
Controller
@RequestMapping("downExcel")
public void downExecl(){
Map<String,Object> map=new HashMap<String,Object>();
map.put("name","学生信息表");
Student student=new Student(123,"张三");
Student student1=new Student(124,"李四");
Student student2=new Student(125,"王五");
List<Student> list=new ArrayList<>();
list.add(student);list.add(student1);list.add(student2);
map.put("studentList",list);
//获取学生信息
//"classpath:static/excel/学生表格.xls" 表格模板保存路径,classpath:代表resources路径
System.out.println(StaticUrl.getFileUrl()+"学生表格.xls");
ExcelUtiles.outExcel(map,response,"学生信息", StaticUrl.getFileUrl()+"学生表格.xls");
}
GitHub项目地址:https://github.com/HarmoniaLeo/MFC-QQbot0x00 这是什么?欢迎使用本QQ机器人开发框架(如果有人会用的话)。本框架是主要使用MFC中的Windows API制作而成、利用Windows的消息机制以及一些系统级底层架构实现利用TIM客户端自动收发QQ消息的QQ机器人框架,用其制作的QQ机器人程序已在VS201...
2019独角兽企业重金招聘Python工程师标准>>> ...
java 动态规划算法递归算法的时间复杂度=递归的次数递归函数本身的时间复杂度*
5V to 3V3通过AMS1117-3.3,将电压降至3V3,钽电容是必须的。蜂鸣器电路通过ss8050的NPN三极管实现对蜂鸣器的控制LED电路通过限流电阻对LED的电流和亮度进行控制。但每种颜色所配合的电阻还需实验,达到LED能亮同时又不晃眼睛的效果。...
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, mLmtdReptStr);adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);mSpinner.setAd
在执行roslaunch mbot_description arbotix_mbot_with_camera_xacro.launch文件时,即包含arbotix模块的程序时,报错No module named ‘rospkg’ 。报错No module named 'rospkg'经过对大家文章的查看,发现基本集中在把环境配置为python3.或者安装rospkg。但是仍没有解决。查看opt/ros/melodic/lib/python2.7/dist-packages,发现根本没有ro.
abaqus2016在线帮助文档因为比较简洁,打开响应速度较快,相对于需要注册的高版本帮助文档算是一大优点。但文档内容对于英文水平一般的同学不太友好,为了提高阅读效率隧寻找网页翻译的方法。1.浏览器右键的网页翻译,实测无效。2.大名鼎鼎的彩云小译扩展,实测无效。3.百度网页翻译扩展,实测无效。4.几个油猴网页翻译脚本,都实测无效:最后的解决办法:同样通过脚本的方式,依据该网页的框架划分翻译区域,从而实现理想的翻译效果。效果图:中英对照,对某些翻译不准确之处可以看原文,
要写这篇文章之前,其实自己已经准备了很多。但是和国外的技术标准一对比。我还是不献丑了…… 这篇文章主要是各种机房建设标准的汇总索引,没什么实质性的内容,只是希望引导大家对标准化机房建设有个初步的概念,了解一下国内外的差距。 真正想向机房设计方向发展的IT人员,建议仔细看一下Facebook的开源服务器和数据中心核心技术(The Open Compu...
// 利用nginx直接下载文件,提高效率 public function download_apk_efficient($internal_file_path,$file_name){ // And redirect user to internal location header("Content-Type: application/vn...
开篇身份认证,对于一个安全的应用来说,是第一道门槛,它为后续所有的安全措施提供了“身份”这样一个关键信息。通常每个公司会有好几个外部应用,如果每一个应用使用独立的账号体系,管理起来会非常复杂,用户也需要保存好几个账号密码,严重影响使用体验。而且公司内部的各个管理或开发系统,比如各种服务器、confluence、jira、gitlab等也都使用单独的账号,一个开发人员要记住各种纷繁杂乱的账号和密码,如果不提前保存下来,根本不可能能靠记忆完成输入。这时,如何设计一个简单易用的身份认证体系就显得尤为重要
命令行启动code如果你的系统是Linux系统(我使用的是Ubuntu 16.04)这样就可以直接使用 code + filename来编辑文件(就像vi + filename)如果你的系统是MacOS 就需要在vscode里面按 command + shift + p 之后输入 shell 基本上在第一提示里面就会显示安装code,如图所示转载于:https://www.cnblo...
默认写法就是转发:return的字符串将对应的前端页面转发到要求的url上配置了视图解析器的viewsresolver @RequestMapping("/hello/{a}/{b}") public String hello(@PathVariable int a, @PathVariable int b, Model model) { model.addAttribute("haha", "结果:" + (a + b)); return "hello"; }没有视图解析器