核算结果导出数值项excel格式为数值
This commit is contained in:
parent
b0a25f6cf0
commit
efa818f8b9
|
|
@ -13,6 +13,11 @@ public class WeaTableColumnGroup extends WeaTableColumn {
|
|||
*/
|
||||
private String lockStatus;
|
||||
|
||||
/**
|
||||
* 保留小数位数
|
||||
*/
|
||||
private Integer pattern;
|
||||
|
||||
private List<WeaTableColumnGroup> children;
|
||||
|
||||
public WeaTableColumnGroup() {
|
||||
|
|
@ -20,22 +25,32 @@ public class WeaTableColumnGroup extends WeaTableColumn {
|
|||
|
||||
public WeaTableColumnGroup(String width, String text, String column) {
|
||||
super(width, text, column);
|
||||
this.pattern = 0;
|
||||
}
|
||||
|
||||
public WeaTableColumnGroup(String width, String text, String column, String lockStatus) {
|
||||
super(width, text, column);
|
||||
this.lockStatus = lockStatus;
|
||||
this.pattern = 0;
|
||||
}
|
||||
|
||||
public WeaTableColumnGroup(String width, String text, String column, String lockStatus, Integer pattern) {
|
||||
super(width, text, column);
|
||||
this.lockStatus = lockStatus;
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
|
||||
public WeaTableColumnGroup(String width, String text, String column, List<WeaTableColumnGroup> children) {
|
||||
super(width, text, column);
|
||||
this.children = children;
|
||||
this.pattern = 0;
|
||||
}
|
||||
|
||||
public WeaTableColumnGroup(String width, String text, String column, List<WeaTableColumnGroup> children, String lockStatus) {
|
||||
super(width, text, column);
|
||||
this.children = children;
|
||||
this.lockStatus = lockStatus;
|
||||
this.pattern = 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,9 +140,9 @@ public class SalaryAcctResultBO {
|
|||
List<WeaTableColumnGroup> childrenColumns = Lists.newArrayList();
|
||||
for (SalarySobItemDTO salarySobItemDTO : salarySobItemGroupDTO.getItems()) {
|
||||
if (lockSalaryItemIds.contains(salarySobItemDTO.getSalaryItemId())) {
|
||||
childrenColumns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.LOCK.getValue()));
|
||||
childrenColumns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.LOCK.getValue(), salarySobItemDTO.getPattern()));
|
||||
} else {
|
||||
childrenColumns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.UNLOCK.getValue()));
|
||||
childrenColumns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.UNLOCK.getValue(), salarySobItemDTO.getPattern()));
|
||||
}
|
||||
}
|
||||
WeaTableColumnGroup weaTableColumnWapper = new WeaTableColumnGroup("150", salarySobItemGroupDTO.getName(), String.valueOf(salarySobItemGroupDTO.getId()), childrenColumns);
|
||||
|
|
@ -151,18 +151,18 @@ public class SalaryAcctResultBO {
|
|||
// 没有分类的薪资项目
|
||||
for (SalarySobItemDTO salarySobItemDTO : salarySobItemAggregateDTO.getItems()) {
|
||||
if (lockSalaryItemIds.contains(salarySobItemDTO.getSalaryItemId())) {
|
||||
columns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.LOCK.getValue()));
|
||||
columns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.LOCK.getValue(), salarySobItemDTO.getPattern()));
|
||||
} else {
|
||||
columns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.UNLOCK.getValue()));
|
||||
columns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.UNLOCK.getValue(), salarySobItemDTO.getPattern()));
|
||||
}
|
||||
|
||||
}
|
||||
// 回算的薪资项目
|
||||
for (SalarySobItemDTO salarySobItemDTO : salarySobItemAggregateDTO.getBackCalcItems()) {
|
||||
if (lockSalaryItemIds.contains(salarySobItemDTO.getSalaryItemId())) {
|
||||
columns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.LOCK.getValue()));
|
||||
columns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.LOCK.getValue(), salarySobItemDTO.getPattern()));
|
||||
} else {
|
||||
columns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.UNLOCK.getValue()));
|
||||
columns.add(new WeaTableColumnGroup("150", salarySobItemDTO.getName(), "" + salarySobItemDTO.getSalaryItemId(), LockStatusEnum.UNLOCK.getValue(), salarySobItemDTO.getPattern()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.ListUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
|
@ -61,6 +62,7 @@ import weaver.file.ImageFileManager;
|
|||
import weaver.hrm.User;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
|
@ -330,6 +332,11 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc
|
|||
if (openSum != null && StringUtils.isNotBlank(openSum.getConfValue()) && OpenEnum.parseByValue(openSum.getConfValue()) == OpenEnum.OPEN) {
|
||||
total = true;
|
||||
Map<String, Object> sumRow = getSalaryAcctResultService(user).sumRow(queryParam);
|
||||
sumRow.forEach((k,v) -> {
|
||||
if (NumberUtils.isCreatable(v.toString())) {
|
||||
sumRow.put(k,new BigDecimal(v.toString()));
|
||||
}
|
||||
});
|
||||
if(sumRow !=null){
|
||||
sumRow.put("taxAgentName", "总计");
|
||||
resultMapList.add(sumRow);
|
||||
|
|
@ -337,19 +344,26 @@ public class SalaryAcctExcelServiceImpl extends Service implements SalaryAcctExc
|
|||
}
|
||||
|
||||
// excel导出的数据
|
||||
String DATA_TYPE_SUFFIX = "_type";
|
||||
List<List<Object>> rows = new ArrayList<>();
|
||||
rows.add(headerList);
|
||||
for (Map<String, Object> map : resultMapList) {
|
||||
List<Object> row = Lists.newArrayListWithExpectedSize(headerColumnGroup.size());
|
||||
for (WeaTableColumnGroup weaTableColumn : headerColumnGroup) {
|
||||
row.add(map.getOrDefault(weaTableColumn.getColumn(), StringUtils.EMPTY));
|
||||
String fieldType = (String) map.getOrDefault(weaTableColumn.getColumn().toString() + DATA_TYPE_SUFFIX, StringUtils.EMPTY);
|
||||
if (StringUtils.equals("number", fieldType)) {
|
||||
row.add(new BigDecimal(StringUtils.isBlank(map.get(weaTableColumn.getColumn()).toString()) ? "0" :map.get(weaTableColumn.getColumn()).toString()));
|
||||
} else {
|
||||
row.add(map.getOrDefault(weaTableColumn.getColumn(), StringUtils.EMPTY));
|
||||
}
|
||||
|
||||
}
|
||||
rows.add(row);
|
||||
}
|
||||
|
||||
String sheetName = "薪资核算结果";
|
||||
// return ExcelUtil.genWorkbookV2(rows, sheetName, total);
|
||||
return ExcelUtilPlus.genWorkbookWithChildTitleColumn(rows, sheetName, total);
|
||||
return ExcelUtilPlus.genWorkbookWithChildTitleColumnWithExcelFormat(rows, sheetName, total);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,10 +6,16 @@ import org.apache.commons.collections4.CollectionUtils;
|
|||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.xssf.usermodel.*;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ExcelUtilPlus {
|
||||
/**
|
||||
|
|
@ -342,7 +348,8 @@ public class ExcelUtilPlus {
|
|||
return workbook;
|
||||
}
|
||||
|
||||
public static XSSFWorkbook genWorkbookWithChildTitleColumn(List<List<Object>> rowList, String sheetName, boolean lastRowRed) {
|
||||
// 数值项目修改excel单元格格式为数值
|
||||
public static XSSFWorkbook genWorkbookWithChildTitleColumnWithExcelFormat(List<List<Object>> rowList, String sheetName, boolean lastRowRed) {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
|
||||
// 设置title样式
|
||||
|
|
@ -413,6 +420,195 @@ public class ExcelUtilPlus {
|
|||
XSSFRow row0 = sheet.createRow(0);
|
||||
XSSFRow row1 = sheet.createRow(1);
|
||||
|
||||
// 保留小数位数
|
||||
List<Integer> patternList = new ArrayList<>();
|
||||
List<Object> header = rowList.get(0);
|
||||
int startIndex = 0;
|
||||
for (int i = 0; i < header.size(); i++) {
|
||||
WeaTableColumnGroup columnGroupItem = (WeaTableColumnGroup) header.get(i);
|
||||
if (columnGroupItem.getChildren() == null) {
|
||||
sheet.addMergedRegion(new CellRangeAddress(0, 1, startIndex, startIndex));
|
||||
|
||||
XSSFCell rowZeroCell = row0.createCell(startIndex, CellType.STRING);
|
||||
rowZeroCell.setCellValue(columnGroupItem.getText().toString());
|
||||
rowZeroCell.setCellStyle(titleCellStyle);
|
||||
XSSFCell rowOneCell = row1.createCell(startIndex, CellType.STRING);
|
||||
rowOneCell.setCellValue(columnGroupItem.getText().toString());
|
||||
rowOneCell.setCellStyle(titleCellStyle);
|
||||
//设置列宽
|
||||
sheet.setColumnWidth(startIndex,Math.max(12, columnGroupItem.getText().length()*4)*256);
|
||||
startIndex++;
|
||||
patternList.add(columnGroupItem.getPattern());
|
||||
} else {
|
||||
List<WeaTableColumnGroup> childrenList = columnGroupItem.getChildren();
|
||||
int endIndex = startIndex + childrenList.size() - 1;
|
||||
|
||||
if (endIndex > startIndex) {
|
||||
sheet.addMergedRegion(new CellRangeAddress(0, 0, startIndex, endIndex));
|
||||
}
|
||||
|
||||
XSSFCell cell = row0.createCell(startIndex, CellType.STRING);
|
||||
cell.setCellValue(columnGroupItem.getText().toString());
|
||||
cell.setCellStyle(childTitleCellStyle);
|
||||
|
||||
for (int j = 0; j < childrenList.size(); j++) {
|
||||
WeaTableColumnGroup childrenItem = (WeaTableColumnGroup) childrenList.get(j);
|
||||
|
||||
XSSFCell subHeader = row1.createCell(startIndex + j, CellType.STRING);
|
||||
subHeader.setCellValue(childrenItem.getText().toString());
|
||||
subHeader.setCellStyle(titleCellStyle);
|
||||
//设置列宽
|
||||
sheet.setColumnWidth(startIndex + j,Math.max(12, childrenItem.getText().length()*4)*256);
|
||||
patternList.add(childrenItem.getPattern());
|
||||
}
|
||||
|
||||
startIndex += childrenList.size();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
HashMap<Integer, XSSFCellStyle> numberCellStyleMap = new HashMap<Integer, XSSFCellStyle>();
|
||||
HashMap<Integer, XSSFCellStyle> numberRedCellStyleMap = new HashMap<Integer, XSSFCellStyle>();
|
||||
XSSFDataFormat df = workbook.createDataFormat();
|
||||
patternList.stream().distinct().forEach(p -> {
|
||||
String start = "0.";
|
||||
if (p==0) {
|
||||
start ="0";
|
||||
}
|
||||
XSSFCellStyle numberRedCellStyle = workbook.createCellStyle();
|
||||
BeanUtils.copyProperties(redCellStyle, numberRedCellStyle);
|
||||
numberRedCellStyle.setFont(redFont);
|
||||
short format = df.getFormat(start + Stream.generate(() -> "0").limit(p).collect(Collectors.joining()) + "_ ");
|
||||
numberRedCellStyle.setDataFormat(format);
|
||||
// 最后一行红色
|
||||
numberRedCellStyleMap.put(p, numberRedCellStyle);
|
||||
XSSFCellStyle numberCellStyle = workbook.createCellStyle();
|
||||
BeanUtils.copyProperties(cellStyle, numberCellStyle);
|
||||
numberCellStyle.setDataFormat(format);
|
||||
numberCellStyleMap.put(p, numberCellStyle);
|
||||
});
|
||||
|
||||
for (int rowIndex = 1; rowIndex < rowList.size(); rowIndex++) {
|
||||
List<Object> infoList = rowList.get(rowIndex);
|
||||
XSSFRow row = sheet.createRow(rowIndex + 1);
|
||||
float height = 18;
|
||||
float finalHeight = 18;
|
||||
|
||||
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
|
||||
XSSFCell cell = row.createCell(cellIndex);
|
||||
|
||||
if (rowIndex == 0) {
|
||||
cell.setCellStyle(titleCellStyle);
|
||||
} else {
|
||||
if (lastRowRed && rowIndex == rowList.size() - 1) {
|
||||
cell.setCellStyle(redCellStyle);
|
||||
} else {
|
||||
cell.setCellStyle(cellStyle);
|
||||
}
|
||||
}
|
||||
Object o = infoList.get(cellIndex);
|
||||
if (o instanceof String) {
|
||||
cell.setCellType(CellType.STRING);
|
||||
cell.setCellValue(String.valueOf(o));
|
||||
} else if (o instanceof BigDecimal) {
|
||||
cell.setCellType(CellType.NUMERIC);
|
||||
cell.setCellValue(o == null ? 0 : ((BigDecimal) o).doubleValue());
|
||||
if (lastRowRed && rowIndex == rowList.size() - 1) {
|
||||
cell.setCellStyle(numberRedCellStyleMap.get(patternList.get(cellIndex)));
|
||||
} else {
|
||||
cell.setCellStyle(numberCellStyleMap.get(patternList.get(cellIndex)));
|
||||
}
|
||||
} else if (o instanceof Boolean) {
|
||||
cell.setCellType(CellType.BOOLEAN);
|
||||
cell.setCellValue(String.valueOf(o));
|
||||
} else if (o instanceof Date) {
|
||||
cell.setCellType(CellType.STRING);
|
||||
cell.setCellValue(SalaryDateUtil.getFormatLocalDate((Date) o));
|
||||
} else {
|
||||
cell.setCellType(CellType.STRING);
|
||||
cell.setCellValue(o == null ? "" : o.toString());
|
||||
}
|
||||
|
||||
//判断是否要调整高度
|
||||
int width = sheet.getColumnWidth(cellIndex) / 256;
|
||||
finalHeight = getFinalHeight(o, width, finalHeight, height);
|
||||
}
|
||||
row.setHeightInPoints(finalHeight);
|
||||
}
|
||||
return workbook;
|
||||
}
|
||||
|
||||
public static XSSFWorkbook genWorkbookWithChildTitleColumn(List<List<Object>> rowList, String sheetName, boolean lastRowRed) {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
|
||||
// 设置title样式
|
||||
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
|
||||
XSSFFont titleFont = workbook.createFont();
|
||||
titleFont.setBold(true);
|
||||
titleFont.setFontName("仿宋");
|
||||
titleFont.setFontHeightInPoints((short) 15);
|
||||
titleCellStyle.setFont(titleFont);
|
||||
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色
|
||||
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
titleCellStyle.setBorderLeft(BorderStyle.THIN);
|
||||
titleCellStyle.setBorderRight(BorderStyle.THIN);
|
||||
titleCellStyle.setBorderTop(BorderStyle.THIN);
|
||||
titleCellStyle.setBorderBottom(BorderStyle.THIN);
|
||||
|
||||
XSSFCellStyle childTitleCellStyle = workbook.createCellStyle();
|
||||
childTitleCellStyle.setFont(titleFont);
|
||||
childTitleCellStyle.setAlignment(HorizontalAlignment.LEFT);
|
||||
childTitleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色
|
||||
childTitleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
childTitleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
childTitleCellStyle.setBorderLeft(BorderStyle.THIN);
|
||||
childTitleCellStyle.setBorderRight(BorderStyle.THIN);
|
||||
childTitleCellStyle.setBorderTop(BorderStyle.THIN);
|
||||
childTitleCellStyle.setBorderBottom(BorderStyle.THIN);
|
||||
|
||||
|
||||
|
||||
// 设置主体样式
|
||||
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
||||
XSSFFont font = workbook.createFont();
|
||||
font.setFontName("宋体");
|
||||
font.setFontHeightInPoints((short) 10);// 设置字体大小
|
||||
cellStyle.setFont(font);// 选择需要用到的字体格式
|
||||
cellStyle.setWrapText(true);
|
||||
|
||||
cellStyle.setBorderLeft(BorderStyle.THIN);
|
||||
cellStyle.setBorderRight(BorderStyle.THIN);
|
||||
cellStyle.setBorderTop(BorderStyle.THIN);
|
||||
cellStyle.setBorderBottom(BorderStyle.THIN);
|
||||
|
||||
XSSFCellStyle redCellStyle = workbook.createCellStyle();
|
||||
XSSFFont redFont = workbook.createFont();
|
||||
redFont.setFontName("宋体");
|
||||
redFont.setFontHeightInPoints((short) 10);// 设置字体大小
|
||||
redFont.setColor(new XSSFColor(new Color(0xFF3333), null));
|
||||
redFont.setBold(true);
|
||||
redCellStyle.setWrapText(true);
|
||||
redCellStyle.setFont(redFont);// 选择需要用到的字体格式
|
||||
|
||||
redCellStyle.setBorderLeft(BorderStyle.THIN);
|
||||
redCellStyle.setBorderRight(BorderStyle.THIN);
|
||||
redCellStyle.setBorderTop(BorderStyle.THIN);
|
||||
redCellStyle.setBorderBottom(BorderStyle.THIN);
|
||||
|
||||
XSSFSheet sheet = workbook.createSheet(sheetName);
|
||||
//自适应宽度
|
||||
sheet.autoSizeColumn(0, true);
|
||||
//默认列宽
|
||||
sheet.setDefaultColumnWidth(20);
|
||||
//默认行高
|
||||
sheet.setDefaultRowHeightInPoints(18);
|
||||
|
||||
//处理合并单元格
|
||||
XSSFRow row0 = sheet.createRow(0);
|
||||
XSSFRow row1 = sheet.createRow(1);
|
||||
List<Object> header = rowList.get(0);
|
||||
int startIndex = 0;
|
||||
for (int i = 0; i < header.size(); i++) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue