2022-03-10 11:09:08 +08:00
|
|
|
|
package com.engine.salary.util.excel;
|
|
|
|
|
|
|
2025-05-20 16:44:46 +08:00
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
2024-01-23 14:06:56 +08:00
|
|
|
|
import com.engine.salary.entity.taxdeclaration.dto.TaxDeclarationLaborListDTO;
|
2022-03-16 14:29:02 +08:00
|
|
|
|
import com.engine.salary.util.SalaryDateUtil;
|
2023-12-28 15:47:05 +08:00
|
|
|
|
import com.engine.salary.util.SalaryI18nUtil;
|
2024-01-23 14:06:56 +08:00
|
|
|
|
import com.google.common.collect.Lists;
|
2022-03-29 17:10:59 +08:00
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
2022-03-10 11:09:08 +08:00
|
|
|
|
import org.apache.poi.ss.usermodel.CellType;
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.FillPatternType;
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
|
|
|
|
|
import org.apache.poi.xssf.usermodel.*;
|
2024-03-18 14:01:08 +08:00
|
|
|
|
import org.springframework.beans.BeanUtils;
|
2022-03-10 11:09:08 +08:00
|
|
|
|
|
2022-09-28 16:59:51 +08:00
|
|
|
|
import java.awt.*;
|
2024-01-23 14:06:56 +08:00
|
|
|
|
import java.beans.BeanInfo;
|
|
|
|
|
|
import java.beans.Introspector;
|
|
|
|
|
|
import java.beans.PropertyDescriptor;
|
2023-12-28 15:47:05 +08:00
|
|
|
|
import java.lang.reflect.Field;
|
2024-01-23 14:06:56 +08:00
|
|
|
|
import java.lang.reflect.Method;
|
2024-03-18 14:01:08 +08:00
|
|
|
|
import java.math.BigDecimal;
|
2024-01-23 14:06:56 +08:00
|
|
|
|
import java.util.ArrayList;
|
2022-03-16 14:29:02 +08:00
|
|
|
|
import java.util.Date;
|
2022-03-10 11:09:08 +08:00
|
|
|
|
import java.util.List;
|
2024-03-18 14:01:08 +08:00
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import java.util.stream.Stream;
|
2022-03-10 11:09:08 +08:00
|
|
|
|
|
|
|
|
|
|
public class ExcelUtil {
|
2024-03-18 14:01:08 +08:00
|
|
|
|
|
2022-03-10 11:09:08 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 生成excel
|
2022-03-16 14:29:02 +08:00
|
|
|
|
*
|
2022-03-10 11:09:08 +08:00
|
|
|
|
* @param rowList
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2022-03-16 14:29:02 +08:00
|
|
|
|
public static XSSFWorkbook genWorkbook(List<List<String>> rowList, String sheetName) {
|
2022-03-10 11:09:08 +08:00
|
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
|
|
|
|
|
|
|
|
|
|
|
// 设置title样式
|
|
|
|
|
|
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont titleFont = workbook.createFont();
|
|
|
|
|
|
titleFont.setFontName("仿宋");
|
|
|
|
|
|
titleFont.setFontHeightInPoints((short) 15);
|
|
|
|
|
|
titleCellStyle.setFont(titleFont);
|
|
|
|
|
|
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
|
2023-04-23 13:35:17 +08:00
|
|
|
|
titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色
|
2022-03-10 11:09:08 +08:00
|
|
|
|
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置主体样式
|
|
|
|
|
|
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont font = workbook.createFont();
|
|
|
|
|
|
font.setFontName("宋体");
|
|
|
|
|
|
font.setFontHeightInPoints((short) 10);// 设置字体大小
|
|
|
|
|
|
cellStyle.setFont(font);// 选择需要用到的字体格式
|
|
|
|
|
|
cellStyle.setWrapText(true);
|
|
|
|
|
|
|
2022-03-10 17:57:46 +08:00
|
|
|
|
XSSFSheet sheet = workbook.createSheet(sheetName);
|
2022-03-10 11:09:08 +08:00
|
|
|
|
//自适应宽度
|
|
|
|
|
|
sheet.autoSizeColumn(0, true);
|
|
|
|
|
|
//默认列宽
|
|
|
|
|
|
sheet.setDefaultColumnWidth(20);
|
|
|
|
|
|
//默认行高
|
|
|
|
|
|
sheet.setDefaultRowHeightInPoints(18);
|
|
|
|
|
|
|
|
|
|
|
|
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
|
|
|
|
|
|
List<String> infoList = rowList.get(rowIndex);
|
|
|
|
|
|
XSSFRow row = sheet.createRow(rowIndex);
|
|
|
|
|
|
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
|
|
|
|
|
|
XSSFCell cell = row.createCell(cellIndex);
|
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
|
if (rowIndex == 0) {
|
|
|
|
|
|
cell.setCellStyle(titleCellStyle);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
|
}
|
|
|
|
|
|
cell.setCellValue(infoList.get(cellIndex));
|
|
|
|
|
|
// sheet.setColumnWidth(cellIndex, 35 * 256);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return workbook;
|
|
|
|
|
|
}
|
2022-03-16 14:29:02 +08:00
|
|
|
|
|
2024-03-18 14:01:08 +08:00
|
|
|
|
// 设置默认舍入位数,可设为null
|
|
|
|
|
|
public static XSSFWorkbook genWorkbookWithDefaultPattern(List<List<Object>> rowList, String sheetName, Integer defaultPattern) {
|
|
|
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
|
|
|
|
|
|
|
|
|
|
|
// 设置title样式
|
|
|
|
|
|
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont titleFont = workbook.createFont();
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置主体样式
|
|
|
|
|
|
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont font = workbook.createFont();
|
|
|
|
|
|
font.setFontName("宋体");
|
|
|
|
|
|
font.setFontHeightInPoints((short) 10);// 设置字体大小
|
|
|
|
|
|
cellStyle.setFont(font);// 选择需要用到的字体格式
|
|
|
|
|
|
cellStyle.setWrapText(true);
|
|
|
|
|
|
|
|
|
|
|
|
XSSFSheet sheet = workbook.createSheet(sheetName);
|
|
|
|
|
|
//自适应宽度
|
|
|
|
|
|
sheet.autoSizeColumn(0, true);
|
|
|
|
|
|
//默认列宽
|
|
|
|
|
|
sheet.setDefaultColumnWidth(20);
|
|
|
|
|
|
//默认行高
|
|
|
|
|
|
sheet.setDefaultRowHeightInPoints(18);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
XSSFCellStyle numberCellStyle = null;
|
|
|
|
|
|
if (defaultPattern != null) {
|
|
|
|
|
|
XSSFDataFormat df = workbook.createDataFormat();
|
|
|
|
|
|
String start = "0.";
|
|
|
|
|
|
if (defaultPattern.equals(0)) {
|
|
|
|
|
|
start = "0";
|
|
|
|
|
|
}
|
|
|
|
|
|
short format = df.getFormat(start + Stream.generate(() -> "0").limit(defaultPattern).collect(Collectors.joining()) + "_ ");
|
|
|
|
|
|
numberCellStyle = workbook.createCellStyle();
|
|
|
|
|
|
BeanUtils.copyProperties(cellStyle, numberCellStyle);
|
|
|
|
|
|
numberCellStyle.setDataFormat(format);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
|
|
|
|
|
|
List<Object> infoList = rowList.get(rowIndex);
|
|
|
|
|
|
XSSFRow row = sheet.createRow(rowIndex);
|
|
|
|
|
|
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
|
|
|
|
|
|
XSSFCell cell = row.createCell(cellIndex);
|
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
|
if (rowIndex == 0) {
|
|
|
|
|
|
cell.setCellStyle(titleCellStyle);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
|
}
|
|
|
|
|
|
Object o = infoList.get(cellIndex);
|
|
|
|
|
|
if (o instanceof String) {
|
2025-05-20 16:44:46 +08:00
|
|
|
|
if (StrUtil.isNotBlank(String.valueOf(o))) {
|
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
|
cell.setCellValue(String.valueOf(o));
|
|
|
|
|
|
}
|
2024-03-18 14:01:08 +08:00
|
|
|
|
} else if (o instanceof BigDecimal) {
|
|
|
|
|
|
cell.setCellType(CellType.NUMERIC);
|
|
|
|
|
|
cell.setCellValue(o == null ? 0 : ((BigDecimal) o).doubleValue());
|
|
|
|
|
|
if (defaultPattern != null) {
|
|
|
|
|
|
cell.setCellStyle(numberCellStyle);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
|
cell.setCellValue(o == null ? "" : o.toString());
|
|
|
|
|
|
}
|
|
|
|
|
|
// sheet.setColumnWidth(cellIndex, 35 * 256);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return workbook;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-23 14:06:56 +08:00
|
|
|
|
public static <T> XSSFWorkbook genWorkbook(String sheetName, List<T> rowList) {
|
|
|
|
|
|
List<Object> headerList = Lists.newArrayList();
|
|
|
|
|
|
List<String> dataIndexList = Lists.newArrayList();
|
|
|
|
|
|
// 解析表头
|
|
|
|
|
|
ExcelUtil.parseHeader(TaxDeclarationLaborListDTO.class, headerList, dataIndexList);
|
|
|
|
|
|
|
|
|
|
|
|
List<List<Object>> rows = new ArrayList<>();
|
|
|
|
|
|
rows.add(headerList);
|
|
|
|
|
|
for (int i = 0; i < rowList.size(); i++) {
|
|
|
|
|
|
List<Object> row = Lists.newArrayListWithExpectedSize(dataIndexList.size());
|
|
|
|
|
|
for (int j = 0; j < dataIndexList.size(); j++) {
|
|
|
|
|
|
Object value = getValue(rowList.get(i), dataIndexList.get(j));
|
|
|
|
|
|
row.add(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
rows.add(row);
|
|
|
|
|
|
}
|
|
|
|
|
|
return genWorkbookV2(rows, sheetName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static <T> Object getValue(T t, String fieldName) {
|
|
|
|
|
|
Object value = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
|
|
|
|
|
|
PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
|
|
|
|
|
|
for (PropertyDescriptor property : props) {
|
|
|
|
|
|
if (fieldName.equals(property.getName())) {
|
|
|
|
|
|
Method method = property.getReadMethod();
|
|
|
|
|
|
value = method.invoke(t, new Object[]{});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}
|
2022-03-16 14:29:02 +08:00
|
|
|
|
|
|
|
|
|
|
public static XSSFWorkbook genWorkbookV2(List<List<Object>> rowList, String sheetName) {
|
|
|
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
|
|
|
|
|
|
|
|
|
|
|
// 设置title样式
|
|
|
|
|
|
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont titleFont = workbook.createFont();
|
|
|
|
|
|
titleFont.setFontName("仿宋");
|
|
|
|
|
|
titleFont.setFontHeightInPoints((short) 15);
|
|
|
|
|
|
titleCellStyle.setFont(titleFont);
|
|
|
|
|
|
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
|
2023-04-23 13:35:17 +08:00
|
|
|
|
titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色
|
2022-03-16 14:29:02 +08:00
|
|
|
|
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置主体样式
|
|
|
|
|
|
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont font = workbook.createFont();
|
|
|
|
|
|
font.setFontName("宋体");
|
|
|
|
|
|
font.setFontHeightInPoints((short) 10);// 设置字体大小
|
|
|
|
|
|
cellStyle.setFont(font);// 选择需要用到的字体格式
|
|
|
|
|
|
cellStyle.setWrapText(true);
|
|
|
|
|
|
|
|
|
|
|
|
XSSFSheet sheet = workbook.createSheet(sheetName);
|
|
|
|
|
|
//自适应宽度
|
|
|
|
|
|
sheet.autoSizeColumn(0, true);
|
|
|
|
|
|
//默认列宽
|
|
|
|
|
|
sheet.setDefaultColumnWidth(20);
|
|
|
|
|
|
//默认行高
|
|
|
|
|
|
sheet.setDefaultRowHeightInPoints(18);
|
|
|
|
|
|
|
|
|
|
|
|
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
|
|
|
|
|
|
List<Object> infoList = rowList.get(rowIndex);
|
|
|
|
|
|
XSSFRow row = sheet.createRow(rowIndex);
|
|
|
|
|
|
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
|
|
|
|
|
|
XSSFCell cell = row.createCell(cellIndex);
|
|
|
|
|
|
if (rowIndex == 0) {
|
|
|
|
|
|
cell.setCellStyle(titleCellStyle);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
|
}
|
|
|
|
|
|
Object o = infoList.get(cellIndex);
|
|
|
|
|
|
if (o instanceof String) {
|
2025-05-20 16:44:46 +08:00
|
|
|
|
if (StrUtil.isNotBlank(String.valueOf(o))) {
|
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
|
cell.setCellValue(String.valueOf(o));
|
|
|
|
|
|
}
|
2024-03-18 14:01:08 +08:00
|
|
|
|
} else if (o instanceof BigDecimal) {
|
|
|
|
|
|
cell.setCellType(CellType.NUMERIC);
|
|
|
|
|
|
cell.setCellValue(o == null ? 0 : ((BigDecimal) o).doubleValue());
|
|
|
|
|
|
} 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());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return workbook;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置默认舍入位数,可传null
|
|
|
|
|
|
public static XSSFWorkbook genWorkbookV2WithDefaultPattern(List<List<Object>> rowList, String sheetName, Integer defaultPattern) {
|
|
|
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
|
|
|
|
|
|
|
|
|
|
|
// 设置title样式
|
|
|
|
|
|
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont titleFont = workbook.createFont();
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置主体样式
|
|
|
|
|
|
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont font = workbook.createFont();
|
|
|
|
|
|
font.setFontName("宋体");
|
|
|
|
|
|
font.setFontHeightInPoints((short) 10);// 设置字体大小
|
|
|
|
|
|
cellStyle.setFont(font);// 选择需要用到的字体格式
|
|
|
|
|
|
cellStyle.setWrapText(true);
|
|
|
|
|
|
|
|
|
|
|
|
XSSFSheet sheet = workbook.createSheet(sheetName);
|
|
|
|
|
|
//自适应宽度
|
|
|
|
|
|
sheet.autoSizeColumn(0, true);
|
|
|
|
|
|
//默认列宽
|
|
|
|
|
|
sheet.setDefaultColumnWidth(20);
|
|
|
|
|
|
//默认行高
|
|
|
|
|
|
sheet.setDefaultRowHeightInPoints(18);
|
|
|
|
|
|
|
|
|
|
|
|
XSSFCellStyle numberCellStyle = null;
|
|
|
|
|
|
if (defaultPattern != null) {
|
|
|
|
|
|
XSSFDataFormat df = workbook.createDataFormat();
|
|
|
|
|
|
String start = "0.";
|
|
|
|
|
|
if (defaultPattern.equals(0)) {
|
|
|
|
|
|
start = "0";
|
|
|
|
|
|
}
|
|
|
|
|
|
short format = df.getFormat(start + Stream.generate(() -> "0").limit(defaultPattern).collect(Collectors.joining()) + "_ ");
|
|
|
|
|
|
numberCellStyle = workbook.createCellStyle();
|
|
|
|
|
|
BeanUtils.copyProperties(cellStyle, numberCellStyle);
|
|
|
|
|
|
numberCellStyle.setDataFormat(format);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
|
|
|
|
|
|
List<Object> infoList = rowList.get(rowIndex);
|
|
|
|
|
|
XSSFRow row = sheet.createRow(rowIndex);
|
|
|
|
|
|
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
|
|
|
|
|
|
XSSFCell cell = row.createCell(cellIndex);
|
|
|
|
|
|
if (rowIndex == 0) {
|
|
|
|
|
|
cell.setCellStyle(titleCellStyle);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
|
}
|
|
|
|
|
|
Object o = infoList.get(cellIndex);
|
|
|
|
|
|
if (o instanceof String) {
|
2025-05-20 16:44:46 +08:00
|
|
|
|
if (StrUtil.isNotBlank(String.valueOf(o))) {
|
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
|
cell.setCellValue(String.valueOf(o));
|
|
|
|
|
|
}
|
2024-03-18 14:01:08 +08:00
|
|
|
|
} else if (o instanceof BigDecimal) {
|
|
|
|
|
|
cell.setCellType(CellType.NUMERIC);
|
|
|
|
|
|
cell.setCellValue(o == null ? 0 : ((BigDecimal) o).doubleValue());
|
|
|
|
|
|
if (defaultPattern != null) {
|
|
|
|
|
|
cell.setCellStyle(numberCellStyle);
|
|
|
|
|
|
}
|
2022-03-16 14:29:02 +08:00
|
|
|
|
} else if (o instanceof Boolean) {
|
|
|
|
|
|
cell.setCellType(CellType.BOOLEAN);
|
|
|
|
|
|
cell.setCellValue(String.valueOf(o));
|
2022-03-29 17:10:59 +08:00
|
|
|
|
} else if (o instanceof Date) {
|
2022-03-16 14:29:02 +08:00
|
|
|
|
cell.setCellType(CellType.STRING);
|
2022-03-29 17:10:59 +08:00
|
|
|
|
cell.setCellValue(SalaryDateUtil.getFormatLocalDate((Date) o));
|
|
|
|
|
|
} else {
|
2022-03-16 14:29:02 +08:00
|
|
|
|
cell.setCellType(CellType.STRING);
|
2022-04-13 14:18:00 +08:00
|
|
|
|
cell.setCellValue(o == null ? "" : o.toString());
|
2022-03-16 14:29:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return workbook;
|
|
|
|
|
|
}
|
2022-03-29 17:10:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static XSSFWorkbook genWorkbookV2(List<List<Object>> rowList, String sheetName, List<ExcelComment> comments) {
|
|
|
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
|
|
|
|
|
|
|
|
|
|
|
// 设置title样式
|
|
|
|
|
|
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont titleFont = workbook.createFont();
|
|
|
|
|
|
titleFont.setFontName("仿宋");
|
|
|
|
|
|
titleFont.setFontHeightInPoints((short) 15);
|
|
|
|
|
|
titleCellStyle.setFont(titleFont);
|
|
|
|
|
|
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
|
2023-04-23 13:35:17 +08:00
|
|
|
|
titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色
|
2022-03-29 17:10:59 +08:00
|
|
|
|
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置主体样式
|
|
|
|
|
|
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont font = workbook.createFont();
|
|
|
|
|
|
font.setFontName("宋体");
|
|
|
|
|
|
font.setFontHeightInPoints((short) 10);// 设置字体大小
|
|
|
|
|
|
cellStyle.setFont(font);// 选择需要用到的字体格式
|
|
|
|
|
|
cellStyle.setWrapText(true);
|
|
|
|
|
|
|
|
|
|
|
|
XSSFSheet sheet = workbook.createSheet(sheetName);
|
|
|
|
|
|
//自适应宽度
|
|
|
|
|
|
sheet.autoSizeColumn(0, true);
|
|
|
|
|
|
//默认列宽
|
|
|
|
|
|
sheet.setDefaultColumnWidth(20);
|
|
|
|
|
|
//默认行高
|
|
|
|
|
|
sheet.setDefaultRowHeightInPoints(18);
|
|
|
|
|
|
|
|
|
|
|
|
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
|
|
|
|
|
|
List<Object> infoList = rowList.get(rowIndex);
|
|
|
|
|
|
XSSFRow row = sheet.createRow(rowIndex);
|
|
|
|
|
|
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
|
|
|
|
|
|
XSSFCell cell = row.createCell(cellIndex);
|
|
|
|
|
|
if (rowIndex == 0) {
|
|
|
|
|
|
cell.setCellStyle(titleCellStyle);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
|
}
|
|
|
|
|
|
Object o = infoList.get(cellIndex);
|
|
|
|
|
|
if (o instanceof String) {
|
2025-05-20 16:44:46 +08:00
|
|
|
|
if (StrUtil.isNotBlank(String.valueOf(o))) {
|
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
|
cell.setCellValue(String.valueOf(o));
|
|
|
|
|
|
}
|
2022-03-29 17:10:59 +08:00
|
|
|
|
} 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(String.valueOf(o));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-13 14:18:00 +08:00
|
|
|
|
if (CollectionUtils.isNotEmpty(comments)) {
|
|
|
|
|
|
for (ExcelComment c : comments) {
|
2022-03-29 17:10:59 +08:00
|
|
|
|
XSSFDrawing patr = sheet.createDrawingPatriarch();
|
|
|
|
|
|
XSSFComment cellComment = patr.createCellComment(new XSSFClientAnchor(c.dx1, c.dy1, c.dx2, c.dy2, c.col1, c.row1, c.col2, c.row2));
|
|
|
|
|
|
cellComment.setString(c.content);
|
2022-03-30 11:29:48 +08:00
|
|
|
|
}
|
2022-03-29 17:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
return workbook;
|
|
|
|
|
|
}
|
2022-09-28 16:59:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static XSSFWorkbook genWorkbookV2(List<List<Object>> rowList, String sheetName, boolean lastRowRed) {
|
|
|
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
|
|
|
|
|
|
|
|
|
|
|
// 设置title样式
|
|
|
|
|
|
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont titleFont = workbook.createFont();
|
|
|
|
|
|
titleFont.setFontName("仿宋");
|
|
|
|
|
|
titleFont.setFontHeightInPoints((short) 15);
|
|
|
|
|
|
titleCellStyle.setFont(titleFont);
|
|
|
|
|
|
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
|
2023-04-23 13:35:17 +08:00
|
|
|
|
titleCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景色
|
2022-09-28 16:59:51 +08:00
|
|
|
|
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置主体样式
|
|
|
|
|
|
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont font = workbook.createFont();
|
|
|
|
|
|
font.setFontName("宋体");
|
|
|
|
|
|
font.setFontHeightInPoints((short) 10);// 设置字体大小
|
|
|
|
|
|
cellStyle.setFont(font);// 选择需要用到的字体格式
|
|
|
|
|
|
cellStyle.setWrapText(true);
|
|
|
|
|
|
|
|
|
|
|
|
XSSFCellStyle redCellStyle = workbook.createCellStyle();
|
|
|
|
|
|
XSSFFont redFont = workbook.createFont();
|
|
|
|
|
|
redFont.setFontName("宋体");
|
|
|
|
|
|
redFont.setFontHeightInPoints((short) 10);// 设置字体大小
|
|
|
|
|
|
redFont.setColor(new XSSFColor(new Color(0xFF3333), null));
|
2023-08-18 17:58:36 +08:00
|
|
|
|
redFont.setBold(true);
|
2022-09-28 16:59:51 +08:00
|
|
|
|
redCellStyle.setWrapText(true);
|
|
|
|
|
|
redCellStyle.setFont(redFont);// 选择需要用到的字体格式
|
|
|
|
|
|
|
|
|
|
|
|
XSSFSheet sheet = workbook.createSheet(sheetName);
|
|
|
|
|
|
//自适应宽度
|
|
|
|
|
|
sheet.autoSizeColumn(0, true);
|
|
|
|
|
|
//默认列宽
|
|
|
|
|
|
sheet.setDefaultColumnWidth(20);
|
|
|
|
|
|
//默认行高
|
|
|
|
|
|
sheet.setDefaultRowHeightInPoints(18);
|
|
|
|
|
|
|
|
|
|
|
|
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
|
|
|
|
|
|
List<Object> infoList = rowList.get(rowIndex);
|
|
|
|
|
|
XSSFRow row = sheet.createRow(rowIndex);
|
|
|
|
|
|
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) {
|
2025-05-20 16:44:46 +08:00
|
|
|
|
if (StrUtil.isNotBlank(String.valueOf(o))) {
|
|
|
|
|
|
cell.setCellType(CellType.STRING);
|
|
|
|
|
|
cell.setCellValue(String.valueOf(o));
|
|
|
|
|
|
}
|
2022-09-28 16:59:51 +08:00
|
|
|
|
} 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());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return workbook;
|
|
|
|
|
|
}
|
2023-12-28 15:47:05 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 解析表头
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param clazz
|
|
|
|
|
|
* @param headerList
|
|
|
|
|
|
* @param dataIndexList
|
|
|
|
|
|
* @param <T>
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static <T> void parseHeader(Class<T> clazz, List<Object> headerList, List<String> dataIndexList) {
|
|
|
|
|
|
Field[] declaredFields = clazz.getDeclaredFields();
|
|
|
|
|
|
for (Field declaredField : declaredFields) {
|
|
|
|
|
|
if (!declaredField.isAnnotationPresent(ExcelHead.class)) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
ExcelHead annotation = declaredField.getAnnotation(ExcelHead.class);
|
|
|
|
|
|
headerList.add(SalaryI18nUtil.getI18nLabel(annotation.labelId(), annotation.title()));
|
|
|
|
|
|
dataIndexList.add(declaredField.getName());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-03-10 11:09:08 +08:00
|
|
|
|
}
|