导出
一、excel格式导出至response
1、List<对象>导出
Map<String, String> headerAlias = new LinkedHashMap<String, String>();
headerAlias.put("数据列一对象英文名称", "显示的中文名称");
...
BigExcelWriter excelWriter = new BigExcelWriter();
excelWriter.setHeaderAlias(headerAlias);
excelWriter.write(list);
if (null != response) {
response.addHeader("Content-Type", "application/vnd.ms-excel;charset=UTF-8");
response.addHeader("Content-Disposition", excelWriter.getDisposition("excel表名", StandardCharsets.UTF_8));
}
excelWriter.flush(response.getOutputStream(), true);
excelWriter.close();
2、json对象不固定列导出
HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
List<List<String>> resultListList = new ArrayList<>();
List<String> resultHeadList = new ArrayList<>();
resultHeadList.add("NAME");
...
for (int j = 0; j < jsonObj.size(); j++) {
JSONArray jsonArray = jsonObj.getJSONArray("num" + j);
List<String> resultList = new ArrayList<>();
if (j == 0) {
resultList.add("固定列的相同取值");
...
} else {
resultList.add("");
...
}
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
String name = StrUtil.trimToEmpty(obj.getStr("name"));
String value = StrUtil.trimToEmpty(obj.getStr("value"));
if (j == 0) {
resultHeadList.add(name);
}
resultList.add(value);
}
resultListList.add(resultList);
}
resultListList.add(0, resultHeadList);
BigExcelWriter excelWriter = new BigExcelWriter();
excelWriter.write(resultListList);
if (null != response) {
response.addHeader("Content-Type", "application/vnd.ms-excel;charset=UTF-8");
response.addHeader("Content-Disposition", excelWriter.getDisposition("excel表名", StandardCharsets.UTF_8));
}
excelWriter.flush(response.getOutputStream(), true);
excelWriter.close();
二、压缩包格式导出至response
String[] fileUrlArray = Arrays.stream(StrUtil.trimToEmpty(request.getParameter("fileUrl")).replaceAll([,;,;:\\s]+, ",").replaceAll("^,|,$", "").split(","))
.filter(StrUtil::isNotBlank).distinct().toArray(String[]::new);
List<String> fileUrlList = new ArrayList<>(fileUrlArray.length);
Collections.addAll(fileUrlList, fileUrlArray);
List<String> suffixList = Arrays.stream(fileUrlArray).map(m -> m.substring(m.lastIndexOf("."))).collect(Collectors.toList());
Map<String, String> fileUrlSuffixMap = Arrays.stream(fileUrlArray).collect(Collectors.toMap(m -> m, m -> suffixList.get(fileUrlList.indexOf(m))));
String sku = StrUtil.trimToEmpty(request.getParameter("sku"));
String rootPath = request.getRealPath("/");
File file = new File(rootPath + "temp_download");
if (!file.exists()) {
file.mkdir();
}
Arrays.stream(fileUrlArray).forEach(m -> HttpUtil.downloadFile(m,FileUtil.file(new File(rootPath + "temp_download" +
File.separator + m.substring(m.lastIndexOf("/") + 1, m.lastIndexOf(".")) + fileUrlSuffixMap.get(m)))));
try {
File zipFile = null;
zipFile = new File(rootPath + "temp_download" + "/" + sku + ".zip");
FileOutputStream zipFos = new FileOutputStream(zipFile);
ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut;
for (int i = 0; i < fileUrlArray.length; i++) {
String ele = fileUrlArray[i];
File imageFile = new File(rootPath + "temp_download" + "/" + ele.substring(ele.lastIndexOf("/") + 1, ele.lastIndexOf(".")) + suffixList.get(i));
ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName());
zos.putArchiveEntry(zipEntry);
zos.write(FileUtils.readFileToByteArray(imageFile));
if (imageFile.exists()) {
imageFile.delete();
}
}
zos.closeArchiveEntry();
zos.flush();
zos.close();
OutputStream out = null;
out = response.getOutputStream();
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=" + new String((sku + ".zip").getBytes("GB2312"), "ISO-8859-1"));
response.setContentType("application/octet-stream; charset=utf-8");
response.setCharacterEncoding("UTF-8");
out.write(FileUtils.readFileToByteArray(zipFile));
out.flush();
out.close();
if (zipFile.exists()) {
zipFile.delete();
}
if (file.exists()) {
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
使用Hutool工具简化代码
String[] fileArray = fileStr.split(",");
try {
AtomicInteger atomicAdd = new AtomicInteger();
List<String> fileName = CollUtil.list(false);
InputStream[] ins = Arrays.stream(fileArray).map(url -> {
byte[] bytes = HttpUtil.downloadBytes(url);
fileName.add(url.substring(url.lastIndexOf("/") + 1));
atomicAdd.addAndGet(bytes.length);
return new ByteArrayInputStream(bytes);
}).toArray(InputStream[]::new);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(atomicAdd.intValue());
ZipUtil.zip(byteStream, fileName.toArray(new String[0]), ins);
response.setHeader("Content-Disposition", "attachment;filename=" + sku + ".zip");
response.setContentType("application/octet-stream; charset=utf-8");
OutputStream out = response.getOutputStream();
out.write(byteStream.toByteArray());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
导入
一、对象导入
excelList = excelReader.readAll(....class);
二、不固定列导入
if (file.isEmpty()) {
return failure("文件为空");
}
InputStream inputStream = null;
Boolean result = false;
List<Map<String,Object>> excelList = new ArrayList<>();
Obj obj = new Obj();
try {
inputStream = file.getInputStream();
ExcelReader excelReader = ExcelUtil.getReader(inputStream);
if (!"NAME".equals(excelReader.readRow(0).get(0))) {
return ...
}
excelList = excelReader.readAll();
obj.setName(excelList.get(0).get("NAME").toString());
JSONObject jsonObj = new JSONObject();
for (int i = 0; i < excelList.size(); i++) {
JSONArray jsonArray = new JSONArray();
Map<String, Object> oneObjMap = excelList.get(i);
for (Map.Entry<String, Object> entry : oneObjMap.entrySet()) {
JSONObject jsonObject = new JSONObject();
String key = entry.getKey();
Object value = entry.getValue();
if ("NAME".equals(key) || ...) {
continue;
}
jsonObject.set("name", key);
jsonObject.set("value", value);
jsonArray.add(jsonObject);
}
jsonObj.set("num" + i, jsonArray);
}
obj.setJsonObj(jsonObj.toString());
} catch (Exception e) {
e.printStackTrace();
}