This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package jnpf.easyexcel;
|
||||
|
||||
import com.alibaba.excel.write.handler.SheetWriteHandler;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
|
||||
import lombok.Data;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
|
||||
import java.util.List;
|
||||
@Data
|
||||
public class DynamicHeaderHandler implements SheetWriteHandler {
|
||||
private List<String> columnNames;
|
||||
|
||||
public DynamicHeaderHandler(List<String> columnNames) {
|
||||
this.columnNames = columnNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
|
||||
Sheet sheet = writeSheetHolder.getSheet();
|
||||
Row headerRow = sheet.createRow(0);
|
||||
// 假设根据某些条件动态添加列
|
||||
// 根据需要添加更多列
|
||||
for (int i = 0; i < columnNames.size(); i++) {
|
||||
Cell cell = headerRow.createCell(i);
|
||||
cell.setCellValue(columnNames.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package jnpf.easyexcel;
|
||||
|
||||
public interface EasyEnumNames {
|
||||
/**
|
||||
* 获取枚举名称
|
||||
*/
|
||||
String getName();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package jnpf.easyexcel;
|
||||
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||
|
||||
public class EasyEnumNamesConverter implements Converter<EasyEnumNames> {
|
||||
@Override
|
||||
public Class<?> supportJavaTypeKey() {
|
||||
return EasyEnumNames.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellDataTypeEnum supportExcelTypeKey() {
|
||||
return CellDataTypeEnum.STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WriteCellData<?> convertToExcelData(EasyEnumNames value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
|
||||
if (contentProperty == null) {
|
||||
return null;
|
||||
} else {
|
||||
return new WriteCellData<>(value.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package jnpf.easyexcel;
|
||||
|
||||
import com.alibaba.fastjson.serializer.JSONSerializer;
|
||||
import com.alibaba.fastjson.serializer.ObjectSerializer;
|
||||
import jnpf.util.ReflectionUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class FastJsonEnumConverter implements ObjectSerializer {
|
||||
|
||||
@Override
|
||||
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
|
||||
if (object == null) {
|
||||
serializer.write(null);
|
||||
return;
|
||||
}
|
||||
if (object instanceof Enum) {
|
||||
try {
|
||||
Object resultName = ReflectionUtil.invokeGetterMethod(object, "code");
|
||||
serializer.write(resultName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package jnpf.easyexcel;
|
||||
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||
import com.alibaba.excel.metadata.data.ReadCellData;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||
import com.alibaba.excel.util.DateUtils;
|
||||
import com.alibaba.excel.util.StringUtils;
|
||||
import org.apache.poi.ss.usermodel.DateUtil;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
public class StaffDateStringConverter implements Converter<Date> {
|
||||
@Override
|
||||
public Class<?> supportJavaTypeKey() {
|
||||
return Date.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellDataTypeEnum supportExcelTypeKey() {
|
||||
return CellDataTypeEnum.STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) throws ParseException {
|
||||
String format = switchDateFormat(cellData.getStringValue());
|
||||
if(StringUtils.isEmpty(format)){
|
||||
BigDecimal numberValue = cellData.getNumberValue();
|
||||
if (numberValue == null){
|
||||
return null;
|
||||
}
|
||||
double v = numberValue.doubleValue();
|
||||
return DateUtil.getJavaDate(v,
|
||||
globalConfiguration.getUse1904windowing(), null);
|
||||
}
|
||||
if (contentProperty == null || format.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
return DateUtils.parseDate(cellData.getStringValue(), format);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
|
||||
return new WriteCellData<>(DateUtils.format(value, null));
|
||||
} else {
|
||||
return new WriteCellData<>(DateUtils.format(value, contentProperty.getDateTimeFormatProperty().getFormat()));
|
||||
}
|
||||
}
|
||||
|
||||
private static final String param2 = "yyyy-MM-dd";
|
||||
private static final String param5 = "yyyy/MM/dd";
|
||||
private static final String param6 = "yyyyMMdd";
|
||||
private static final String param7 = "yyyy.MM.dd";
|
||||
private static final String MINUS = "-";
|
||||
private static final String MINUSD = ".";
|
||||
|
||||
private String switchDateFormat(String dateString) {
|
||||
if (dateString == null) {
|
||||
return "";
|
||||
}
|
||||
int length = dateString.length();
|
||||
switch (length) {
|
||||
case 10:
|
||||
if (dateString.contains(MINUS)) {
|
||||
return param2;
|
||||
}else if (dateString.contains(MINUSD)){
|
||||
return param7;
|
||||
}else {
|
||||
return param5;
|
||||
}
|
||||
case 8:
|
||||
return param6;
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user