修改bug
This commit is contained in:
parent
9ae0be4345
commit
545f388192
|
|
@ -1,46 +1,31 @@
|
|||
package com.nu.modules.allMaterialInfo.controller;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.common.system.query.QueryGenerator;
|
||||
import org.jeecg.common.system.query.QueryRuleEnum;
|
||||
import org.jeecg.common.system.vo.LoginUser;
|
||||
import org.jeecg.common.util.oConvertUtils;
|
||||
import com.nu.modules.allMaterialInfo.entity.NuBizAllMaterialInfo;
|
||||
import com.nu.modules.allMaterialInfo.service.INuBizAllMaterialInfoService;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.jeecgframework.poi.excel.ExcelImportUtil;
|
||||
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
|
||||
import org.jeecgframework.poi.excel.entity.ExportParams;
|
||||
import org.jeecgframework.poi.excel.entity.ImportParams;
|
||||
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
|
||||
import org.jeecg.common.system.base.controller.JeecgController;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.nu.modules.allMaterialInfo.entity.NuBizAllMaterialInfo;
|
||||
import com.nu.modules.allMaterialInfo.service.INuBizAllMaterialInfoService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.jeecg.common.aspect.annotation.AutoLog;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.common.aspect.annotation.AutoLog;
|
||||
import org.jeecg.common.system.base.controller.JeecgController;
|
||||
import org.jeecg.common.system.query.QueryGenerator;
|
||||
import org.jeecg.common.util.oConvertUtils;
|
||||
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
|
||||
import org.jeecgframework.poi.excel.entity.ExportParams;
|
||||
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description: 物料信息汇总
|
||||
|
|
@ -155,53 +140,6 @@ public class NuBizAllMaterialInfoController extends JeecgController<NuBizAllMate
|
|||
return Result.OK(nuBizAllMaterialInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出excel
|
||||
*
|
||||
* @param request
|
||||
* @param nuBizAllMaterialInfo
|
||||
*/
|
||||
@RequestMapping(value = "/exportXls")
|
||||
public ModelAndView exportXls(HttpServletRequest request, NuBizAllMaterialInfo nuBizAllMaterialInfo) {
|
||||
// nuBizAllMaterialInfo.setSuppliersName("供应商");
|
||||
return exportXls(request, nuBizAllMaterialInfo, NuBizAllMaterialInfo.class, "供应商可提供的物料信息");
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出excel
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
protected ModelAndView exportXls(HttpServletRequest request, NuBizAllMaterialInfo object, Class<NuBizAllMaterialInfo> clazz, String title) {
|
||||
// Step.1 组装查询条件
|
||||
QueryWrapper<NuBizAllMaterialInfo> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
|
||||
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||
|
||||
// 过滤选中数据
|
||||
String selections = request.getParameter("selections");
|
||||
if (oConvertUtils.isNotEmpty(selections)) {
|
||||
List<String> selectionList = Arrays.asList(selections.split(","));
|
||||
queryWrapper.in("id",selectionList);
|
||||
}
|
||||
// Step.2 获取导出数据
|
||||
List<NuBizAllMaterialInfo> exportList = service.list(queryWrapper);
|
||||
exportList.forEach(item -> {
|
||||
item.setSuppliersName(object.getSuppliersName());
|
||||
});
|
||||
|
||||
// Step.3 AutoPoi 导出Excel
|
||||
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
|
||||
//此处设置的filename无效 ,前端会重更新设置一下
|
||||
mv.addObject(NormalExcelConstants.FILE_NAME, title);
|
||||
mv.addObject(NormalExcelConstants.CLASS, clazz);
|
||||
//update-begin--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置--------------------
|
||||
ExportParams exportParams=new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
|
||||
//update-end--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置----------------------
|
||||
mv.addObject(NormalExcelConstants.PARAMS,exportParams);
|
||||
mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
|
||||
return mv;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过excel导入数据
|
||||
*
|
||||
|
|
@ -215,4 +153,12 @@ public class NuBizAllMaterialInfoController extends JeecgController<NuBizAllMate
|
|||
return super.importExcel(request, response, NuBizAllMaterialInfo.class);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/exportXls")
|
||||
public void exportXls(NuBizAllMaterialInfo nuBizAllMaterialInfo,HttpServletResponse response) {
|
||||
nuBizAllMaterialInfoService.exportExcel(nuBizAllMaterialInfo,response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,4 +76,8 @@ public class NuBizAllMaterialInfo implements Serializable {
|
|||
@Excel(name = "销售单位", width = 15)
|
||||
@ApiModelProperty(value = "销售单位")
|
||||
private java.lang.String salesUnit;
|
||||
/**是否提供*/
|
||||
@Excel(name = "是否提供", width = 15, dicCode = "yn")
|
||||
@TableField(exist = false)
|
||||
private java.lang.String isSftg;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ package com.nu.modules.allMaterialInfo.service;
|
|||
import com.nu.modules.allMaterialInfo.entity.NuBizAllMaterialInfo;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @Description: 物料信息汇总
|
||||
* @Author: jeecg-boot
|
||||
|
|
@ -11,4 +13,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||
*/
|
||||
public interface INuBizAllMaterialInfoService extends IService<NuBizAllMaterialInfo> {
|
||||
|
||||
void exportExcel(NuBizAllMaterialInfo nuBizAllMaterialInfo,HttpServletResponse response);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,10 +3,30 @@ package com.nu.modules.allMaterialInfo.service.impl;
|
|||
import com.nu.modules.allMaterialInfo.entity.NuBizAllMaterialInfo;
|
||||
import com.nu.modules.allMaterialInfo.mapper.NuBizAllMaterialInfoMapper;
|
||||
import com.nu.modules.allMaterialInfo.service.INuBizAllMaterialInfoService;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.ss.util.CellRangeAddressList;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.nu.modules.allMaterialInfo.entity.NuBizAllMaterialInfo;
|
||||
import com.nu.modules.allMaterialInfo.mapper.NuBizAllMaterialInfoMapper;
|
||||
import com.nu.modules.allMaterialInfo.service.INuBizAllMaterialInfoService;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.usermodel.charts.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.jeecgframework.poi.excel.ExcelExportUtil;
|
||||
import org.jeecgframework.poi.excel.entity.ExportParams;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
/**
|
||||
* @Description: 物料信息汇总
|
||||
* @Author: jeecg-boot
|
||||
|
|
@ -16,4 +36,158 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|||
@Service
|
||||
public class NuBizAllMaterialInfoServiceImpl extends ServiceImpl<NuBizAllMaterialInfoMapper, NuBizAllMaterialInfo> implements INuBizAllMaterialInfoService {
|
||||
|
||||
}
|
||||
@Override
|
||||
public void exportExcel(NuBizAllMaterialInfo nuBizAllMaterialInfo,HttpServletResponse response) {
|
||||
// 1. 查询数据
|
||||
QueryWrapper<NuBizAllMaterialInfo> queryWrapper = new QueryWrapper<>();
|
||||
List<NuBizAllMaterialInfo> dataList = baseMapper.selectList(queryWrapper);
|
||||
|
||||
// 2. 设置响应头
|
||||
String fileName = "物料信息汇总_" + System.currentTimeMillis();
|
||||
try {
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
response.setHeader("Content-Disposition",
|
||||
"attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", StandardCharsets.UTF_8.toString()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 3. 创建Excel工作簿
|
||||
Workbook workbook = new XSSFWorkbook();
|
||||
Sheet sheet = workbook.createSheet("物料信息");
|
||||
|
||||
// 4. 创建表头样式(不可编辑)
|
||||
CellStyle lockedStyle = workbook.createCellStyle();
|
||||
lockedStyle.setLocked(true);
|
||||
lockedStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
||||
lockedStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
lockedStyle.setBorderBottom(BorderStyle.THIN);
|
||||
lockedStyle.setBorderTop(BorderStyle.THIN);
|
||||
lockedStyle.setBorderLeft(BorderStyle.THIN);
|
||||
lockedStyle.setBorderRight(BorderStyle.THIN);
|
||||
lockedStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
lockedStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
lockedStyle.setWrapText(true);
|
||||
|
||||
// 5. 创建可编辑样式
|
||||
CellStyle unlockedStyle = workbook.createCellStyle();
|
||||
unlockedStyle.setLocked(false);
|
||||
unlockedStyle.setBorderBottom(BorderStyle.THIN);
|
||||
unlockedStyle.setBorderTop(BorderStyle.THIN);
|
||||
unlockedStyle.setBorderLeft(BorderStyle.THIN);
|
||||
unlockedStyle.setBorderRight(BorderStyle.THIN);
|
||||
unlockedStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
unlockedStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
|
||||
// 创建可编辑样式
|
||||
CellStyle unlockedStyleRed = workbook.createCellStyle();
|
||||
unlockedStyleRed.setLocked(false);
|
||||
// unlockedStyleRed.setBorderBottom(BorderStyle.THIN);
|
||||
// unlockedStyleRed.setBorderTop(BorderStyle.THIN);
|
||||
// unlockedStyleRed.setBorderLeft(BorderStyle.THIN);
|
||||
// unlockedStyleRed.setBorderRight(BorderStyle.THIN);
|
||||
// unlockedStyleRed.setAlignment(HorizontalAlignment.CENTER);
|
||||
// unlockedStyleRed.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
// 创建红色字体
|
||||
Font redFont = workbook.createFont();
|
||||
redFont.setColor(IndexedColors.RED.getIndex()); // 设置字体颜色为红色
|
||||
// 可选:设置字体大小、加粗等
|
||||
// redFont.setFontHeightInPoints((short) 12);
|
||||
// redFont.setBold(true);
|
||||
|
||||
// 将红色字体应用到样式
|
||||
unlockedStyleRed.setFont(redFont);
|
||||
|
||||
// 6. 创建大标题行(第1行)
|
||||
Row titleRow = sheet.createRow(0);
|
||||
Cell materialTitleCell = titleRow.createCell(0);
|
||||
materialTitleCell.setCellValue("供应商名称:");
|
||||
materialTitleCell.setCellStyle(lockedStyle);
|
||||
Cell materialTitleCell2 = titleRow.createCell(1);
|
||||
materialTitleCell2.setCellValue("请填写供应商名称");
|
||||
materialTitleCell2.setCellStyle(unlockedStyleRed);
|
||||
sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 5)); // 合并 B1:G1
|
||||
|
||||
// 7. 创建列名行(第2行)
|
||||
Row headerRow = sheet.createRow(1);
|
||||
String[] headers = { "是否提供", "货品名称", "规格型号", "品牌型号", "销售单价", "销售单位"};
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
Cell cell = headerRow.createCell(i);
|
||||
cell.setCellValue(headers[i]);
|
||||
cell.setCellStyle(lockedStyle);
|
||||
}
|
||||
|
||||
// 8. 填充数据(从第3行开始)
|
||||
int rowNum = 2;
|
||||
for (NuBizAllMaterialInfo info : dataList) {
|
||||
Row row = sheet.createRow(rowNum++);
|
||||
|
||||
// 第1列:供应商名称 - 不可编辑
|
||||
setCellValueWithStyle(row, 0, info.getIsSftg(), unlockedStyle);
|
||||
// 第2-4列:货品名称、规格型号、品牌型号 - 不可编辑
|
||||
setCellValueWithStyle(row, 1, info.getMaterialName(), lockedStyle);
|
||||
setCellValueWithStyle(row, 2, info.getSpecificationModel(), lockedStyle);
|
||||
setCellValueWithStyle(row, 3, info.getBrandType(), lockedStyle);
|
||||
// 第5-6列:销售单价、销售单位 - 可编辑
|
||||
setCellValueWithStyle(row, 4, info.getSalesUnitPrice(), unlockedStyle);
|
||||
setCellValueWithStyle(row, 5, info.getSalesUnit(), unlockedStyle);
|
||||
// 第7列:是否提供 - 可编辑
|
||||
// setCellValueWithStyle(row, 6, info.getIsSftg(), unlockedStyle);
|
||||
}
|
||||
|
||||
// 9. 设置列宽
|
||||
int[] colWidths = {5000, 10000, 10000, 10000, 5000, 5000};
|
||||
for (int i = 0; i < colWidths.length; i++) {
|
||||
sheet.setColumnWidth(i, colWidths[i]);
|
||||
}
|
||||
|
||||
// 10. 冻结前两行(大标题 + 列名)
|
||||
sheet.createFreezePane(0, 2, 0, 2);
|
||||
|
||||
// 11. 设置“是否提供”列为下拉选(第7列,index=6)
|
||||
String[] options = {"是", "否"};
|
||||
DataValidationHelper validationHelper = sheet.getDataValidationHelper();
|
||||
DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(options);
|
||||
CellRangeAddressList addressList = new CellRangeAddressList(2, 65535, 0, 0); // 从第3行开始
|
||||
DataValidation validation = validationHelper.createValidation(constraint, addressList);
|
||||
validation.setShowErrorBox(true);
|
||||
validation.setShowPromptBox(true);
|
||||
validation.createPromptBox("提示", "请从下拉框选择:是或否");
|
||||
validation.createErrorBox("输入错误", "只能填写是或否");
|
||||
sheet.addValidationData(validation);
|
||||
|
||||
// 12. 设置工作表保护
|
||||
sheet.protectSheet("123456");
|
||||
|
||||
// 13. 写入响应流
|
||||
try {
|
||||
workbook.write(response.getOutputStream());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
workbook.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置单元格值并应用样式
|
||||
*/
|
||||
private void setCellValueWithStyle(Row row, int column, Object value, CellStyle style) {
|
||||
Cell cell = row.createCell(column);
|
||||
if (value == null) {
|
||||
cell.setCellValue("");
|
||||
} else if (value instanceof Number) {
|
||||
cell.setCellValue(((Number) value).doubleValue());
|
||||
} else if (value instanceof java.util.Date) {
|
||||
cell.setCellValue((java.util.Date) value);
|
||||
} else {
|
||||
cell.setCellValue(String.valueOf(value));
|
||||
}
|
||||
cell.setCellStyle(style);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue