java导出多个excel并打包压缩成.zip文件
1、先获取到数据
并将数据导出excel到指定位置
public void downpolicezip(worksitepoliceapiinfo worksitepoliceapiinfo) throws exception {
string zipname = "同步数据" + localdate.now() ;
list<map<string, object>> workermaps = new linkedlist<>();
list<map<string, object>> worksitemaps = new linkedlist<>();
map<string,object > map = new linkedhashmap<>();
//获取数据
.........
// string realpath = request.getsession().getservletcontext().getcontextpath();
//创建临时文件夹保存excel
string tempdir = zippath + "/tempdir/" + localdate.now() + "/";
//将导出的数据转成多个excel
list<file> files = this.getstoreorderexcels(tempdir, workermaps, worksitemaps, flag);
//下载zip
boolean tag = filedownloadutils.generatefile(tempdir, "zip", zippath, zipname);
//删除tempdir文件夹和其中的excel和zip文件
boolean b = filedownloadutils.deletedir(new file(zippath + "\\tempdir"));
if (!b) {
throw new runtimeexception("tempdir文件夹及其中的临时excel和zip文件删除失败");
}
}
2、将导出的数据转成多个excel
/**
* 将导出的数据转成多个excel
*
* @param tempdir 路径
* @param workermaps map集合
* @param worksitemaps map集合
* @param flag 标识
* @return list<file>
* @throws ioexception 异常
*/
private list<file> getstoreorderexcels(string tempdir, list<map<string, object>> workermaps, list<map<string, object>> worksitemaps, string[] flag) throws ioexception {
filedownloadutils.createfile(tempdir);
//存在多个文件
list<file> files = new arraylist<>();
string path;
for (int i = 0; i < flag.length; i++) {
if (flag[i].equals("worker")) {
path = this.getstoreorderexcel(flag[i], workermaps, tempdir);
} else {
path = this.getstoreorderexcel(flag[i], worksitemaps, tempdir);
}
//excel添加到files中
files.add(new file(path));
}
return files;
}
/**
* @param flag 标识
* @param maps map数组
* @param tempdir 路径
* @return string
* @throws ioexception 异常
*/
public string getstoreorderexcel(string flag, list<map<string, object>> maps, string tempdir) throws ioexception {
// 通过工具类创建writer,默认创建xls格式
excelwriter writer = excelutil.getwriter();
if (flag.equals("worker")) {
//自定义标题别名
writer.addheaderalias("workername", "姓名");
writer.addheaderalias("workeridcard", "身份证号");
} else {
//自定义标题别名
writer.addheaderalias("workersitename", "工地名称");
writer.addheaderalias("worksiteaddress", "工地地址");
}
writer.write(maps, true);
//生成一个excel
string path = tempdir + localdate.now() + "_" + flag + ".xls";
//本地测试下载
fileoutputstream outputstream = new fileoutputstream(path);
writer.flush(outputstream, true);
writer.close();
return path;
}
3、相关工具类
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.util.zip.zipentry;
import java.util.zip.zipoutputstream;
public class filedownloadutils {
/**
* 创建文件夹;
*
* @param path 路径
*/
public static void createfile(string path) {
file file = new file(path);
//判断文件是否存在;
if (!file.exists()) {
//创建文件;
file.mkdirs();
}
}
/**
* 删除文件夹及文件夹下所有文件
*
* @param dir 文件地址
* @return boolean
*/
public static boolean deletedir(file dir) {
if (dir == null || !dir.exists()) {
return true;
}
if (dir.isdirectory()) {
string[] children = dir.list();
//递归删除目录中的子目录下
for (string child : children) {
boolean success = deletedir(new file(dir, child));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
/**
* @description 将多个文件进行压缩到指定位置
* @param path 要压缩的文件路径
* @param format 生成的格式(zip、rar)
* @param zippath zip的路径
* @param zipname zip文件名
*/
public static boolean generatefile(string path, string format,string zippath,string zipname) throws exception {
file file = new file(path);
// 压缩文件的路径不存在
if (!file.exists()) {
throw new exception("路径 " + path + " 不存在文件,无法进行压缩...");
}
// 用于存放压缩文件的文件夹
string generatefile = zippath + file.separator ;
file compress = new file(generatefile);
// 如果文件夹不存在,进行创建
if( !compress.exists() ){
compress.mkdirs();
}
// 目的压缩文件
string generatefilename = compress.getabsolutepath() + file.separator + zipname + "." + format;
// 输出流
fileoutputstream outputstream = new fileoutputstream(generatefilename);
// 压缩输出流
zipoutputstream zipoutputstream = new zipoutputstream(new bufferedoutputstream(outputstream));
//压缩
generatefile(zipoutputstream,file,"");
system.out.println("源文件位置:" + file.getabsolutepath() + ",目的压缩文件生成位置:" + generatefilename);
// 关闭 输出流
zipoutputstream.close();
return true;
}
/**
* @param out 输出流
* @param file 目标文件
* @param dir 文件夹
* @throws exception
*/
private static void generatefile(zipoutputstream out, file file, string dir) throws exception {
// 当前的是文件夹,则进行一步处理
if (file.isdirectory()) {
//得到文件列表信息
file[] files = file.listfiles();
//将文件夹添加到下一级打包目录
out.putnextentry(new zipentry(dir + "/"));
dir = dir.length() == 0 ? "" : dir + "/";
//循环将文件夹中的文件打包
for (int i = 0; i < files.length; i++) {
generatefile(out, files[i], dir + files[i].getname());
}
} else { // 当前是文件
// 输入流
fileinputstream inputstream = new fileinputstream(file);
// 标记要打包的条目
out.putnextentry(new zipentry(dir));
// 进行写操作
int len = 0;
byte[] bytes = new byte[1024];
while ((len = inputstream.read(bytes)) > 0) {
out.write(bytes, 0, len);
}
// 关闭输入流
inputstream.close();
}
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论