Java如何導(dǎo)出多個(gè)excel并打包壓縮成.zip文件
更新時(shí)間:2024年09月25日 11:45:54 作者:碼農(nóng)娟
本文介紹了Java如何導(dǎo)出多個(gè)excel文件并將這些文件打包壓縮成zip格式,首先,需要從數(shù)據(jù)庫中獲取數(shù)據(jù)并導(dǎo)出到指定位置形成excel文件,接著,將這些數(shù)據(jù)分散到不同的excel文件中,最后,使用相關(guān)的Java工具類對這些excel文件進(jìn)行打包壓縮
Java導(dǎo)出多個(gè)excel并打包壓縮成.zip文件
1、先獲取到數(shù)據(jù)
并將數(shù)據(jù)導(dǎo)出excel到指定位置
public void downPoliceZip(WorksitePoliceApiInfo worksitePoliceApiInfo) throws Exception {
String zipName = "同步數(shù)據(jù)" + LocalDate.now() ;
List<Map<String, Object>> workerMaps = new LinkedList<>();
List<Map<String, Object>> worksiteMaps = new LinkedList<>();
Map<String,Object > map = new LinkedHashMap<>();
//獲取數(shù)據(jù)
.........
// String realPath = request.getSession().getServletContext().getContextPath();
//創(chuàng)建臨時(shí)文件夾保存excel
String tempDir = zipPath + "/tempDir/" + LocalDate.now() + "/";
//將導(dǎo)出的數(shù)據(jù)轉(zhuǎn)成多個(gè)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文件夾及其中的臨時(shí)Excel和zip文件刪除失敗");
}
}
2、將導(dǎo)出的數(shù)據(jù)轉(zhuǎn)成多個(gè)excel
/**
* 將導(dǎo)出的數(shù)據(jù)轉(zhuǎn)成多個(gè)excel
*
* @param tempDir 路徑
* @param workerMaps map集合
* @param worksiteMaps map集合
* @param flag 標(biāo)識
* @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);
//存在多個(gè)文件
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 標(biāo)識
* @param maps map數(shù)組
* @param tempDir 路徑
* @return String
* @throws IOException 異常
*/
public String getStoreOrderExcel(String flag, List<Map<String, Object>> maps, String tempDir) throws IOException {
// 通過工具類創(chuàng)建writer,默認(rèn)創(chuàng)建xls格式
ExcelWriter writer = ExcelUtil.getWriter();
if (flag.equals("worker")) {
//自定義標(biāo)題別名
writer.addHeaderAlias("workerName", "姓名");
writer.addHeaderAlias("workerIdcard", "身份證號");
} else {
//自定義標(biāo)題別名
writer.addHeaderAlias("workersiteName", "工地名稱");
writer.addHeaderAlias("worksiteAddress", "工地地址");
}
writer.write(maps, true);
//生成一個(gè)excel
String path = tempDir + LocalDate.now() + "_" + flag + ".xls";
//本地測試下載
FileOutputStream outputStream = new FileOutputStream(path);
writer.flush(outputStream, true);
writer.close();
return path;
}
3、相關(guān)工具類
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 {
/**
* 創(chuàng)建文件夾;
*
* @param path 路徑
*/
public static void createFile(String path) {
File file = new File(path);
//判斷文件是否存在;
if (!file.exists()) {
//創(chuàng)建文件;
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;
}
}
}
// 目錄此時(shí)為空,可以刪除
return dir.delete();
}
/**
* @Description 將多個(gè)文件進(jìn)行壓縮到指定位置
* @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 + " 不存在文件,無法進(jìn)行壓縮...");
}
// 用于存放壓縮文件的文件夾
String generateFile = zipPath + File.separator ;
File compress = new File(generateFile);
// 如果文件夾不存在,進(jìn)行創(chuàng)建
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);
// 關(guān)閉 輸出流
zipOutputStream.close();
return true;
}
/**
* @param out 輸出流
* @param file 目標(biāo)文件
* @param dir 文件夾
* @throws Exception
*/
private static void generateFile(ZipOutputStream out, File file, String dir) throws Exception {
// 當(dāng)前的是文件夾,則進(jìn)行一步處理
if (file.isDirectory()) {
//得到文件列表信息
File[] files = file.listFiles();
//將文件夾添加到下一級打包目錄
out.putNextEntry(new ZipEntry(dir + "/"));
dir = dir.length() == 0 ? "" : dir + "/";
//循環(huán)將文件夾中的文件打包
for (int i = 0; i < files.length; i++) {
generateFile(out, files[i], dir + files[i].getName());
}
} else { // 當(dāng)前是文件
// 輸入流
FileInputStream inputStream = new FileInputStream(file);
// 標(biāo)記要打包的條目
out.putNextEntry(new ZipEntry(dir));
// 進(jìn)行寫操作
int len = 0;
byte[] bytes = new byte[1024];
while ((len = inputStream.read(bytes)) > 0) {
out.write(bytes, 0, len);
}
// 關(guān)閉輸入流
inputStream.close();
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中@Valid對List校驗(yàn)失效問題的有效解決方法
在Spring Boot應(yīng)用開發(fā)中,我們經(jīng)常需要對傳入的請求參數(shù)進(jìn)行校驗(yàn),以確保數(shù)據(jù)的合法性和安全性,然而,當(dāng)我們嘗試對列表(List)類型的參數(shù)進(jìn)行校驗(yàn)時(shí),可能會遇到校驗(yàn)失效的問題,本文將詳細(xì)探討這一問題的失效原因,并提供有效的解決方法,需要的朋友可以參考下2025-07-07
SpringBoot?使用?spring.profiles.active?來區(qū)分不同環(huán)境配置實(shí)現(xiàn)
很多時(shí)候,我們項(xiàng)目在開發(fā)環(huán)境和生產(chǎn)環(huán)境的配置是不一樣的,例如,數(shù)據(jù)庫配置,在開發(fā)的時(shí)候,我們一般用測試數(shù)據(jù)庫,而在生產(chǎn)環(huán)境,我們要用生產(chǎn)數(shù)據(jù)庫,這時(shí)候,我們可以利用?profile?在不同的環(huán)境下配置用不同的配置文件或者不同的配置,感興趣的可以了解一下2026-04-04
如何用Java設(shè)計(jì)一個(gè)隨機(jī)驗(yàn)證碼生成器
在信息技術(shù)領(lǐng)域,驗(yàn)證碼是一種常見的安全機(jī)制,用于防止自動(dòng)化的機(jī)器人程序或惡意攻擊者進(jìn)行非法操作,這篇文章主要介紹了如何用Java設(shè)計(jì)一個(gè)隨機(jī)驗(yàn)證碼生成器的相關(guān)資料,需要的朋友可以參考下2026-01-01

