Java exports multiple excels and packages and compresses them into .zip files
1. Get the data first
And export the data to the specified location
public void downPoliceZip(WorksitePoliceApiInfo worksitePoliceApiInfo) throws Exception { String zipName = "Synchronous data" + () ; List<Map<String, Object>> workerMaps = new LinkedList<>(); List<Map<String, Object>> worksiteMaps = new LinkedList<>(); Map<String,Object > map = new LinkedHashMap<>(); //Get data ......... // String realPath = ().getServletContext().getContextPath(); //Create temporary folder to save excel String tempDir = zipPath + "/tempDir/" + () + "/"; //Convert the exported data into multiple excel List<File> files = (tempDir, workerMaps, worksiteMaps, flag); //Download zip boolean tag = (tempDir, "zip", zipPath, zipName); //Delete the tempDir folder and the excel and zip files in it boolean b = (new File(zipPath + "\\tempDir")); if (!b) { throw new RuntimeException("Deletion of tempDir folder and temporary Excel and zip files in it failed"); } }
2. Convert the exported data into multiple excels
/** * Convert the exported data into multiple excels * * @param tempDir path * @param workerMaps map collection * @param worksiteMaps map collection * @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 { (tempDir); // Multiple files exist List<File> files = new ArrayList<>(); String path; for (int i = 0; i < ; i++) { if (flag[i].equals("worker")) { path = (flag[i], workerMaps, tempDir); } else { path = (flag[i], worksiteMaps, tempDir); } // Add excel to files (new File(path)); } return files; } /** * @param flag * @param maps map array * @param tempDir path * @return String * @throws IOException */ public String getStoreOrderExcel(String flag, List<Map<String, Object>> maps, String tempDir) throws IOException { // Create writer through tool class, create xls format by default ExcelWriter writer = (); if (("worker")) { //Custom title alias ("workerName", "Name"); ("workerIdcard", "Identity card number"); } else { //Custom title alias ("workersiteName", "Construction site name"); ("worksiteAddress", "Construction site address"); } (maps, true); // Generate an excel String path = tempDir + () + "_" + flag + ".xls"; //Local test download FileOutputStream outputStream = new FileOutputStream(path); (outputStream, true); (); return path; }
3. Related tools
import ; import ; import ; import ; import ; import ; public class FileDownloadUtils { /** * Create folder; * * @param path */ public static void createFile(String path) { File file = new File(path); //Judge whether the file exists; if (!()) { //Create a file; (); } } /** * Delete all files under folders and folders * * @param dir file address * @return boolean */ public static boolean deleteDir(File dir) { if (dir == null || !()) { return true; } if (()) { String[] children = (); //Recursively delete the subdirectory in the directory for (String child : children) { boolean success = deleteDir(new File(dir, child)); if (!success) { return false; } } } // The directory is empty at this time and can be deleted return (); } /** * @Description Compress multiple files to a specified location * @param path file path to compress * @param format generated format (zip, rar) * @param zipPath zip path * @param zipName zip file name */ public static boolean generateFile(String path, String format,String zipPath,String zipName) throws Exception { File file = new File(path); // The path to the compressed file does not exist if (!()) { throw new Exception("Path" + path + "The file does not exist, cannot be compressed..."); } // Folder for storing compressed files String generateFile = zipPath + ; File compress = new File(generateFile); // If the folder does not exist, create it if( !() ){ (); } // Purpose compressed file String generateFileName = () + + zipName + "." + format; // Output stream FileOutputStream outputStream = new FileOutputStream(generateFileName); // Compress output stream ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(outputStream)); //compression generateFile(zipOutputStream,file,""); ("Source file location:" + () + ", destination compression file generation location:" + generateFileName); // Close the output stream (); return true; } /** * @param out Output Stream * @param file Target file * @param dir folder * @throws Exception */ private static void generateFile(ZipOutputStream out, File file, String dir) throws Exception { // The current folder is a folder, then one-step process if (()) { //Get file list information File[] files = (); //Add folder to the next level packaging directory (new ZipEntry(dir + "/")); dir = () == 0 ? "" : dir + "/"; //Loop to package files in the folder for (int i = 0; i < ; i++) { generateFile(out, files[i], dir + files[i].getName()); } } else { // It's currently a file // Input stream FileInputStream inputStream = new FileInputStream(file); // Mark the entry to be packaged (new ZipEntry(dir)); // Perform write operations int len = 0; byte[] bytes = new byte[1024]; while ((len = (bytes)) > 0) { (bytes, 0, len); } // Close the input stream (); } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.