根据实体类生成Excel压缩文件
<dependency>
<groupId>cn.ibaijia</groupId>
<artifactId>jsm-frame</artifactId>
<version>1.3.24</version>
</dependency>
public class Test {
@ExcelField(ignore = true)
public Long orderId;
@ExcelField(name = "订单编号",height = 14)
public String orderNo;
@ExcelField(name = "订单类型", replaceRule="大货|1;打样|2;变更|3",height = 14)
}
List<Test> dataList =new ArrayList<>();
Workbook workbook = ExcelUtil.writeToNew(dataList);
public class Test{
public static void main(String[] args) throws Exception {
FileOutputStream fos2 = new FileOutputStream(new File("E:\\DaiMa\\pod\\pod-api\\src\\main\\resources\\template\\test.zip"));
List<Map> list=new ArrayList<>();
List<BookPriceItemModel> bookPriceItemModels=new ArrayList<>();
Map<Object, Object> map = new HashMap<>();
BookPriceItemModel bookPriceItemModel = new BookPriceItemModel();
bookPriceItemModels.add(bookPriceItemModel);
bookPriceItemModel.name="十点多发";
map.put("name","表格");
map.put("data",bookPriceItemModels);
list.add(map);
ZipUtils.getEcselZip(list,fos2);
}
}
import com.hucai.rest.resp.BookPriceItemModel;
import com.jsmframe.excel.ExcelUtil;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private static final int BUFFER_SIZE = 2 * 1024;
public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure)
throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
File sourceFile = new File(srcDir);
compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void toInZip(List<Map> srcFiles, OutputStream out) throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
for (Map map : srcFiles) {
ByteArrayInputStream in = (ByteArrayInputStream) map.get("IN");
String name = (String) map.get("name");
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(name));
int len;
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static <T> void getEcselZip(List<Map> list, OutputStream fileOut ) throws RuntimeException {
List<Map> mapList = new ArrayList<Map>();
for (Map map : list) {
ByteArrayInputStream excelStream = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
List<T> data = (List<T>) map.get("data");
String name = (String) map.get("name")+".xlsx";
Workbook workbook = ExcelUtil.writeToNew(data);
try {
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
excelStream = new ByteArrayInputStream(out.toByteArray());
map.put("IN", excelStream);
map.put("name", name);
mapList.add(map);
}
toInZip(mapList, fileOut);
}
private static void compress(File sourceFile, ZipOutputStream zos, String name,
boolean KeepDirStructure) throws Exception {
byte[] buf = new byte[BUFFER_SIZE];
if (sourceFile.isFile()) {
zos.putNextEntry(new ZipEntry(name));
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
} else {
File[] listFiles = sourceFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
if (KeepDirStructure) {
zos.putNextEntry(new ZipEntry(name + "/"));
zos.closeEntry();
}
} else {
for (File file : listFiles) {
if (KeepDirStructure) {
compress(file, zos, name + "/" + file.getName(), KeepDirStructure);
} else {
compress(file, zos, file.getName(), KeepDirStructure);
}
}
}
}
}
public static void main(String[] args) throws Exception {
FileOutputStream fos2 = new FileOutputStream(new File("E:\\DaiMa\\pod\\pod-api\\src\\main\\resources\\template\\test.zip"));
List<Map> list=new ArrayList<>();
List<BookPriceItemModel> bookPriceItemModels=new ArrayList<>();
Map<Object, Object> map = new HashMap<>();
BookPriceItemModel bookPriceItemModel = new BookPriceItemModel();
bookPriceItemModels.add(bookPriceItemModel);
bookPriceItemModel.name="十点多发";
map.put("name","表格");
map.put("data",bookPriceItemModels);
list.add(map);
getEcselZip(list,fos2);
}
}