forked from fanruan/easyexcel
Jiaju Zhuang
4 years ago
62 changed files with 746 additions and 540 deletions
@ -0,0 +1,13 @@
|
||||
package com.alibaba.excel.constant; |
||||
|
||||
/** |
||||
* Order constant. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class OrderConstant { |
||||
/** |
||||
* Sorting of styles written to cells. |
||||
*/ |
||||
public static final int FILL_DATA_FORMAT = 10000; |
||||
} |
@ -0,0 +1,34 @@
|
||||
package com.alibaba.excel.converters.date; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.util.WorkBookUtil; |
||||
import com.alibaba.excel.write.metadata.holder.WriteHolder; |
||||
|
||||
/** |
||||
* Date and date converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class DateDateConverter implements Converter<Date> { |
||||
@Override |
||||
public Class<Date> supportJavaTypeKey() { |
||||
return Date.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, |
||||
WriteHolder currentWriteHolder) throws Exception { |
||||
CellData<?> cellData = new CellData<>(value); |
||||
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null |
||||
|| contentProperty.getDateTimeFormatProperty().getFormat() == null) { |
||||
return cellData; |
||||
} |
||||
WorkBookUtil.fillDataFormat(cellData, currentWriteHolder, |
||||
contentProperty.getDateTimeFormatProperty().getFormat()); |
||||
return cellData; |
||||
} |
||||
} |
@ -0,0 +1,63 @@
|
||||
package com.alibaba.excel.util; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* Map utils |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public class MapUtils { |
||||
private MapUtils() {} |
||||
/** |
||||
* Creates a <i>mutable</i>, empty {@code HashMap} instance. |
||||
* |
||||
* <p><b>Note:</b> if mutability is not required, use {@link ImmutableMap#of()} instead. |
||||
* |
||||
* <p><b>Note:</b> if {@code K} is an {@code enum} type, use {@link #newEnumMap} instead. |
||||
* |
||||
* <p><b>Note for Java 7 and later:</b> this method is now unnecessary and should be treated as |
||||
* deprecated. Instead, use the {@code HashMap} constructor directly, taking advantage of the new |
||||
* <a href="http://goo.gl/iz2Wi">"diamond" syntax</a>. |
||||
* |
||||
* @return a new, empty {@code HashMap} |
||||
*/ |
||||
public static <K, V> HashMap<K, V> newHashMap() { |
||||
return new HashMap<>(); |
||||
} |
||||
|
||||
/** |
||||
* Creates a {@code HashMap} instance, with a high enough "initial capacity" that it <i>should</i> |
||||
* hold {@code expectedSize} elements without growth. This behavior cannot be broadly guaranteed, |
||||
* but it is observed to be true for OpenJDK 1.7. It also can't be guaranteed that the method |
||||
* isn't inadvertently <i>oversizing</i> the returned map. |
||||
* |
||||
* @param expectedSize the number of entries you expect to add to the returned map |
||||
* @return a new, empty {@code HashMap} with enough capacity to hold {@code expectedSize} entries |
||||
* without resizing |
||||
* @throws IllegalArgumentException if {@code expectedSize} is negative |
||||
*/ |
||||
public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) { |
||||
return new HashMap<>(capacity(expectedSize)); |
||||
} |
||||
|
||||
/** |
||||
* Returns a capacity that is sufficient to keep the map from being resized as long as it grows no |
||||
* larger than expectedSize and the load factor is ≥ its default (0.75). |
||||
*/ |
||||
static int capacity(int expectedSize) { |
||||
if (expectedSize < 3) { |
||||
return expectedSize + 1; |
||||
} |
||||
if (expectedSize < IntUtils.MAX_POWER_OF_TWO) { |
||||
// This is the calculation used in JDK8 to resize when a putAll
|
||||
// happens; it seems to be the most conservative calculation we
|
||||
// can make. 0.75 is the default load factor.
|
||||
return (int) ((float) expectedSize / 0.75F + 1.0F); |
||||
} |
||||
return Integer.MAX_VALUE; |
||||
} |
||||
} |
@ -0,0 +1,75 @@
|
||||
package com.alibaba.excel.write.handler.impl; |
||||
|
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import com.alibaba.excel.constant.OrderConstant; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.event.Order; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.Head; |
||||
import com.alibaba.excel.util.DateUtils; |
||||
import com.alibaba.excel.write.handler.CellWriteHandler; |
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
||||
import com.alibaba.excel.write.metadata.holder.WriteTableHolder; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.collections4.CollectionUtils; |
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.CellStyle; |
||||
import org.apache.poi.ss.usermodel.DataFormat; |
||||
import org.apache.poi.ss.usermodel.Workbook; |
||||
|
||||
/** |
||||
* fill cell style. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Slf4j |
||||
public class FillDataFormatCellWriteHandler implements CellWriteHandler, Order { |
||||
|
||||
private final Set<CellStyle> cellStyleSet = new HashSet<>(); |
||||
|
||||
private CellStyle defaultDateCellStyle; |
||||
|
||||
@Override |
||||
public int order() { |
||||
return OrderConstant.FILL_DATA_FORMAT; |
||||
} |
||||
|
||||
@Override |
||||
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, |
||||
List<CellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { |
||||
if (CollectionUtils.isEmpty(cellDataList) || cellDataList.size() > 1) { |
||||
return; |
||||
} |
||||
CellData<?> cellData = cellDataList.get(0); |
||||
CellStyle cellStyle = cell.getCellStyle(); |
||||
if (cellStyle == null) { |
||||
if (cellData.getType() == CellDataTypeEnum.DATE) { |
||||
cell.setCellStyle(getDefaultDateCellStyle(writeSheetHolder)); |
||||
} |
||||
return; |
||||
} |
||||
if (cellStyleSet.contains(cellStyle)) { |
||||
return; |
||||
} |
||||
if (cellData.getDataFormat() != null && cellData.getDataFormat() >= 0) { |
||||
cellStyle.setDataFormat(cellData.getDataFormat()); |
||||
} |
||||
cellStyleSet.add(cellStyle); |
||||
} |
||||
|
||||
private CellStyle getDefaultDateCellStyle(WriteSheetHolder writeSheetHolder) { |
||||
if (defaultDateCellStyle != null) { |
||||
return defaultDateCellStyle; |
||||
} |
||||
Workbook workbook = writeSheetHolder.getParentWriteWorkbookHolder().getWorkbook(); |
||||
defaultDateCellStyle = workbook.createCellStyle(); |
||||
DataFormat dataFormat = workbook.createDataFormat(); |
||||
defaultDateCellStyle.setDataFormat(dataFormat.getFormat(DateUtils.defaultDateFormat)); |
||||
return defaultDateCellStyle; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue