Java如何導(dǎo)出多個(gè)excel并打包壓縮成.zip文件
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)識(shí) * @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)識(shí) * @param maps map數(shù)組 * @param tempDir 路徑 * @return String * @throws IOException 異常 */ public String getStoreOrderExcel(String flag, List<Map<String, Object>> maps, String tempDir) throws IOException { // 通過(guò)工具類(lèi)創(chuàng)建writer,默認(rèn)創(chuàng)建xls格式 ExcelWriter writer = ExcelUtil.getWriter(); if (flag.equals("worker")) { //自定義標(biāo)題別名 writer.addHeaderAlias("workerName", "姓名"); writer.addHeaderAlias("workerIdcard", "身份證號(hào)"); } else { //自定義標(biāo)題別名 writer.addHeaderAlias("workersiteName", "工地名稱(chēng)"); writer.addHeaderAlias("worksiteAddress", "工地地址"); } writer.write(maps, true); //生成一個(gè)excel String path = tempDir + LocalDate.now() + "_" + flag + ".xls"; //本地測(cè)試下載 FileOutputStream outputStream = new FileOutputStream(path); writer.flush(outputStream, true); writer.close(); return path; }
3、相關(guān)工具類(lèi)
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 + " 不存在文件,無(wú)法進(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(); //將文件夾添加到下一級(jí)打包目錄 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)行寫(xiě)操作 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)文章
Java讀取制表符文本轉(zhuǎn)換為JSON實(shí)現(xiàn)實(shí)例
在Java開(kāi)發(fā)中,處理各種數(shù)據(jù)格式是常見(jiàn)的任務(wù),本文將介紹如何使用Java讀取制表符文本文件,并將其轉(zhuǎn)換為JSON格式,以便于后續(xù)的數(shù)據(jù)處理和分析,我們將使用Java中的相關(guān)庫(kù)來(lái)實(shí)現(xiàn)這個(gè)過(guò)程,并提供詳細(xì)的代碼示例2024-01-01Java繼承方法重寫(xiě)實(shí)現(xiàn)原理及解析
這篇文章主要介紹了Java繼承方法重寫(xiě)實(shí)現(xiàn)原理及解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Java高并發(fā)系統(tǒng)限流算法的實(shí)現(xiàn)
這篇文章主要介紹了Java高并發(fā)系統(tǒng)限流算法的應(yīng)用,在開(kāi)發(fā)高并發(fā)系統(tǒng)時(shí)有三把利器用來(lái)保護(hù)系統(tǒng):緩存、降級(jí)和限流,限流可以認(rèn)為服務(wù)降級(jí)的一種,限流是對(duì)系統(tǒng)的一種保護(hù)措施,需要的朋友可以參考下2022-05-05SpringBoot使用Apache Tika實(shí)現(xiàn)多種文檔的內(nèi)容解析
在日常開(kāi)發(fā)中,我們經(jīng)常需要解析不同類(lèi)型的文檔,如PDF、Word、Excel、HTML、TXT等,Apache Tika是一個(gè)強(qiáng)大的內(nèi)容解析工具,可以輕松地提取文檔中的內(nèi)容和元數(shù)據(jù)信息,本文將通過(guò)SpringBoot和Apache Tika的結(jié)合,介紹如何實(shí)現(xiàn)對(duì)多種文檔格式的內(nèi)容解析2024-12-12Java網(wǎng)絡(luò)編程教程之設(shè)置請(qǐng)求超時(shí)的方法
這篇文章主要給大家介紹了關(guān)于Java網(wǎng)絡(luò)編程教程之設(shè)置請(qǐng)求超時(shí)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12通過(guò)MyBatis讀取數(shù)據(jù)庫(kù)數(shù)據(jù)并提供rest接口訪問(wèn)
這篇文章主要介紹了通過(guò)MyBatis讀取數(shù)據(jù)庫(kù)數(shù)據(jù)并提供rest接口訪問(wèn) 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08詳談Array和ArrayList的區(qū)別與聯(lián)系
下面小編就為大家?guī)?lái)一篇詳談Array和ArrayList的區(qū)別與聯(lián)系。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06詳解Java設(shè)計(jì)模式編程中命令模式的項(xiàng)目結(jié)構(gòu)實(shí)現(xiàn)
這篇文章主要介紹了Java設(shè)計(jì)模式編程中命令模式的項(xiàng)目結(jié)構(gòu)實(shí)現(xiàn),命令模式將請(qǐng)求與執(zhí)行分離,可以多個(gè)命令接口的實(shí)現(xiàn)類(lèi),隱藏真實(shí)的被調(diào)用方,需要的朋友可以參考下2016-04-04java實(shí)現(xiàn)的統(tǒng)計(jì)字符算法示例
這篇文章主要介紹了java實(shí)現(xiàn)的統(tǒng)計(jì)字符算法,涉及java針對(duì)字符的遍歷、判斷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10