文档库--模板下载

This commit is contained in:
zhn
2022-02-25 19:24:15 +08:00
parent 027246fb4f
commit a0089d2b04
2 changed files with 105 additions and 55 deletions
@@ -1,5 +1,6 @@
package com.jero.modules.collection.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -32,11 +32,15 @@ import com.jero.modules.tag.service.impl.OnlCgformAreaServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -1126,11 +1130,11 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
@Override
public void exportTemplate(Map<String,Object> map,HttpServletResponse response, HttpServletRequest request) {
OutputStream os = null;
OutputStream excelOS = null;
HSSFWorkbook workbook = new HSSFWorkbook();
String fileOriName = "导入模板";
String filePath = uploadpath + "/" + fileOriName;
try{
List<String> list = getWorkbookTitle((String) map.get("cut"));
//创建临时文件夹
File nowFile = new File(filePath);
if (nowFile.exists()){
@@ -1138,46 +1142,48 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
}
nowFile.mkdirs();
String fileName = "导入模板.xls";
HSSFSheet sheetItems = workbook.createSheet("模板");
sheetItems.setDefaultColumnWidth(16);
HSSFSheet sheet = workbook.createSheet("文档库导入模板");
sheet.setDefaultColumnWidth(16);//列宽
HSSFCellStyle cellStyle =workbook.createCellStyle();
cellStyle.setWrapText(true);
cellStyle.setWrapText(true);//自动换行
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
Row rowHeader = sheetItems.createRow(0);//开始创建标题行
getWorkbookTitle((String)map.get("cut"));
String exportFieldName = "标准编号,标题,发布时间";
//合并单元格
CellRangeAddress region1 =
new CellRangeAddress(1, 1, 0, Arrays.asList(list.get(0).split(",")).size()-1); //参数1:起始行 参数2:终止行 参数3:起始列 参数4:终止列
sheet.addMergedRegion(region1);
if (StringUtils.isNotBlank(exportFieldName)) {
String[] headerArr = exportFieldName.split(",");
for (int i=0;i < headerArr.length; i++) {
rowHeader.createCell(i).setCellValue(headerArr[i]);
for (int i = 0; i < 2; i++) {
//表头
Row row = sheet.createRow(i);//开始创建标题行
String exportFieldName = list.get(i);
if(i==0){
if (StringUtils.isNotBlank(exportFieldName)) {
String[] headerArr = exportFieldName.split(",");
for (int j = 0; j < headerArr.length; j++) {
row.createCell(j).setCellValue(headerArr[j]);
}
}
}else{
//说明
short height = (short) (Integer.parseInt(list.get(2)) * 252);
row.setHeight((short) height);
Cell cell = row.createCell(0);
cell.setCellStyle(cellStyle);
cell.setCellValue(new HSSFRichTextString(list.get(i)));
}
}
String repFileName = fileName.replaceAll("/","_");
excelOS = new FileOutputStream(filePath + "/" + repFileName);
response.setHeader("Content-Disposition",
"attachment; filename=\""+ fileOriName+".zip");
"attachment; filename=\""+ fileOriName+".xlsx");
response.setContentType("application/force-download");
response.flushBuffer();
os = response.getOutputStream();
workbook.write(excelOS);
excelOS.flush();
excelOS.close();
ZipUtil.zip(filePath,filePath+".zip");
FileInputStream fis = new FileInputStream(filePath+".zip");
int len = 0;
while ((len = fis.read()) != -1) {
os.write(len);
}
os.flush();
os.close(); // 后开先关
fis.close(); // 先开后关
workbook.write(os);
} catch (Exception e) {
throw new JeroBootException("下载文件失败,请重试");
} finally {
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(excelOS);
}
}
@@ -1186,49 +1192,92 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
* 获取模板下载的表头
* @return
*/
private String getWorkbookTitle(String cut){
private List<String> getWorkbookTitle(String cut){
List<Map<String, Object>> addForm = getAddForm(ModuleEnum.DOCUMENT_LIBRARY.getValue(), cut);
StringBuilder sb = new StringBuilder();
List<String> multipleList = new LinkedList<>();
List<String> singleList = new LinkedList<>();
List<String> fileList = new LinkedList<>();
List<String> pullMoreList = new LinkedList<>();//多选字段
List<String> pullSingleList = new LinkedList<>();//单选字段
List<String> fileList = new LinkedList<>();//文件字段
List<String> textNumberList = new LinkedList<>();//数字输入框字段
List<String> textStringList = new LinkedList<>();//字符串输入框字段
List<String> dateSingleList = new LinkedList<>();//单选日期字段
List<String> dateMoreList = new LinkedList<>();//多选日期字段
List<String> standardList = new LinkedList<>();//标准选择字段
for (Map<String, Object> map : addForm) {
if("0".equals(map.get("field_must_input"))){
sb.append("*" + map.get("db_field_txt")+",");
}else{
sb.append(map.get("db_field_txt")+",");
}
String fieldShowType = "field_show_type";
//多选字段
// if(FieldTypeEnum.PULL_MORE.getValue().equals(map.get("field_show_type"))){
// multipleList.add((String) map.get("db_field_txt"));
// }else if(FieldTypeEnum.PULL_SINGLE.getValue().equals(map.get("field_show_type"))){
// //单选字段
// singleList.add((String) map.get("db_field_txt"));
// }else if(FieldTypeEnum.FILE.getValue().equals(map.get("field_show_type"))){
// //文件字段
// fileList.add((String) map.get("db_field_txt"));
// }else if(FieldTypeEnum.0000.getValue().equals(map.get("field_show_type"))){
// //数字输入框字段
// fileList.add((String) map.get("db_field_txt"));
// }else if(FieldTypeEnum.0000.getValue().equals(map.get("field_show_type"))){
// //汉字输入框字段
// fileList.add((String) map.get("db_field_txt"));
// }else if(FieldTypeEnum.0000.getValue().equals(map.get("field_show_type"))){
// //单选日期字段
// fileList.add((String) map.get("db_field_txt"));
// }else if(FieldTypeEnum.000.getValue().equals(map.get("field_show_type"))){
// //多选日期字段
// fileList.add((String) map.get("db_field_txt"));
// }
if(FieldTypeEnum.PULL_MORE.getValue().equals(map.get(fieldShowType))){
pullMoreList.add((String) map.get("db_field_txt"));
}else if(FieldTypeEnum.PULL_SINGLE.getValue().equals(map.get(fieldShowType))){
//单选字段
pullSingleList.add((String) map.get("db_field_txt"));
}else if(FieldTypeEnum.FILE.getValue().equals(map.get(fieldShowType))){
//文件字段
fileList.add((String) map.get("db_field_txt"));
}else if(FieldTypeEnum.TEXT_NUMBER.getValue().equals(map.get(fieldShowType))){
//数字输入框字段
textNumberList.add((String) map.get("db_field_txt"));
}else if(FieldTypeEnum.TEXT_STRING.getValue().equals(map.get(fieldShowType))){
//字符串输入框字段
textStringList.add((String) map.get("db_field_txt"));
}else if(FieldTypeEnum.DATE_SINGLE.getValue().equals(map.get(fieldShowType))){
//单选日期字段
dateSingleList.add((String) map.get("db_field_txt"));
}else if(FieldTypeEnum.DATE_MORE.getValue().equals(map.get(fieldShowType))){
//多选日期字段
dateMoreList.add((String) map.get("db_field_txt"));
}else if(FieldTypeEnum.STANDARD.getValue().equals(map.get(fieldShowType))){
//标准选择字段
standardList.add((String) map.get("db_field_txt"));
}
}
String title = sb.substring(0, sb.length() - 1);
//第二行
StringBuilder sbTwo = new StringBuilder();
sbTwo.append("填写说明\n"+"1.所有带*号的字段必须填写");
sbTwo.append("填写说明\n"+"1.所有带*号的字段必须填写\n");
int count = 2;
if(pullSingleList.size() != 0){
//单选
String pullSingle = StringUtils.join(pullSingleList,",")+"字段是单选属性,必须和系统中的对应字段选项相匹配\n";
sbTwo.append(count ++ +"."+pullSingle);
}
return null;
if(pullMoreList.size() != 0){
//多选
String pullMore = StringUtils.join(pullMoreList,",")+"字段是多选属性,必须和系统中的对应字段选项相匹配,填写多个时采用英文或中文逗号分割\n";
sbTwo.append(count ++ +"."+pullMore);
}
if(dateSingleList.size() != 0){
//单选日期
String dateSingle = StringUtils.join(dateSingleList,",")+"字段为日期,必须精确到日\n";
sbTwo.append(count ++ +"."+dateSingle);
}
if(textStringList.size() != 0){
//输入框
String textString = StringUtils.join(textStringList,",")+"字段为文本,填写文本内容\n";
sbTwo.append(count ++ +"."+textString);
}
if(standardList.size() != 0){
//标准选择
String standard = StringUtils.join(standardList,",")+"字段为标准号,填写多个时采用英文或中文逗号分割\n";
sbTwo.append(count ++ +"."+standard);
}
if(fileList.size() != 0){
//文件
String file = StringUtils.join(fileList,",")+"字段为文件属性,填写时需要在本文件同级目录下以标准号为名称建立文件夹,并在文件夹下放置文件,假设在AAA标准号下放置了B.pdf,则应填写AAA/B.pdf\n";
sbTwo.append(count ++ +"."+file);
}
List<String> list = new LinkedList<>();
list.add(title);
list.add(sbTwo.toString());
list.add(count+"");
return list;
}