|
@@ -43,6 +43,7 @@ import java.io.*;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.List;
|
|
@@ -503,32 +504,17 @@ public class AuthServiceImpl implements AuthService {
|
|
|
public ResponseJson exportDataByExcel(Integer authUserId, HttpServletResponse response) {
|
|
|
try {
|
|
|
// 导出表格名
|
|
|
- String fileName = new String("机构信息.xls".getBytes("UTF-8"),"iso-8859-1");
|
|
|
+ String fileName = new String("机构信息.xlsx".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
|
|
|
// 机构数据
|
|
|
List<AuthFormVo> authPartyList = authMapper.getAuthPartyList(authUserId);
|
|
|
- /*authPartyList.forEach(authParty->{
|
|
|
- // 商品参数数量最大值
|
|
|
- final Integer[] maxParamNum = {0};
|
|
|
- Integer authId = authParty.getId();
|
|
|
- List<ProductFormVo> productList = authProductMapper.getAuthProductList(authId);
|
|
|
- productList.forEach(product->{
|
|
|
- // 参数列表
|
|
|
- List<ProductParamPo> paramList = authProductMapper.getParamsByProductId(product.getProductId());
|
|
|
- product.setParamList(paramList);
|
|
|
- if (paramList.size() > maxParamNum[0]) {
|
|
|
- maxParamNum[0] = paramList.size();
|
|
|
- }
|
|
|
- });
|
|
|
- authParty.setMaxParamNum(maxParamNum[0]);
|
|
|
- authParty.setProductList(productList);
|
|
|
- });*/
|
|
|
OutputStream outputStream = response.getOutputStream();
|
|
|
response.reset();
|
|
|
response.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
response.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
response.setHeader("Content-disposition",
|
|
|
"attachment; filename="+fileName);
|
|
|
- response.setContentType("application/vnd.ms-excel");
|
|
|
+// response.setContentType("application/vnd.ms-excel");
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
return exportData(authPartyList, outputStream);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
@@ -750,168 +736,74 @@ public class AuthServiceImpl implements AuthService {
|
|
|
HSSFCellStyle topCellStyle = this.getTopCellStyle(workbook);
|
|
|
HSSFCellStyle customCellStyle = this.getCustomCellStyle(workbook);
|
|
|
// 创建列头行
|
|
|
- HSSFRow topRow = authPartySheet.createRow(0);
|
|
|
- HSSFCell topRowCell = topRow.createCell(0);
|
|
|
- topRowCell.setCellType(CellType.valueOf("STRING"));
|
|
|
- HSSFRichTextString text = new HSSFRichTextString("授权机构");
|
|
|
- topRowCell.setCellValue(text);
|
|
|
- topRowCell.setCellStyle(topCellStyle);
|
|
|
+ HSSFRow productTopRow = authPartySheet.createRow(0);
|
|
|
+ // 添加列头单元格(5个固定列+参数列表)
|
|
|
+ String[] rowName = {"机构名称(必填)", "所在地区(必填)", "详细地址(必填)", "经纬度(必填)",
|
|
|
+ "联系电话(必填)", "认证编号(选填)", "认证日期(选填)", "员工人数(必填)", "店铺备注(选填)"};
|
|
|
+ for (int i = 0; i < rowName.length; i++) {
|
|
|
+ HSSFCell productTopRowCell = productTopRow.createCell(i);
|
|
|
+ productTopRowCell.setCellType(CellType.valueOf("STRING"));
|
|
|
+ HSSFRichTextString columnText = new HSSFRichTextString(rowName[i]);
|
|
|
+ productTopRowCell.setCellValue(columnText);
|
|
|
+ productTopRowCell.setCellStyle(topCellStyle);
|
|
|
+ }
|
|
|
// 行索引
|
|
|
AtomicInteger authPartyRowNum = new AtomicInteger(1);
|
|
|
- // 每个机构创建一行数据
|
|
|
+
|
|
|
authPartyList.forEach(authParty -> {
|
|
|
- HSSFRow authPartyRow = authPartySheet.createRow(authPartyRowNum.get());
|
|
|
- HSSFCell authPartyCell = authPartyRow.createCell(0);
|
|
|
- authPartyCell.setCellType(CellType.valueOf("STRING"));
|
|
|
- HSSFRichTextString authPartyName = new HSSFRichTextString(authParty.getAuthParty().trim());
|
|
|
- authPartyCell.setCellValue(authPartyName);
|
|
|
- authPartyCell.setCellStyle(customCellStyle);
|
|
|
+ // 创建商品行
|
|
|
+ HSSFRow authRow = authPartySheet.createRow(authPartyRowNum.get());
|
|
|
+ // 组装商品数据
|
|
|
+ List<String> authData = new ArrayList<>();
|
|
|
+ authData.add(authParty.getAuthParty());
|
|
|
+ authData.add(authParty.getArea());
|
|
|
+ authData.add(authParty.getAddress());
|
|
|
+ authData.add(authParty.getLngAndLat());
|
|
|
+ authData.add(authParty.getMobile());
|
|
|
+ authData.add(authParty.getAuthCode());
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
|
|
|
+ Date authDate = authParty.getAuthDate();
|
|
|
+ authData.add(format.format(authDate));
|
|
|
+ authData.add(authParty.getEmpNum().toString());
|
|
|
+ authData.add(authParty.getRemarks());
|
|
|
+ for (int j = 0; j < authData.size(); j++) {
|
|
|
+ HSSFCell authCell = authRow.createCell(j);
|
|
|
+ authCell.setCellStyle(customCellStyle);
|
|
|
+ if (j == 7) {
|
|
|
+ authCell.setCellType(CellType.NUMERIC);
|
|
|
+ authCell.setCellValue(Integer.parseInt(authData.get(j)));
|
|
|
+ } else {
|
|
|
+ authCell.setCellType(CellType.valueOf("STRING"));
|
|
|
+ HSSFRichTextString authCellValue = new HSSFRichTextString(authData.get(j));
|
|
|
+ authCell.setCellValue(authCellValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
authPartyRowNum.getAndIncrement();
|
|
|
});
|
|
|
// 让列宽随着导出的列长自动适应
|
|
|
- int columnWidth = authPartySheet.getColumnWidth(0) / 256;
|
|
|
- for (int rowNum = 0; rowNum < authPartySheet.getLastRowNum(); rowNum++) {
|
|
|
- HSSFRow currentRow;
|
|
|
- // 当前行未被使用过
|
|
|
- if (authPartySheet.getRow(rowNum) == null) {
|
|
|
- currentRow = authPartySheet.createRow(rowNum);
|
|
|
- } else {
|
|
|
- currentRow = authPartySheet.getRow(rowNum);
|
|
|
- }
|
|
|
- if (currentRow.getCell(0) != null) {
|
|
|
- HSSFCell currentCell = currentRow.getCell(0);
|
|
|
- if (currentCell.getCellTypeEnum().equals(CellType.valueOf("STRING"))) {
|
|
|
- int length = currentCell.getStringCellValue()
|
|
|
- .getBytes().length;
|
|
|
- if (columnWidth < length) {
|
|
|
- columnWidth = length;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- authPartySheet.setColumnWidth(0, (columnWidth + 4) * 256);
|
|
|
- List<String> sheetNameList = new ArrayList<>();
|
|
|
- // 创建每个机构对应的商品表
|
|
|
- authPartyList.forEach(authParty -> {
|
|
|
- // 授权机构名称作为表名
|
|
|
- String productSheetName = authParty.getAuthParty();
|
|
|
- // 商品列表
|
|
|
- List<ProductFormVo> productList = authParty.getProductList();
|
|
|
- // 创建商品工作表
|
|
|
- int k = 1;
|
|
|
- while (sheetNameList.contains(productSheetName)) {
|
|
|
- productSheetName = productSheetName.replace("(" + (k-1) + ")", "") + "(" + k + ")";
|
|
|
- k++;
|
|
|
- }
|
|
|
- sheetNameList.add(productSheetName);
|
|
|
- HSSFSheet productSheet = workbook.createSheet(productSheetName);
|
|
|
- // 创建列头行
|
|
|
- HSSFRow productTopRow = productSheet.createRow(0);
|
|
|
- // 添加列头单元格(5个固定列+参数列表)
|
|
|
- String[] rowName = {"商品名称", "商品SN码", "所属品牌", "商品图片(尺寸:128px×88px)",
|
|
|
- "授权牌(尺寸:128px×88px)", "参数名称1", "参数值1", "参数名称2", "参数值2", "参数名称3",
|
|
|
- "参数值3", "参数名称4", "参数值4", "参数名称5", "参数值5", "参数名称6", "参数值6", "参数名称7",
|
|
|
- "参数值7", "参数名称8", "参数值8", "参数名称9", "参数值9", "参数名称10", "参数值10", "参数名称11",
|
|
|
- "参数值11", "参数名称12", "参数值12"};
|
|
|
- for (int i = 0; i < 5 + (0 == authParty.getMaxParamNum() ? 4 : authParty.getMaxParamNum()) * 2; i++) {
|
|
|
- HSSFCell productTopRowCell = productTopRow.createCell(i);
|
|
|
- productTopRowCell.setCellType(CellType.valueOf("STRING"));
|
|
|
- HSSFRichTextString columnText = new HSSFRichTextString(rowName[i]);
|
|
|
- productTopRowCell.setCellValue(columnText);
|
|
|
- productTopRowCell.setCellStyle(topCellStyle);
|
|
|
- }
|
|
|
- // 添加商品数据到单元格中
|
|
|
- // 行索引
|
|
|
- AtomicInteger productRowNum = new AtomicInteger(1);
|
|
|
- productList.forEach(product -> {
|
|
|
- // 创建商品行
|
|
|
- HSSFRow productRow = productSheet.createRow(productRowNum.get());
|
|
|
- // 设置行高度
|
|
|
- productRow.setHeight((short) 2200);
|
|
|
- // 参数列表
|
|
|
- List<ProductParamPo> paramList = product.getParamList();
|
|
|
- // 组装商品数据
|
|
|
- List<String> productData = new ArrayList<>();
|
|
|
- productData.add(product.getProductName());
|
|
|
- productData.add(product.getSnCode());
|
|
|
- productData.add(product.getBrandName());
|
|
|
- productData.add(product.getProductImage());
|
|
|
- productData.add(product.getCertificateImage());
|
|
|
- paramList.forEach(param -> {
|
|
|
- productData.add(param.getParamName());
|
|
|
- productData.add(param.getParamContent());
|
|
|
- });
|
|
|
- for (int j = 0; j < 5 + paramList.size() * 2; j++) {
|
|
|
- HSSFCell productCell = productRow.createCell(j);
|
|
|
- if (j == 3 || j == 4) {
|
|
|
-// productSheet.addMergedRegion(new CellRangeAddress(j + 1,j + 1,j + 1,j + 1)) ;
|
|
|
- // 头像
|
|
|
- String imageUrl = productData.get(j);
|
|
|
- if (StringUtils.isNotEmpty(imageUrl)) {
|
|
|
-
|
|
|
- String fileType = imageUrl.substring(imageUrl.lastIndexOf(".") + 1);
|
|
|
- InputStream imageStream = getImageStream(imageUrl);
|
|
|
- if (null != imageStream) {
|
|
|
- try {
|
|
|
- BufferedImage bufferedImage = ImageIO.read(imageStream);
|
|
|
- ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
|
|
|
- ImageIO.write(bufferedImage, fileType, byteArrayOut);
|
|
|
- byte[] data = byteArrayOut.toByteArray();
|
|
|
- HSSFPatriarch drawingPatriarch = productSheet.createDrawingPatriarch();
|
|
|
- // 设置图片位置
|
|
|
- HSSFClientAnchor anchor = new HSSFClientAnchor(30, 30, 700, 200, (short) j, productRowNum.get(), (short) j, productRowNum.get());
|
|
|
- drawingPatriarch.createPicture(anchor, workbook.addPicture(data, HSSFWorkbook.PICTURE_TYPE_JPEG));
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- } else {
|
|
|
- productCell.setCellType(CellType.valueOf("STRING"));
|
|
|
- productCell.setCellValue("");
|
|
|
- }
|
|
|
- } else {
|
|
|
- productCell.setCellType(CellType.valueOf("STRING"));
|
|
|
- productCell.setCellValue("");
|
|
|
- }
|
|
|
- } else {
|
|
|
- productCell.setCellType(CellType.valueOf("STRING"));
|
|
|
- HSSFRichTextString productCellValue = new HSSFRichTextString(productData.get(j));
|
|
|
- productCell.setCellValue(productCellValue);
|
|
|
- productCell.setCellStyle(customCellStyle);
|
|
|
- }
|
|
|
- }
|
|
|
- productRowNum.getAndIncrement();
|
|
|
- });
|
|
|
-
|
|
|
- // 让列宽随着导出的列长自动适应
|
|
|
- for (int colNum = 0; (colNum < 5 + authParty.getMaxParamNum() * 2); colNum++) {
|
|
|
- if (colNum == 3 || colNum == 4) {
|
|
|
- productSheet.setColumnWidth(colNum, 8200);
|
|
|
+ for (int colNum = 0; colNum < rowName.length; colNum++) {
|
|
|
+ int productColumnWidth = authPartySheet.getColumnWidth(colNum) / 256;
|
|
|
+ for (int rowNum = 0; rowNum < authPartySheet.getLastRowNum(); rowNum++) {
|
|
|
+ HSSFRow currentRow;
|
|
|
+ // 当前行未被使用过
|
|
|
+ if (authPartySheet.getRow(rowNum) == null) {
|
|
|
+ currentRow = authPartySheet.createRow(rowNum);
|
|
|
} else {
|
|
|
- int productColumnWidth = productSheet.getColumnWidth(colNum) / 256;
|
|
|
- for (int rowNum = 0; rowNum < productSheet.getLastRowNum(); rowNum++) {
|
|
|
- HSSFRow currentRow;
|
|
|
- // 当前行未被使用过
|
|
|
- if (productSheet.getRow(rowNum) == null) {
|
|
|
- currentRow = productSheet.createRow(rowNum);
|
|
|
- } else {
|
|
|
- currentRow = productSheet.getRow(rowNum);
|
|
|
- }
|
|
|
- if (currentRow.getCell(colNum) != null) {
|
|
|
- HSSFCell currentCell = currentRow.getCell(colNum);
|
|
|
- if (currentCell.getCellTypeEnum().equals(CellType.valueOf("STRING"))) {
|
|
|
- int length = currentCell.getStringCellValue()
|
|
|
- .getBytes().length;
|
|
|
- if (productColumnWidth < length) {
|
|
|
- productColumnWidth = length;
|
|
|
- }
|
|
|
- }
|
|
|
+ currentRow = authPartySheet.getRow(rowNum);
|
|
|
+ }
|
|
|
+ if (currentRow.getCell(colNum) != null) {
|
|
|
+ HSSFCell currentCell = currentRow.getCell(colNum);
|
|
|
+ if (currentCell.getCellTypeEnum().equals(CellType.valueOf("STRING"))) {
|
|
|
+ int length = currentCell.getStringCellValue()
|
|
|
+ .getBytes().length;
|
|
|
+ if (productColumnWidth < length) {
|
|
|
+ productColumnWidth = length;
|
|
|
}
|
|
|
}
|
|
|
- productSheet.setColumnWidth(colNum, (productColumnWidth + 4) * 256);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- });
|
|
|
+ authPartySheet.setColumnWidth(colNum, (productColumnWidth + 4) * 256);
|
|
|
+ }
|
|
|
outputStream.flush();
|
|
|
workbook.write(outputStream);
|
|
|
outputStream.close();
|