DownloadServiceImpl.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. package com.caimei.service.auth.impl;
  2. import com.aliyuncs.utils.StringUtils;
  3. import com.caimei.mapper.cmMapper.AuthMapper;
  4. import com.caimei.mapper.cmMapper.AuthProductMapper;
  5. import com.caimei.mapper.cmMapper.FileMapper;
  6. import com.caimei.model.ResponseJson;
  7. import com.caimei.model.po.ProductImagePo;
  8. import com.caimei.model.po.ProductParamPo;
  9. import com.caimei.model.po.UploadFilePo;
  10. import com.caimei.model.vo.AuthFormVo;
  11. import com.caimei.model.vo.AuthVo;
  12. import com.caimei.model.vo.ClubUserVo;
  13. import com.caimei.model.vo.ProductFormVo;
  14. import com.caimei.service.auth.DownloadService;
  15. import com.caimei.service.auth.UploadService;
  16. import com.caimei.utils.FileIOUtils;
  17. import com.caimei.utils.OSSUtils;
  18. import com.caimei.utils.PoiUtils;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.poi.hssf.usermodel.HSSFCell;
  21. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  22. import org.apache.poi.ss.usermodel.CellType;
  23. import org.apache.poi.xssf.usermodel.*;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.stereotype.Service;
  27. import javax.annotation.Resource;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletResponse;
  30. import java.io.*;
  31. import java.net.URLEncoder;
  32. import java.nio.charset.StandardCharsets;
  33. import java.text.SimpleDateFormat;
  34. import java.util.*;
  35. import java.util.concurrent.atomic.AtomicInteger;
  36. /**
  37. * Description
  38. *
  39. * @author : Aslee
  40. * @date : 2021/7/9
  41. */
  42. @Slf4j
  43. @Service
  44. public class DownloadServiceImpl implements DownloadService {
  45. @Resource
  46. private FileMapper fileMapper;
  47. @Resource
  48. private AuthMapper authMapper;
  49. @Resource
  50. private AuthProductMapper authProductMapper;
  51. private UploadService uploadService;
  52. @Value("${caimei.oldapi}")
  53. private String wwwServer;
  54. @Value("${caimei.zpapi}")
  55. private String zpServer;
  56. @Value("${spring.profiles.active}")
  57. private String active;
  58. @Autowired
  59. public void setUploadService(UploadService uploadService) {
  60. this.uploadService = uploadService;
  61. }
  62. @Override
  63. public void downloadFile(String ossName, String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException {
  64. OSSUtils.downFile("authFile/", ossName, fileName);
  65. download(request, response, "./" + fileName, fileName);
  66. }
  67. @Override
  68. public void downloadShopImage(String authIds, Integer type, HttpServletRequest request, HttpServletResponse response) throws Exception {
  69. ArrayList<UploadFilePo> fileList = new ArrayList<>();
  70. HashMap<String, List<UploadFilePo>> fileMap = new HashMap<>();
  71. List<AuthVo> authImageList = fileMapper.getAuthImageList(authIds);
  72. if (1 == type) {
  73. // 机构授权牌
  74. List<String> fileNameList = new ArrayList<>();
  75. authImageList.forEach(authImage ->{
  76. if (StringUtils.isNotEmpty(authImage.getAuthImage())) {
  77. UploadFilePo file = new UploadFilePo();
  78. String fileName = authImage.getAuthParty();
  79. int k = 1;
  80. while (fileNameList.contains(fileName)) {
  81. fileName = fileName.replace("(" + (k-1) + ")", "") + "(" + k + ")";
  82. k++;
  83. }
  84. fileNameList.add(fileName);
  85. file.setFileUrl(authImage.getAuthImage());
  86. file.setFileName(fileName);
  87. fileList.add(file);
  88. }
  89. });
  90. if (fileList.size() > 0) {
  91. fileMap.put("机构授权牌", fileList);
  92. }
  93. } else if (2 == type) {
  94. // 设备授权牌
  95. authImageList.forEach(authImage->{
  96. List<ProductImagePo> productList = fileMapper.getProductImageList(authImage.getAuthId());
  97. ArrayList<UploadFilePo> list = new ArrayList<>();
  98. productList.forEach(product->{
  99. if (StringUtils.isNotEmpty(product.getCertificateImage())) {
  100. UploadFilePo file = new UploadFilePo();
  101. file.setFileUrl(product.getCertificateImage());
  102. file.setFileName(product.getProductName() + "-" + product.getSnCode());
  103. list.add(file);
  104. }
  105. });
  106. if (list.size() > 0) {
  107. fileMap.put(authImage.getAuthParty(), list);
  108. }
  109. });
  110. } else if (3 == type) {
  111. // 机构二维码
  112. List<String> fileNameList = new ArrayList<>();
  113. authImageList.forEach(authImage -> {
  114. String qrCodeLink = zpServer + "/" + authImage.getAuthUserId() + "/app/approve/club/detail?id=" + authImage.getAuthId();
  115. String fileName = authImage.getAuthParty();
  116. int k = 1;
  117. while (fileNameList.contains(fileName)) {
  118. fileName = fileName.replace("(" + (k-1) + ")", "") + "(" + k + ")";
  119. k++;
  120. }
  121. fileNameList.add(fileName);
  122. UploadFilePo file = new UploadFilePo();
  123. file.setFileUrl(qrCodeLink);
  124. file.setFileName(authImage.getAuthParty());
  125. fileList.add(file);
  126. // String imagePath = createBufferedImageFile(qrCodeImage, fileName, "png");
  127. });
  128. if (fileList.size() > 0) {
  129. fileMap.put("机构二维码", fileList);
  130. }
  131. } else {
  132. // 设备二维码
  133. authImageList.forEach(authImage->{
  134. List<ProductImagePo> productList = fileMapper.getProductImageList(authImage.getAuthId());
  135. ArrayList<UploadFilePo> list = new ArrayList<>();
  136. productList.forEach(product->{
  137. String qrCodeLink = wwwServer + "/product/auth/product-" + product.getProductId() + ".html";
  138. UploadFilePo file = new UploadFilePo();
  139. file.setFileUrl(qrCodeLink);
  140. file.setFileName(product.getProductName() + "-" + product.getSnCode());
  141. list.add(file);
  142. });
  143. if (list.size() > 0) {
  144. fileMap.put(authImage.getAuthParty(), list);
  145. }
  146. });
  147. }
  148. String fileName = 1 == type ? "机构授权牌" : 2 == type ? "设备授权牌" : 3 == type ? "机构二维码" : "设备二维码";
  149. // 将授权牌压缩成zip文件
  150. String imageZipPath = uploadService.createImageZip(fileMap, type);
  151. download(request, response, imageZipPath, fileName + ".zip");
  152. // 删除临时文件夹
  153. String tempPath = imageZipPath.substring(0, imageZipPath.lastIndexOf("/"));
  154. File tempFile = new File(tempPath);
  155. boolean delete = tempFile.delete();
  156. log.info("【图片压缩包】,删除图片压缩包文件夹:" + delete);
  157. }
  158. @Override
  159. public ResponseJson downloadAuthData(String authIds, HttpServletResponse response) {
  160. try {
  161. // 导出表格名
  162. String fileName = new String("机构信息.xlsx".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
  163. // 机构数据
  164. List<AuthFormVo> authPartyList = authMapper.getAuthPartyList(authIds);
  165. OutputStream outputStream = response.getOutputStream();
  166. response.reset();
  167. response.setHeader("Access-Control-Allow-Origin", "*");
  168. response.setHeader("Access-Control-Allow-Credentials", "true");
  169. response.setHeader("Content-disposition",
  170. "attachment; filename="+fileName);
  171. // response.setContentType("application/vnd.ms-excel"); -- xls
  172. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  173. return generateAuthExcel(authPartyList, outputStream);
  174. } catch (Exception e) {
  175. e.printStackTrace();
  176. return ResponseJson.error("导出失败");
  177. }
  178. }
  179. @Override
  180. public ResponseJson downloadProductData(String authIds, HttpServletRequest request, HttpServletResponse response) throws IOException {
  181. HashMap<AuthVo, List<ProductFormVo>> fileMap = new HashMap<>();
  182. // 机构列表
  183. List<AuthVo> authImageList = fileMapper.getAuthImageList(authIds);
  184. // 设备列表
  185. authImageList.forEach(authImage->{
  186. final Integer[] maxParamNum = {0};
  187. List<ProductFormVo> productList = authProductMapper.getAuthProductList(authImage.getAuthId());
  188. productList.forEach(product->{
  189. // 参数列表
  190. List<ProductParamPo> paramList = new ArrayList<>();
  191. if (null == product.getProductTypeId()) {
  192. paramList = authProductMapper.getParamsByProductId(product.getProductId());
  193. } else {
  194. paramList = authProductMapper.getProductTypeParamList(product.getProductTypeId());
  195. }
  196. product.setParamList(paramList);
  197. if (paramList.size() > maxParamNum[0]) {
  198. maxParamNum[0] = paramList.size();
  199. }
  200. });
  201. authImage.setMaxParamNum(maxParamNum[0]);
  202. if (productList.size() > 0) {
  203. fileMap.put(authImage, productList);
  204. }
  205. });
  206. // 将授权牌压缩成zip文件
  207. String imageZipPath = createExcelZip(fileMap);
  208. download(request, response, imageZipPath, "设备信息.zip");
  209. // 删除临时文件夹
  210. String tempPath = imageZipPath.substring(0, imageZipPath.lastIndexOf("/"));
  211. File tempFile = new File(tempPath);
  212. boolean delete = tempFile.delete();
  213. log.info("【图片压缩包】,删除图片压缩包文件夹:" + delete);
  214. return null;
  215. }
  216. @Override
  217. public void downloadAuthTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
  218. String ossName = "认证通机构导入模板.xlsx";
  219. OSSUtils.downFile("authFile/", ossName, ossName);
  220. download(request, response, "./" + ossName, ossName);
  221. }
  222. private String createExcelZip(HashMap<AuthVo, List<ProductFormVo>> authMap) throws FileNotFoundException {
  223. String randomStr = UUID.randomUUID().toString();
  224. String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempImage/";
  225. if ("dev".equals(active)){
  226. filePath = "D:\\uploadZip\\";
  227. }
  228. // 压缩文件夹上层临时文件夹路径
  229. String tempPath = filePath + randomStr;
  230. // 压缩文件夹路径
  231. String basePath = "";
  232. // 生成唯一临时文件夹
  233. filePath += randomStr + "/";
  234. // 在临时文件夹中生成要压缩的基础文件夹
  235. String baseDirName = "设备信息";
  236. filePath += baseDirName;
  237. basePath = filePath;
  238. File fileDir = new File(filePath);
  239. if (!fileDir.exists()) {
  240. fileDir.mkdirs();
  241. }
  242. // 往基础文件夹中填充相应数据
  243. for (Map.Entry<AuthVo, List<ProductFormVo>> entry : authMap.entrySet()) {
  244. AuthVo auth = entry.getKey();
  245. List<ProductFormVo> productList = entry.getValue();
  246. generateProductExcel(filePath, auth, productList);
  247. }
  248. // 将文件夹转为压缩文件
  249. String zipPath = tempPath + "/" + baseDirName + ".zip";
  250. FileOutputStream zipOut = new FileOutputStream(zipPath);
  251. FileIOUtils.zipFolder(basePath, zipOut, true);
  252. return zipPath;
  253. }
  254. private void generateProductExcel(String tempFilePath, AuthVo authParty, List<ProductFormVo> productList) {
  255. String fileName = authParty.getAuthParty();
  256. String filePath = tempFilePath + "/" + fileName + ".xlsx";
  257. try {
  258. FileOutputStream outputStream = new FileOutputStream(filePath);
  259. XSSFWorkbook workbook = new XSSFWorkbook();
  260. // 创建设备工作表
  261. XSSFSheet productSheet = workbook.createSheet("设备信息");
  262. // sheet样式定义
  263. XSSFCellStyle topCellStyle = PoiUtils.getXSSFTopCellStyle(workbook);
  264. XSSFCellStyle customCellStyle = PoiUtils.getXSSFCustomCellStyle(workbook);
  265. // 创建列头行
  266. XSSFRow authTopRow = productSheet.createRow(0);
  267. // 添加列头单元格(4个固定列+参数列表)
  268. String[] rowName = {"设备名称", "设备SN码", "所属品牌", "购买渠道",
  269. "参数名1", "参数值1", "参数名2", "参数值2", "参数名3", "参数值3", "参数名4", "参数值4", "参数名5",
  270. "参数值5", "参数名6", "参数值6", "参数名7", "参数值7", "参数名8", "参数值8", "参数名9", "参数值9",
  271. "参数名10", "参数值10", "参数名11", "参数值11", "参数名12", "参数值12"};
  272. for (int i = 0; i < 4 + (0 == authParty.getMaxParamNum() ? 4 : authParty.getMaxParamNum()) * 2; i++) {
  273. XSSFCell authTopRowCell = authTopRow.createCell(i);
  274. authTopRowCell.setCellType(CellType.valueOf("STRING"));
  275. XSSFRichTextString columnText = new XSSFRichTextString(rowName[i]);
  276. authTopRowCell.setCellValue(columnText);
  277. authTopRowCell.setCellStyle(topCellStyle);
  278. }
  279. // 行索引
  280. AtomicInteger productRowNum = new AtomicInteger(1);
  281. productList.forEach(product->{
  282. // 创建设备行
  283. XSSFRow productRow = productSheet.createRow(productRowNum.get());
  284. // 参数列表
  285. List<ProductParamPo> paramList = product.getParamList();
  286. // 组装设备数据
  287. List<String> productData = new ArrayList<>();
  288. productData.add(product.getProductName());
  289. productData.add(product.getSnCode());
  290. productData.add(product.getBrandName());
  291. productData.add(product.getPurchaseWay());
  292. paramList.forEach(param -> {
  293. productData.add(param.getParamName());
  294. productData.add(param.getParamContent());
  295. });
  296. for (int j = 0; j < 4 + paramList.size() * 2; j++) {
  297. XSSFCell productCell = productRow.createCell(j);
  298. productCell.setCellType(CellType.valueOf("STRING"));
  299. XSSFRichTextString productCellValue = new XSSFRichTextString(productData.get(j));
  300. productCell.setCellValue(productCellValue);
  301. productCell.setCellStyle(customCellStyle);
  302. }
  303. productRowNum.getAndIncrement();
  304. });
  305. // 让列宽随着导出的列长自动适应
  306. for (int colNum = 0; colNum < rowName.length; colNum++) {
  307. int productColumnWidth = productSheet.getColumnWidth(colNum) / 256;
  308. for (int rowNum = 0; rowNum < productSheet.getLastRowNum(); rowNum++) {
  309. XSSFRow currentRow;
  310. // 当前行未被使用过
  311. if (productSheet.getRow(rowNum) == null) {
  312. currentRow = productSheet.createRow(rowNum);
  313. } else {
  314. currentRow = productSheet.getRow(rowNum);
  315. }
  316. if (currentRow.getCell(colNum) != null) {
  317. XSSFCell currentCell = currentRow.getCell(colNum);
  318. if (currentCell.getCellTypeEnum().equals(CellType.valueOf("STRING"))) {
  319. int length = currentCell.getStringCellValue()
  320. .getBytes().length;
  321. if (productColumnWidth < length) {
  322. productColumnWidth = length;
  323. }
  324. }
  325. }
  326. }
  327. productSheet.setColumnWidth(colNum, (productColumnWidth + 4) * 256);
  328. }
  329. workbook.write(outputStream);
  330. } catch (Exception e) {
  331. e.printStackTrace();
  332. }
  333. }
  334. private ResponseJson generateAuthExcel(List<AuthFormVo> authPartyList, OutputStream outputStream) {
  335. try {
  336. XSSFWorkbook workbook = new XSSFWorkbook();
  337. // 创建机构工作表
  338. XSSFSheet authPartySheet = workbook.createSheet("授权机构");
  339. // sheet样式定义
  340. XSSFCellStyle topCellStyle = PoiUtils.getXSSFTopCellStyle(workbook);
  341. XSSFCellStyle customCellStyle = PoiUtils.getXSSFCustomCellStyle(workbook);
  342. // 创建列头行
  343. XSSFRow authTopRow = authPartySheet.createRow(0);
  344. // 添加列头单元格
  345. String[] rowName = {"机构名称(必填)", "所在地区(必填)", "详细地址(必填)", "经纬度(选填)",
  346. "联系电话(必填)", "认证编号(选填)", "认证日期(选填)", "员工人数(必填)", "店铺备注(选填)"};
  347. for (int i = 0; i < rowName.length; i++) {
  348. XSSFCell authTopRowCell = authTopRow.createCell(i);
  349. authTopRowCell.setCellType(CellType.valueOf("STRING"));
  350. XSSFRichTextString columnText = new XSSFRichTextString(rowName[i]);
  351. authTopRowCell.setCellValue(columnText);
  352. authTopRowCell.setCellStyle(topCellStyle);
  353. }
  354. // 行索引
  355. AtomicInteger authPartyRowNum = new AtomicInteger(1);
  356. authPartyList.forEach(authParty -> {
  357. // 创建机构行
  358. XSSFRow authRow = authPartySheet.createRow(authPartyRowNum.get());
  359. // 组装机构数据
  360. List<String> authData = new ArrayList<>();
  361. authData.add(authParty.getAuthParty());
  362. authData.add(authParty.getArea());
  363. authData.add(authParty.getAddress());
  364. authData.add(authParty.getLngAndLat());
  365. authData.add(authParty.getMobile());
  366. authData.add(authParty.getAuthCode());
  367. SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
  368. Date authDate = authParty.getAuthDate();
  369. if (null != authDate) {
  370. authData.add(format.format(authDate));
  371. } else {
  372. authData.add("");
  373. }
  374. authData.add(authParty.getEmpNum().toString());
  375. authData.add(authParty.getRemarks());
  376. for (int j = 0; j < authData.size(); j++) {
  377. XSSFCell authCell = authRow.createCell(j);
  378. authCell.setCellStyle(customCellStyle);
  379. if (j == 7) {
  380. authCell.setCellType(CellType.NUMERIC);
  381. authCell.setCellValue(Integer.parseInt(authData.get(j)));
  382. } else {
  383. authCell.setCellType(CellType.valueOf("STRING"));
  384. XSSFRichTextString authCellValue = new XSSFRichTextString(authData.get(j));
  385. authCell.setCellValue(authCellValue);
  386. }
  387. }
  388. authPartyRowNum.getAndIncrement();
  389. });
  390. // 让列宽随着导出的列长自动适应
  391. for (int colNum = 0; colNum < rowName.length; colNum++) {
  392. int productColumnWidth = authPartySheet.getColumnWidth(colNum) / 256;
  393. for (int rowNum = 0; rowNum < authPartySheet.getLastRowNum(); rowNum++) {
  394. XSSFRow currentRow;
  395. // 当前行未被使用过
  396. if (authPartySheet.getRow(rowNum) == null) {
  397. currentRow = authPartySheet.createRow(rowNum);
  398. } else {
  399. currentRow = authPartySheet.getRow(rowNum);
  400. }
  401. if (currentRow.getCell(colNum) != null) {
  402. XSSFCell currentCell = currentRow.getCell(colNum);
  403. if (currentCell.getCellTypeEnum().equals(CellType.valueOf("STRING"))) {
  404. int length = currentCell.getStringCellValue()
  405. .getBytes().length;
  406. if (productColumnWidth < length) {
  407. productColumnWidth = length;
  408. }
  409. }
  410. }
  411. }
  412. authPartySheet.setColumnWidth(colNum, (productColumnWidth + 4) * 256);
  413. }
  414. outputStream.flush();
  415. workbook.write(outputStream);
  416. outputStream.close();
  417. } catch (Exception e) {
  418. e.printStackTrace();
  419. return ResponseJson.error("导出失败");
  420. }
  421. return null;
  422. }
  423. public void download(HttpServletRequest request, HttpServletResponse response, String filePath, String fileName) throws IOException {
  424. File file = new File(filePath);
  425. // 文件存在才下载
  426. if (file.exists()) {
  427. OutputStream out = null;
  428. FileInputStream in = null;
  429. try {
  430. // 1.读取要下载的内容
  431. in = new FileInputStream(file);
  432. // 2. 告诉浏览器下载的方式以及一些设置
  433. // 解决文件名乱码问题,获取浏览器类型,转换对应文件名编码格式,IE要求文件名必须是utf-8, firefo要求是iso-8859-1编码
  434. String agent = request.getHeader("user-agent");
  435. if (agent.contains("FireFox")) {
  436. fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
  437. } else {
  438. fileName = URLEncoder.encode(fileName, "UTF-8");
  439. }
  440. // 设置下载文件的mineType,告诉浏览器下载文件类型
  441. String mineType = request.getServletContext().getMimeType(fileName);
  442. response.setContentType(mineType);
  443. // 设置一个响应头,无论是否被浏览器解析,都下载
  444. response.setHeader("Content-disposition", "attachment; filename=" + fileName);
  445. // 将要下载的文件内容通过输出流写到浏览器
  446. out = response.getOutputStream();
  447. int len = 0;
  448. byte[] buffer = new byte[1024];
  449. while ((len = in.read(buffer)) > 0) {
  450. out.write(buffer, 0, len);
  451. }
  452. } catch (IOException e) {
  453. e.printStackTrace();
  454. } finally {
  455. if (out != null) {
  456. out.close();
  457. }
  458. if (in != null) {
  459. in.close();
  460. }
  461. file.delete();
  462. }
  463. }
  464. }
  465. /**
  466. * 下载用户信息
  467. * @param clubUserIds
  468. * @param response
  469. * @return
  470. */
  471. @Override
  472. public ResponseJson downloadClubuserlistInfo(String clubUserIds, HttpServletResponse response) {
  473. //导出的用户表名
  474. String fileName=new String("用户信息.xlsx".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
  475. //获取用户信息
  476. List<ClubUserVo> clubUserVosList = authMapper.getclubuserInfo(clubUserIds);
  477. try {
  478. OutputStream outputStream = response.getOutputStream();
  479. response.reset();
  480. response.setHeader("Access-Control-Allow-Origin", "*");
  481. response.setHeader("Access-Control-Allow-Credentials", "true");
  482. response.setHeader("Content-disposition",
  483. "attachment; filename="+fileName);
  484. // response.setContentType("application/vnd.ms-excel"); -- xls
  485. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  486. return generateClubUserExcel(clubUserVosList,outputStream);
  487. } catch (IOException e) {
  488. e.printStackTrace();
  489. return ResponseJson.error("导出失败");
  490. }
  491. }
  492. private ResponseJson generateClubUserExcel(List<ClubUserVo> clubUserVosList, OutputStream outputStream) {
  493. try {
  494. XSSFWorkbook workbook = new XSSFWorkbook();
  495. // 创建机构工作表
  496. XSSFSheet authPartySheet = workbook.createSheet("用户信息");
  497. // sheet样式定义
  498. XSSFCellStyle topCellStyle = PoiUtils.getXSSFTopCellStyle(workbook);
  499. XSSFCellStyle customCellStyle = PoiUtils.getXSSFCustomCellStyle(workbook);
  500. // 创建列头行
  501. XSSFRow authTopRow = authPartySheet.createRow(0);
  502. // 添加列头单元格
  503. String[] rowName = {"序号", "机构名称", "手机号", "注册时间",
  504. "认证状态", "用户状态"};
  505. for (int i = 0; i < rowName.length; i++) {
  506. XSSFCell authTopRowCell = authTopRow.createCell(i);
  507. authTopRowCell.setCellType(CellType.valueOf("STRING"));
  508. XSSFRichTextString columnText = new XSSFRichTextString(rowName[i]);
  509. authTopRowCell.setCellValue(columnText);
  510. authTopRowCell.setCellStyle(topCellStyle);
  511. }
  512. // 行索引
  513. AtomicInteger authPartyRowNum = new AtomicInteger(1);
  514. int serialNum=1;
  515. // clubUserVosList.forEach(club -> {
  516. for (ClubUserVo club:clubUserVosList) {
  517. // 创建机构行
  518. XSSFRow authRow = authPartySheet.createRow(authPartyRowNum.get());
  519. // 组装机构数据
  520. List<String> authData = new ArrayList<>();
  521. authData.add(serialNum + "");
  522. authData.add(club.getName());
  523. authData.add(club.getMobile());
  524. SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm");
  525. Date authDate = club.getAddTime();
  526. if (null != authDate) {
  527. authData.add(format.format(authDate));
  528. } else {
  529. authData.add("");
  530. }
  531. authData.add(club.getAuthenticationStatus());
  532. authData.add(club.getStatus() + "");
  533. for (int j = 0; j < authData.size(); j++) {
  534. XSSFCell authCell = authRow.createCell(j);
  535. authCell.setCellStyle(customCellStyle);
  536. // if (j == 7) {
  537. // authCell.setCellType(CellType.NUMERIC);
  538. // authCell.setCellValue(Integer.parseInt(authData.get(j)));
  539. // } else {
  540. authCell.setCellType(CellType.valueOf("STRING"));
  541. XSSFRichTextString authCellValue = new XSSFRichTextString(authData.get(j));
  542. authCell.setCellValue(authCellValue);
  543. // }
  544. }
  545. authPartyRowNum.getAndIncrement();
  546. serialNum++;
  547. }
  548. // });
  549. // 让列宽随着导出的列长自动适应
  550. for (int colNum = 0; colNum < rowName.length; colNum++) {
  551. int productColumnWidth = authPartySheet.getColumnWidth(colNum) / 256;
  552. for (int rowNum = 0; rowNum < authPartySheet.getLastRowNum(); rowNum++) {
  553. XSSFRow currentRow;
  554. // 当前行未被使用过
  555. if (authPartySheet.getRow(rowNum) == null) {
  556. currentRow = authPartySheet.createRow(rowNum);
  557. } else {
  558. currentRow = authPartySheet.getRow(rowNum);
  559. }
  560. if (currentRow.getCell(colNum) != null) {
  561. XSSFCell currentCell = currentRow.getCell(colNum);
  562. if (currentCell.getCellTypeEnum().equals(CellType.valueOf("STRING"))) {
  563. int length = currentCell.getStringCellValue()
  564. .getBytes().length;
  565. if (productColumnWidth < length) {
  566. productColumnWidth = length;
  567. }
  568. }
  569. }
  570. }
  571. authPartySheet.setColumnWidth(colNum, (productColumnWidth + 4) * 256);
  572. }
  573. outputStream.flush();
  574. workbook.write(outputStream);
  575. outputStream.close();
  576. } catch (Exception e) {
  577. e.printStackTrace();
  578. return ResponseJson.error("导出失败");
  579. }
  580. return null;
  581. }
  582. }