feat: 企标导出模板为压缩包

This commit is contained in:
2023-10-08 09:11:29 +08:00
parent cb2d3846b0
commit dbc8e4911b
3 changed files with 188 additions and 70 deletions
@@ -163,4 +163,10 @@ public interface ILawsCommonService {
* @throws Exception
*/
byte[] getMinioFileContent(OSSFile file) throws Exception;
/**
* 企标的模板下载是压缩包,里面有两个Excel模板
* @param response
*/
void esTemplateDownload(HttpServletResponse response) throws Exception;
}
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jero.common.constant.CommonConstant;
import com.jero.common.constant.enums.LanguageEnum;
import com.jero.common.constant.enums.YesOrNoEnum;
import com.jero.common.exception.JeroBootException;
@@ -1230,75 +1231,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
}
// 创建一个Excel工作簿
Workbook workbook = new XSSFWorkbook();
// 创建一个工作表
Sheet sheet = workbook.createSheet("导入模板");
// 设置列宽
for (int i = 0; i < fieldList.size(); i++) {
sheet.setColumnWidth(i, 15 * 256);
}
// 创建红色字体
Font redFont = workbook.createFont();
redFont.setColor(IndexedColors.RED.getIndex());
// 创建默认字体(黑色)
Font defaultFont = workbook.createFont();
defaultFont.setColor(IndexedColors.BLACK.getIndex());
// 创建第一行并添加字段
Row row1 = sheet.createRow(0);
for (int i = 0; i < fieldList.size(); i++) {
LawsTag tag = fieldList.get(i);
String cellValue = tag.getDbFieldTxt();
// 创建单元格
Cell cell = row1.createCell(i);
// 判断是否为必填项
if (YesOrNoEnum.YES.getValue().equals(tag.getFieldMustInput())) {
// 创建富文本字符串
CreationHelper createHelper = workbook.getCreationHelper();
RichTextString richString = createHelper.createRichTextString("*" + cellValue);
// 设置 * 为红色
richString.applyFont(0, 1, redFont);
// 设置其他文字为默认样式(黑色)
richString.applyFont(1, richString.length(), defaultFont);
// 设置单元格的值
cell.setCellValue(richString);
} else {
cell.setCellValue(cellValue);
}
}
// 创建第二行并合并单元格
Row row2 = sheet.createRow(1);
row2.setHeightInPoints(95);
// 创建一个单元格样式,并设置为自动换行
CellStyle style = workbook.createCellStyle();
style.setWrapText(true);
sheet.addMergedRegion(new CellRangeAddress(
1,
1,
0,
fieldList.size() - 1
));
Cell cell2 = row2.createCell(0);
cell2.setCellValue("填写说明\n" +
"1.所有带星号的为必填\n" +
"2.导入数据从第四行开始第一行为表头第二行为填写说明第三行为示例第四行才是正式数据\n" +
"3.字段为多选属性必须与系统中对应选项字段相匹配填写多个时使用逗号隔开\n" +
"4.字段为文件属性填写时需在本文件同级目录下以标准号为名称建立文件夹并在文件夹内放置文件假设在A标准号的文件夹内放置了A1文件那么单元格需填写A/A1.pdf");
// 应用样式到单元格
cell2.setCellStyle(style);
// 创建第三行并添加固定内容
Row row3 = sheet.createRow(2);
row3.setHeightInPoints(105);
this.setRow3CellValue(row3, fieldList);
Workbook workbook = this.createWorkBookWithLawsTags(fieldList);
// 设置HTTP响应头
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
@@ -1933,4 +1866,178 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
return outputStream.toByteArray();
}
}
@Override
public void esTemplateDownload(HttpServletResponse response) throws Exception {
// 1. 生成企标的模板
Workbook mainWorkbook = this.createEsExcelTemplate();
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
mainWorkbook.write(baos1);
byte[] mainWorkbookBytes = baos1.toByteArray();
// 2. 生成稽查内容的模板
Workbook workbook2 = createInspectContentExcelTemplate(); // 这是一个新函数,你需要根据第二个模板的需求来实现
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
workbook2.write(baos2);
byte[] inspectContentBytes = baos2.toByteArray();
// 3. 将两个Excel文件放入ZIP包中
ByteArrayOutputStream baosZip = new ByteArrayOutputStream();
try (ZipOutputStream zos = new ZipOutputStream(baosZip)) {
// 添加第一个文件到ZIP中
ZipEntry entry1 = new ZipEntry("企标导入模板.xlsx");
zos.putNextEntry(entry1);
zos.write(mainWorkbookBytes);
zos.closeEntry();
// 添加第二个文件到ZIP中
ZipEntry entry2 = new ZipEntry("稽查内容.xlsx");
zos.putNextEntry(entry2);
zos.write(inspectContentBytes);
zos.closeEntry();
}
// 4. 设置HTTP响应头,并返回ZIP文件
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=\"导入模板.zip\"");
// 5. 输出ZIP文件到响应流中
try (OutputStream out = response.getOutputStream()) {
baosZip.writeTo(out);
out.flush();
}
}
/**
* 创建企标导入模板的Workbook
* @return
*/
private Workbook createEsExcelTemplate() {
// 获取全部字段
List<LawsTag> fieldList = lawsCommonMapper.getFieldList(TableNameEnum.ENTERPRISE_STANDARDS.getTableName());
// 筛选出新增要用的字段
fieldList = fieldList.stream().filter(lawsTag -> YesOrNoEnum.YES.getValue()
.equals(lawsTag.getIsShowFormCreate().toString())).collect(Collectors.toList());
// 去除企标导入逻辑不需要显示的字段
fieldList = fieldList.stream()
.filter(lawsTag -> !lawsTag.getDbFieldName().equals(FieldCommon.ENTERPRISE_STANDARD_CODE))
.filter(lawsTag -> !lawsTag.getDbFieldName().equals(FieldCommon.ENTERPRISE_NAME_CODE))
.filter(lawsTag -> !lawsTag.getDbFieldName().equals(FieldCommon.STANDARD_CATEGORY_CODE))
.filter(lawsTag -> !lawsTag.getDbFieldName().equals(FieldCommon.YEAR_NUMBER))
.collect(Collectors.toList());
// 添加稽查内容字段
LawsTag inspectContentTag = new LawsTag();
inspectContentTag.setDbFieldTxt("稽查内容");
inspectContentTag.setFieldShowType(FieldShowTypeEnum.FILE_UPLOAD.getValue());
fieldList.add(inspectContentTag);
// 创建一个Excel工作簿
return this.createWorkBookWithLawsTags(fieldList);
}
/**
* 创建稽查内容模板的Workbook
* @return
*/
private Workbook createInspectContentExcelTemplate() {
List<LawsTag> fieldList = new ArrayList<>();
// 依据条款
LawsTag clauseTag = new LawsTag();
clauseTag.setDbFieldTxt("依据条款");
clauseTag.setFieldShowType(FieldShowTypeEnum.TEXT_BOX.getValue());
clauseTag.setFieldMustInput(YesOrNoEnum.YES.getValue());
fieldList.add(clauseTag);
// 标准内容或要求
LawsTag requirementTag = new LawsTag();
requirementTag.setDbFieldTxt("标注内容或要求");
requirementTag.setFieldShowType(FieldShowTypeEnum.TEXT_BOX.getValue());
requirementTag.setFieldMustInput(YesOrNoEnum.YES.getValue());
fieldList.add(requirementTag);
// 创建一个Excel工作簿
return this.createWorkBookWithLawsTags(fieldList);
}
/**
* 通用创建WorkBook方法
* @param fieldList
* @return
*/
private Workbook createWorkBookWithLawsTags(List<LawsTag> fieldList) {
// 创建一个Excel工作簿
Workbook workbook = new XSSFWorkbook();
// 创建一个工作表
Sheet sheet = workbook.createSheet("导入模板");
// 设置列宽
for (int i = 0; i < fieldList.size(); i++) {
sheet.setColumnWidth(i, 15 * 256);
}
// 创建红色字体
Font redFont = workbook.createFont();
redFont.setColor(IndexedColors.RED.getIndex());
// 创建默认字体(黑色)
Font defaultFont = workbook.createFont();
defaultFont.setColor(IndexedColors.BLACK.getIndex());
// 创建第一行并添加字段
Row row1 = sheet.createRow(0);
for (int i = 0; i < fieldList.size(); i++) {
LawsTag tag = fieldList.get(i);
String cellValue = tag.getDbFieldTxt();
// 创建单元格
Cell cell = row1.createCell(i);
// 判断是否为必填项
if (YesOrNoEnum.YES.getValue().equals(tag.getFieldMustInput())) {
// 创建富文本字符串
CreationHelper createHelper = workbook.getCreationHelper();
RichTextString richString = createHelper.createRichTextString("*" + cellValue);
// 设置 * 为红色
richString.applyFont(0, 1, redFont);
// 设置其他文字为默认样式(黑色)
richString.applyFont(1, richString.length(), defaultFont);
// 设置单元格的值
cell.setCellValue(richString);
} else {
cell.setCellValue(cellValue);
}
}
// 创建第二行并合并单元格
Row row2 = sheet.createRow(1);
row2.setHeightInPoints(95);
// 创建一个单元格样式,并设置为自动换行
CellStyle style = workbook.createCellStyle();
style.setWrapText(true);
sheet.addMergedRegion(new CellRangeAddress(
1,
1,
0,
fieldList.size() - 1
));
Cell cell2 = row2.createCell(0);
cell2.setCellValue("填写说明:\n" +
"1.所有带星号的为必填\n" +
"2.导入数据从第四行开始,第一行为表头,第二行为填写说明,第三行为示例。第四行才是正式数据\n" +
"3.字段为多选属性,必须与系统中对应选项字段相匹配,填写多个时使用逗号隔开\n" +
"4.字段为文件属性,填写时需在本文件同级目录下以标准号为名称建立文件夹,并在文件夹内放置文件。假设在A标准号的文件夹内放置了A1文件,那么单元格需填写:A/A1.pdf");
// 应用样式到单元格
cell2.setCellStyle(style);
// 创建第三行并添加固定内容
Row row3 = sheet.createRow(2);
row3.setHeightInPoints(105);
this.setRow3CellValue(row3, fieldList);
return workbook;
}
}
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jero.common.api.vo.Result;
import com.jero.common.aspect.annotation.AutoLog;
import com.jero.common.exception.JeroBootException;
import com.jero.common.util.MessageUtils;
import com.jero.modules.laws.common.constant.FieldCommon;
import com.jero.modules.laws.common.constant.ResultCommon;
@@ -215,7 +216,11 @@ public class LawsEnterpriseController {
@ApiOperation(value="企业法规标准-模板下载", notes="企业法规标准-模板下载")
@GetMapping("/templateDownload")
public void templateDownload(HttpServletResponse response) {
lawsCommonService.templateDownload(response, TableNameEnum.ENTERPRISE_STANDARDS.getTableName());
try {
lawsCommonService.esTemplateDownload(response);
} catch (Exception e) {
throw new JeroBootException("模板下载失败");
}
}
@AutoLog(value = "企业法规标准-导入")