班次奶量导出功能修复

This commit is contained in:
ll 2025-08-28 11:12:31 +08:00
parent 84bc894668
commit 29efc43392
4 changed files with 85 additions and 48 deletions

View File

@ -33,6 +33,11 @@ public @interface Excel
*/
public String dateFormat() default "";
/**
* 数字格式, : 0.00
*/
public String numFormat() default ""; // 新增数字格式属性
/**
* 如果是字典类型请设置字典的type值 (: sys_user_sex)
*/

View File

@ -1142,7 +1142,6 @@ public class ExcelUtil<T>
sheet.addMergedRegion(new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column));
}
}
cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType(), attr.wrapText())));
// 用于读取对象中的属性
Object value = getTargetValue(vo, field, attr);
@ -1150,14 +1149,33 @@ public class ExcelUtil<T>
String readConverterExp = attr.readConverterExp();
String separator = attr.separator();
String dictType = attr.dictType();
if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
String numFormat = attr.numFormat(); // 获取数字格式
// 创建单元格样式
CellStyle cellStyle = wb.createCellStyle();
cellStyle.cloneStyleFrom(styles.get(StringUtils.format("data_{}_{}_{}_{}_{}",
attr.align(), attr.color(), attr.backgroundColor(), attr.cellType(), attr.wrapText())));
// 处理日期格式
if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value) && value instanceof Date)
{
cell.getCellStyle().setDataFormat(this.wb.getCreationHelper().createDataFormat().getFormat(dateFormat));
cell.setCellValue(parseDateToStr(dateFormat, value));
DataFormat format = wb.createDataFormat();
cellStyle.setDataFormat(format.getFormat(dateFormat));
cell.setCellValue((Date) value);
cell.setCellStyle(cellStyle);
}
// 处理数字格式
else if (StringUtils.isNotEmpty(numFormat) && StringUtils.isNotNull(value) && value instanceof Number)
{
DataFormat format = wb.createDataFormat();
cellStyle.setDataFormat(format.getFormat(numFormat));
cell.setCellValue(((Number) value).doubleValue());
cell.setCellStyle(cellStyle);
}
else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
{
cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
cell.setCellStyle(cellStyle);
}
else if (StringUtils.isNotEmpty(dictType) && StringUtils.isNotNull(value))
{
@ -1167,20 +1185,32 @@ public class ExcelUtil<T>
sysDictMap.put(dictType + value, lable);
}
cell.setCellValue(sysDictMap.get(dictType + value));
cell.setCellStyle(cellStyle);
}
else if (value instanceof BigDecimal && -1 != attr.scale())
{
cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).doubleValue());
cell.setCellStyle(cellStyle);
}
else if (!attr.handler().equals(ExcelHandlerAdapter.class))
{
cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell));
cell.setCellStyle(cellStyle);
}
else
{
// 设置列类型
setCellVo(value, attr, cell);
// 对于日期类型确保应用日期格式
if (value instanceof Date && StringUtils.isNotEmpty(dateFormat)) {
DataFormat format = wb.createDataFormat();
CellStyle dateStyle = wb.createCellStyle();
dateStyle.cloneStyleFrom(cell.getCellStyle());
dateStyle.setDataFormat(format.getFormat(dateFormat));
cell.setCellStyle(dateStyle);
}
}
addStatisticsData(column, Convert.toStr(value), attr);
}
}
@ -1203,7 +1233,7 @@ public class ExcelUtil<T>
* @param endCol 结束列
*/
public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,
int firstCol, int endCol)
int firstCol, int endCol)
{
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
@ -1254,7 +1284,7 @@ public class ExcelUtil<T>
DataValidationHelper helper = sheet.getDataValidationHelper();
// 加载下拉列表内容
DataValidationConstraint constraint = helper.createFormulaListConstraint(hideSheetName + "_data");
// 设置数据有效性加载在哪个单元格上,四个参数分别是起始行终止行起始列终止列
// 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行终止行起始列终止列
CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
// 数据有效性对象
DataValidation dataValidation = helper.createValidation(constraint, regions);
@ -1754,7 +1784,7 @@ public class ExcelUtil<T>
* @param workbook 工作簿对象
* @return Map key:图片单元格索引1_1Stringvalue:图片流PictureData
*/
public static Map<String, List<PictureData>> getSheetPictures03(HSSFSheet sheet, HSSFWorkbook workbook)
public static Map<String, List<PictureData> > getSheetPictures03(HSSFSheet sheet, HSSFWorkbook workbook)
{
Map<String, List<PictureData>> sheetIndexPicMap = new HashMap<>();
List<HSSFPictureData> pictures = workbook.getAllPictures();

View File

@ -58,10 +58,12 @@ public class NpMilkProdClassesController extends BaseController {
public void export(HttpServletResponse response,
@RequestParam(required = false) Date datetimeStart,
@RequestParam(required = false) Date datetimeEnd,
@RequestParam(required = false) String manageEarNos,
@RequestParam(required = false) String manageEarNo,
@RequestParam(required = false) String factory,
@RequestParam(required = false) Integer classes) {
List<NpMilkProdClasses> list = npMilkProdClassesService.selectNpMilkProdClassesList(datetimeStart, datetimeEnd, manageEarNos, factory, classes);
List<NpMilkProdClasses> list = npMilkProdClassesService.selectNpMilkProdClassesList(
datetimeStart, datetimeEnd, manageEarNo, factory, classes);
ExcelUtil<NpMilkProdClasses> util = new ExcelUtil<>(NpMilkProdClasses.class);
util.exportExcel(response, list, "班次产奶数据");
}

View File

@ -15,7 +15,7 @@ public class NpMilkProdClasses implements Serializable {
private Date updateTime; // 更新时间
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "日期")
@Excel(name = "日期", dateFormat = "yyyy-MM-dd") // 添加日期格式
private Date datetime;
@Excel(name = "管理耳号")
@ -33,10 +33,10 @@ public class NpMilkProdClasses implements Serializable {
@Excel(name = "班次")
private Integer classes;
@Excel(name = "班次产奶量")
@Excel(name = "班次产奶量", numFormat = "0.00")
private Double milk;
@Excel(name = "班次校正奶量")
@Excel(name = "班次校正奶量", numFormat = "0.00")
private Double correctedMilk;
private String sheepId;