文档库--导出excel(不带文件)

This commit is contained in:
zhn
2022-02-26 14:40:10 +08:00
parent 7fad73f8f4
commit 6cd7d38c7f
5 changed files with 504 additions and 338 deletions
@@ -377,25 +377,8 @@ public class BussDocumentLibraryEOController extends JeroController<BussDocument
@GetMapping(value = "/exportExcel")
public void exportExcel(@RequestParam Map<String,Object> map,
HttpServletResponse response,
HttpServletRequest request) throws Exception {
OutputStream os = null;
Workbook workbook = null;
try {
String fileName = "11111";
response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
response.setContentType("application/force-download");
// 导出所有数据
List<Map<String,Object>> datas = new ArrayList<>();
workbook = bussDocumentLibraryEOService.exportDatas(datas);
os = response.getOutputStream();
workbook.write(os);
os.flush();
} catch (IOException e) {
throw new JeroBootException("下载文件失败,请重试");
} finally {
IOUtils.closeQuietly(os);
}
HttpServletRequest request){
bussDocumentLibraryEOService.exportExcel(map,response,request);
}
@ApiOperation(value = "带文件导出")
@@ -2,10 +2,12 @@ package com.jero.modules.document.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jero.modules.document.entity.BussDocumentLibraryEO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* @Description: 文档库信息表
* @Author: jero-boot
@@ -19,6 +21,7 @@ public interface BussDocumentLibraryEOMapper extends BaseMapper<BussDocumentLibr
/**
* 分页查询
*
* @param page
* @param field
* @param condition
@@ -28,8 +31,21 @@ public interface BussDocumentLibraryEOMapper extends BaseMapper<BussDocumentLibr
@Param("field") String field,
@Param("condition") String condition);
/**
* 列表查询
*
* @param
* @param field
* @param condition
* @return
*/
List<Map<String, Object>> getInfoList(@Param("field") String field,
@Param("condition") String condition);
/**
* 代替标准分页
*
* @param page
* @param field
* @param condition
@@ -32,7 +32,6 @@
<result column="exposure_draft" property="exposureDraft" />
<result column="datum" property="datum" />
<result column="warn_flag" property="warnFlag" />
<result column="read_flag" property="readFlag" />
</resultMap>
<insert id="insertInfo">
@@ -40,10 +39,16 @@
values (${insertValue})
</insert>
<!--分页-->
<select id="getInfoPage" resultType="java.util.LinkedHashMap">
select ${field} from buss_document_library
${condition}
</select>
<!--列表-->
<select id="getInfoList" resultType="java.util.LinkedHashMap">
select ${field} from buss_document_library
${condition}
</select>
<!--代替标准分页-->
<select id="replacePageInfo" resultType="java.util.LinkedHashMap">
select ${field} from buss_document_library
@@ -134,7 +134,16 @@ public interface IBussDocumentLibraryEOService extends IService<BussDocumentLibr
*/
void cancelSubscribe(String id);
Workbook exportDatas(List<Map<String,Object>> datas);
/**
* 导出excel
* @param map
* @param response
* @param request
*/
void exportExcel(Map<String,Object> map,
HttpServletResponse response,
HttpServletRequest request);
void exportZip(Map<String,Object> map,
HttpServletResponse response,
@@ -1,6 +1,5 @@
package com.jero.modules.document.service.impl;
import cn.hutool.core.util.ZipUtil;
import com.aliyuncs.utils.IOUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -33,16 +32,19 @@ 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.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
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.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@@ -50,8 +52,6 @@ import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
@@ -67,8 +67,6 @@ import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import static cn.hutool.core.io.FileUtil.copyFile;
/**
* @Description: 文档库信息表
* @Author: jero-boot
@@ -574,6 +572,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
/**
* 更新log
*
* @param map
*/
private void updateLog(Map<String, Object> map,
@@ -710,6 +709,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
/**
* 处理修改代替标准,发送通知
*
* @param map
*/
private void sendMessage(Map<String, Object> map, List<Map<String, Object>> dataList) {
@@ -745,6 +745,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
/**
* 代替标准分页
*
* @param parameter
* @return
*/
@@ -806,7 +807,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
List<String> fieldListTemp = fieldList.stream().map(OnlCgformField::getDbFieldName).collect(Collectors.toList());
List<String> fieldListNew = new ArrayList<>();
fieldListNew.add("id");
//转时间格式 case when region is null then "" else region end region
//转时间格式
for (String field : fieldListTemp) {
String fieldTemp = null;
if ("issue_time".equals(field) || "implement_time".equals(field)) {
@@ -818,6 +819,38 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
fieldListNew.add(fieldTemp);
}
}
//封装查询条件
String condition = getConditionStr(parameter);
int pageNo = Integer.parseInt(parameter.get("pageNo").toString());
int pageSize = Integer.parseInt(parameter.get("pageSize").toString());
IPage page = new Page(pageNo, pageSize);
IPage infoPage = bussDocumentLibraryEOMapper.getInfoPage(page, " " + StringUtils.join(fieldListNew, ","), condition);
//数据字典
List<SysDictItem> sysDictItems = sysDictItemServiceImpl.selectItemsAll();
//下拉选处理数据字典
List<Map> records = infoPage.getRecords();
List<String> idList = new ArrayList<>();
for (Map record : records) {
Map<String, Object> record1 = (Map) record;
idList.add((String) record1.get("id"));
for (Map.Entry<String, Object> entry : record1.entrySet()) {
List<OnlCgformField> collect = fieldList.stream()
.filter(e -> entry.getKey().equals(e.getDictField()))
.collect(Collectors.toList());
//下拉选处理数据字典
dictItem(sysDictItems, entry, collect);
}
}
//收藏和订阅
collectAndSubscribe(records, idList);
return infoPage;
}
@NotNull
private String getConditionStr(Map<String, Object> parameter) {
//查询条件
StringBuilder conditionSb = new StringBuilder("where 1 = 1");
@@ -834,7 +867,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
if (key.equals(fieldName)) {
//字段类型(判断是输入框还是下拉,等等)
String fieldType = (String) map.get("field_show_type");//字段类型
if (FieldTypeEnum.TEXT.getValue().equals(fieldType)) {
if (FieldTypeEnum.TEXT_STRING.getValue().equals(fieldType)) {
//输入框
conditionSb.append(" and " + key + " like concat(concat('%','" + value + "'),'%')");
} else if (FieldTypeEnum.PULL_SINGLE.getValue().equals(fieldType)
@@ -871,36 +904,14 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
} else {
conditionSb.append(" order by create_time desc");
}
int pageNo = Integer.parseInt(parameter.get("pageNo").toString());
int pageSize = Integer.parseInt(parameter.get("pageSize").toString());
IPage page = new Page(pageNo, pageSize);
IPage infoPage = bussDocumentLibraryEOMapper.getInfoPage(page, " " + StringUtils.join(fieldListNew, ","), conditionSb.toString());
//数据字典
List<SysDictItem> sysDictItems = sysDictItemServiceImpl.selectItemsAll();
//下拉选处理数据字典
List<Map> records = infoPage.getRecords();
List<String> idList = new ArrayList<>();
for (Map record : records) {
Map<String, Object> record1 = (Map) record;
idList.add((String) record1.get("id"));
for (Map.Entry<String, Object> entry : record1.entrySet()) {
List<OnlCgformField> collect = fieldList.stream()
.filter(e -> entry.getKey().equals(e.getDictField()))
.collect(Collectors.toList());
//下拉选处理数据字典
dictItem(sysDictItems, entry, collect);
}
}
//收藏和订阅
collectAndSubscribe(records, idList);
return infoPage;
String condition = conditionSb.toString();
return condition;
}
/**
* 收藏和订阅
*
* @param records
* @param idList
*/
@@ -970,7 +981,6 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
}
@Override
public void addCollect(String id) {
//收藏表中添加数据
@@ -1013,9 +1023,9 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
}
/**
* 判断两个集合元素是否完全一致
*
* @param list
* @param list1
* @return
@@ -1047,17 +1057,158 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
/**
* 导出excel
* @param datas
*
* @param map
* @param response
* @param request
*/
@Override
public Workbook exportDatas(List<Map<String, Object>> datas) {
public void exportExcel(Map<String, Object> map, HttpServletResponse response, HttpServletRequest request) {
OutputStream os = null;
Workbook workbook = new XSSFWorkbook();
return workbook;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fileName = "文件库 " + sdf.format(new Date()) + ".xlsx";
response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
response.setContentType("application/force-download");
//需要查询的字段
List<OnlCgformField> fieldList = onlCgformFieldService.getFieldList("1");
//表头
List<String> headerFieldList = new ArrayList<>();
List<OnlCgformField> fieldListTemp = new ArrayList<>();
if (fieldList.size() != 0) {
//过滤掉系统默认的字段
fieldListTemp = fieldList.stream().filter(e -> !"id".equals(e.getDbFieldName())
&& !"create_by".equals(e.getDbFieldName())
&& !"create_time".equals(e.getDbFieldName())
&& !"update_by".equals(e.getDbFieldName())
&& !"update_time".equals(e.getDbFieldName())
&& !"sys_org_code".equals(e.getDbFieldName())
&& !"warn_flag".equals(e.getDbFieldName())).collect(Collectors.toList());
//只导出数据时,过滤掉文件类型
fieldListTemp = fieldListTemp.stream().filter(e -> !FieldTypeEnum.FILE.getValue().equals(e.getFieldShowType())).collect(Collectors.toList());
headerFieldList = fieldListTemp.stream().map(OnlCgformField::getDbFieldTxt).collect(Collectors.toList());
}
//查询字段
List<String> conditionFieldList = fieldList.stream().map(OnlCgformField::getDbFieldName).collect(Collectors.toList());
//查询条件
String condition = getConditionStr(map);
//导出数据(全部导出的数据)
List<Map<String, Object>> datas = bussDocumentLibraryEOMapper.getInfoList(" " + StringUtils.join(conditionFieldList, ","), condition);
//部分导出
String partId = (String) map.get("id");
if (StringUtils.isNotBlank(partId)) {
//部门导出的数据
datas = datas.stream().filter(entry -> partId.contains((String) entry.get("id"))).collect(Collectors.toList());
}
//创建工作表对象
Sheet sheet = workbook.createSheet();
// 创建头部
String header = StringUtils.join(headerFieldList, ",");
createHeader(workbook, sheet, header);
// 创建数据
createDatas(workbook, sheet, datas, header, fieldListTemp);
os = response.getOutputStream();
workbook.write(os);
os.flush();
} catch (IOException e) {
throw new JeroBootException("下载文件失败,请重试");
} finally {
IOUtils.closeQuietly(os);
}
}
public void createDatas(Workbook workbook, Sheet sheet, List<Map<String, Object>> datas, String header, List<OnlCgformField> fieldList) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
CellStyle cellStyle = workbook.createCellStyle();//初始化单元格格式对象
cellStyle.setAlignment(HorizontalAlignment.CENTER);
//数据字典
List<SysDictItem> sysDictItems = sysDictItemServiceImpl.selectItemsAll();
//过滤出下拉单选和下拉多选的字段
List<OnlCgformField> onlCgformFieldList = fieldList.stream()
.filter(e -> FieldTypeEnum.PULL_SINGLE.getValue().equals(e.getFieldShowType())
|| FieldTypeEnum.PULL_MORE.getValue().equals(e.getFieldShowType())).collect(Collectors.toList());
int i = 0;
for (Map<String, Object> data : datas) {
for (Map.Entry<String, Object> entry : data.entrySet()) {
List<OnlCgformField> collect = fieldList.stream()
.filter(e -> entry.getKey().equals(e.getDictField()))
.collect(Collectors.toList());
//下拉选处理数据字典
dictItem(sysDictItems, entry, collect);
}
Row row = sheet.createRow(i + 1);
i++;
int sheetNum = 0;
String value = "";
for (OnlCgformField onlCgformField : fieldList) {
//时间类型
if (FieldTypeEnum.DATE_SINGLE.getValue().equals(onlCgformField.getFieldShowType())) {
if (ObjectUtils.isNotEmpty(data.get(onlCgformField.getDbFieldName()))) {
value = sdf.format(data.get(onlCgformField.getDbFieldName()));
}
} else {
value = (String) data.get(onlCgformField.getDbFieldName());
}
row.createCell(sheetNum).setCellValue(value);
sheetNum++;
}
}
int a = 0;
}
/**
* 创建表头
*
* @param workbook
* @param sheet
* @param header
*/
public static void createHeader(Workbook workbook, Sheet sheet, String header) {
CellStyle cellStyle = workbook.createCellStyle();//初始化单元格格式对象
cellStyle.setAlignment(HorizontalAlignment.CENTER);
Row rowHeader = sheet.createRow(0);//开始创建标题行
if (StringUtils.isNotBlank(header)) {
String[] headerArr = header.split(",");
for (int i = 0; i < headerArr.length; i++) {
rowHeader.createCell(i).setCellValue(headerArr[i]);
}
}
}
/**
* 获取导出的数据
*
* @param map
* @return
*/
public List<Map<String, Object>> getExportDatas(Map<String, Object> map) {
//需要查询的字段
List<OnlCgformField> fieldList = onlCgformFieldService.getFieldList("1");
if (fieldList.size() != 0) {
//过滤列表字段(is_show_list-->列表是否显示0否 1是)
fieldList = fieldList.stream().filter(e -> YesOrNoEnum.YES.getValue().equals(String.valueOf(e.getIsShowList()))).collect(Collectors.toList());
}
List<String> fieldListTemp = fieldList.stream().map(OnlCgformField::getDbFieldName).collect(Collectors.toList());
List<String> fieldListNew = new ArrayList<>();
fieldListNew.add("id");
fieldListNew.addAll(fieldListTemp);
//查询条件
String condition = getConditionStr(map);
List<Map<String, Object>> infoList = bussDocumentLibraryEOMapper.getInfoList(" " + StringUtils.join(fieldListNew, ","), condition);
return infoList;
}
/**
* 带文件导出
*
* @param datas
* @return
*/
@@ -1065,65 +1216,66 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
public void exportZip(Map<String, Object> map,
HttpServletResponse response,
HttpServletRequest request) {
try {
String fileName = "带文件导出.zip";
response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
response.setContentType("application/force-download");
// 导出所有数据
List<Map<String,Object> > datas = new ArrayList<>();
// try {
// String fileName = "带文件导出.zip";
// response.setHeader("Content-Disposition",
// "attachment; filename=" + fileName);
// response.setContentType("application/force-download");
// // 导出所有数据
// List<Map<String,Object> > datas = new ArrayList<>();
//
// List<String> pdfFileIdList = new ArrayList<>();
// //从查询的数据中,过滤出文件(根据字段类型)
//
// //文件信息
// List<OSSFile> oSSFileList = iOSSFileService.getFileInfosByConnectId(StringUtils.join(pdfFileIdList,","));
//
// //创建临时文件夹
// String uuid = "";
// String fileNowPath = "path" + "带文件导出";
// File nowFile = new File(fileNowPath);
// if (nowFile.exists()){
// nowFile.delete();
// }
// nowFile.mkdirs();
List<String> pdfFileIdList = new ArrayList<>();
//从查询的数据中,过滤出文件(根据字段类型)
//文件信息
List<OSSFile> oSSFileList = iOSSFileService.getFileInfosByConnectId(StringUtils.join(pdfFileIdList,","));
//创建临时文件夹
String uuid = "";
String fileNowPath = "path" + "带文件导出";
File nowFile = new File(fileNowPath);
if (nowFile.exists()){
nowFile.delete();
}
nowFile.mkdirs();
try(Workbook workbook = exportDatas(datas);
OutputStream excelOS = new FileOutputStream(fileNowPath + File.separator + "军品数据.xls");
OutputStream os = response.getOutputStream();
) {
for (OSSFile oSSFile : oSSFileList) {
//文件路径
String path = oSSFile.getUrl();
copyFile(path,fileNowPath+File.separator);
}
workbook.write(excelOS);
excelOS.flush();
ZipUtil.zip(fileNowPath,fileNowPath+".zip");
FileInputStream fis = new FileInputStream(fileNowPath+".zip");
int len = 0;
while ((len = fis.read()) != -1) {
os.write(len);
}
os.flush();
fis.close();
} catch (IOException e) {
throw new JeroBootException("下载文件失败,请重试");
}finally {
// File file = new File(uploadFilePath + "/tempZip/" + uuid);
// FileUnZip.deleteDir(file);
}
} catch (Exception e) {
throw new JeroBootException("下载文件失败,请重试");
}
// try(Workbook workbook = exportDatas(datas);
// OutputStream excelOS = new FileOutputStream(fileNowPath + File.separator + "军品数据.xls");
// OutputStream os = response.getOutputStream();
// ) {
// for (OSSFile oSSFile : oSSFileList) {
// //文件路径
// String path = oSSFile.getUrl();
// copyFile(path,fileNowPath+File.separator);
// }
//
// workbook.write(excelOS);
// excelOS.flush();
//
// ZipUtil.zip(fileNowPath,fileNowPath+".zip");
// FileInputStream fis = new FileInputStream(fileNowPath+".zip");
// int len = 0;
// while ((len = fis.read()) != -1) {
// os.write(len);
// }
// os.flush();
// fis.close();
// } catch (IOException e) {
// throw new JeroBootException("下载文件失败,请重试");
// }finally {
//// File file = new File(uploadFilePath + "/tempZip/" + uuid);
//// FileUnZip.deleteDir(file);
//
// }
// } catch (Exception e) {
// throw new JeroBootException("下载文件失败,请重试");
// }
}
/**
* 模板下载
*
* @param response
* @param request
*/
@@ -1190,6 +1342,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
/**
* 获取模板下载的表头
*
* @return
*/
private List<String> getWorkbookTitle(String cut) {