|
|
@@ -16,6 +16,10 @@ import com.alibaba.excel.EasyExcel;
|
|
|
import com.alibaba.excel.read.listener.PageReadListener;
|
|
|
import com.alibaba.excel.write.handler.CellWriteHandler;
|
|
|
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
|
|
|
+import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
|
|
|
+import com.alibaba.excel.write.metadata.style.WriteCellStyle;
|
|
|
+import com.alibaba.excel.write.metadata.style.WriteFont;
|
|
|
+import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
|
|
|
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
@@ -55,12 +59,14 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author lgh on 2018/09/11.
|
|
|
@@ -131,52 +137,55 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|
|
|
|
|
@Override
|
|
|
public void downloadExcelGoods(HttpServletResponse response) {
|
|
|
+ ServletOutputStream outputStream = null;
|
|
|
try {
|
|
|
- List<SysDictData> userAttrTypeList = dictDataMapper.selectDictDataByType("user_attr_type");//查询人员属性
|
|
|
- String userAttrName =null;
|
|
|
- if (userAttrTypeList!=null&&!userAttrTypeList.isEmpty()){
|
|
|
- userAttrName="";
|
|
|
- for (SysDictData sysDictData : userAttrTypeList) {
|
|
|
- userAttrName=userAttrName+sysDictData.getDictLabel()+";";
|
|
|
- }
|
|
|
+ // 重置响应,避免之前的数据影响
|
|
|
+ response.reset();
|
|
|
+
|
|
|
+ // 先设置响应头
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
+ String fileName = URLEncoder.encode("企业员工导入模板", "UTF-8").replaceAll("\\+", "%20");
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
|
|
+
|
|
|
+ // 查询数据
|
|
|
+ List<SysDictData> userAttrTypeList = dictDataMapper.selectDictDataByType("user_attr_type");
|
|
|
+ String userAttrName = null;
|
|
|
+ if (userAttrTypeList != null && !userAttrTypeList.isEmpty()) {
|
|
|
+ userAttrName = userAttrTypeList.stream()
|
|
|
+ .filter(data -> data.getDictCode() != 1L)
|
|
|
+ .map(SysDictData::getDictLabel)
|
|
|
+ .collect(Collectors.joining(";"));
|
|
|
}
|
|
|
|
|
|
+ // 准备数据
|
|
|
List<EnterpriseUserExcelInfo> list = Lists.newArrayList();
|
|
|
- //添加实例行
|
|
|
EnterpriseUserExcelInfo enterpriseUserExcelInfo = new EnterpriseUserExcelInfo();
|
|
|
enterpriseUserExcelInfo.setChannel("必填!");
|
|
|
enterpriseUserExcelInfo.setRealName("必填!");
|
|
|
- if (StringUtils.isNotEmpty(userAttrName)){
|
|
|
- enterpriseUserExcelInfo.setUserAttrName("必填!"+userAttrName);
|
|
|
- }else {
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(userAttrName)) {
|
|
|
+ enterpriseUserExcelInfo.setUserAttrName("必填!" + userAttrName);
|
|
|
+ } else {
|
|
|
enterpriseUserExcelInfo.setUserAttrName("必填!且值为1");
|
|
|
}
|
|
|
|
|
|
- enterpriseUserExcelInfo.setPhone("必填!可必须输入真实手机号,且该手机号码有微信");
|
|
|
+ enterpriseUserExcelInfo.setPhone("必填!必须输入真实手机号,且该手机号码有微信");
|
|
|
list.add(enterpriseUserExcelInfo);
|
|
|
- response.setCharacterEncoding("utf-8");
|
|
|
- String fileName = URLEncoder.encode("企业员工导入模板", "UTF-8").replaceAll("\\+", "%20");
|
|
|
- response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
|
|
- EasyExcel.write(response.getOutputStream(), EnterpriseUserExcelInfo.class)
|
|
|
+
|
|
|
+ // 获取输出流
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+
|
|
|
+ // 写入Excel
|
|
|
+ EasyExcel.write(outputStream, EnterpriseUserExcelInfo.class)
|
|
|
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
|
|
- .registerWriteHandler(new CellWriteHandler() {
|
|
|
- @Override
|
|
|
- public void afterCellDispose(CellWriteHandlerContext context) {
|
|
|
- // 为第一行(示例行)设置特殊样式
|
|
|
- if (context.getRowIndex() == 0) {
|
|
|
- Cell cell = context.getCell();
|
|
|
- CellStyle cellStyle = cell.getSheet().getWorkbook().createCellStyle();
|
|
|
- Font font = cell.getSheet().getWorkbook().createFont();
|
|
|
- font.setColor(IndexedColors.RED.getIndex()); // 红色字体
|
|
|
- font.setBold(true); // 加粗
|
|
|
- cellStyle.setFont(font);
|
|
|
- cell.setCellStyle(cellStyle);
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
- .sheet("列表").doWrite(list);
|
|
|
- } catch (IOException e) {
|
|
|
- throw new GlobalException("文件下载异常");
|
|
|
+ .sheet("列表")
|
|
|
+ .doWrite(list);
|
|
|
+ // 刷新流
|
|
|
+ outputStream.flush();
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("下载Excel模板失败", e);
|
|
|
}
|
|
|
}
|
|
|
|