DownloadServiceImpl.java 29 KB

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