forked from fanruan/easyexcel
Jiaju Zhuang
5 years ago
26 changed files with 450 additions and 113 deletions
@ -0,0 +1,42 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.IndexRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
|
||||
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class IndexRecordHandler extends AbstractXlsRecordHandler { |
||||
|
||||
private AnalysisContext context; |
||||
|
||||
public IndexRecordHandler(AnalysisContext context) { |
||||
this.context = context; |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(Record record) { |
||||
return record instanceof IndexRecord; |
||||
} |
||||
|
||||
@Override |
||||
public void init() {} |
||||
|
||||
@Override |
||||
public void processRecord(Record record) { |
||||
if (context.readSheetHolder() == null) { |
||||
return; |
||||
} |
||||
context.readSheetHolder().setApproximateTotalRowNumber(((IndexRecord)record).getLastRowAdd1()); |
||||
} |
||||
|
||||
@Override |
||||
public int getOrder() { |
||||
return 1; |
||||
} |
||||
} |
@ -0,0 +1,150 @@
|
||||
package com.alibaba.excel.util; |
||||
|
||||
import java.lang.ref.SoftReference; |
||||
import java.lang.reflect.Field; |
||||
import java.lang.reflect.Modifier; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.TreeMap; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnore; |
||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; |
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.exception.ExcelCommonException; |
||||
import com.alibaba.excel.metadata.BaseRowModel; |
||||
|
||||
/** |
||||
* Class utils |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public class ClassUtils { |
||||
private static final Map<Class, SoftReference<FieldCache>> FIELD_CACHE = |
||||
new ConcurrentHashMap<Class, SoftReference<FieldCache>>(); |
||||
|
||||
public static void declaredFields(Class clazz, List<Field> defaultFieldList, Map<Integer, Field> customFiledMap, |
||||
Map<String, Field> ignoreMap, Boolean convertAllFiled) { |
||||
FieldCache fieldCache = getFieldCache(clazz, convertAllFiled); |
||||
if (fieldCache != null) { |
||||
defaultFieldList.addAll(fieldCache.getDefaultFieldList()); |
||||
customFiledMap.putAll(fieldCache.getCustomFiledMap()); |
||||
ignoreMap.putAll(fieldCache.getIgnoreMap()); |
||||
} |
||||
} |
||||
|
||||
public static void declaredFields(Class clazz, List<Field> fieldList, Boolean convertAllFiled) { |
||||
FieldCache fieldCache = getFieldCache(clazz, convertAllFiled); |
||||
if (fieldCache != null) { |
||||
fieldList.addAll(fieldCache.getAllFieldList()); |
||||
} |
||||
} |
||||
|
||||
private static FieldCache getFieldCache(Class clazz, Boolean convertAllFiled) { |
||||
if (clazz == null) { |
||||
return null; |
||||
} |
||||
SoftReference<FieldCache> fieldCacheSoftReference = FIELD_CACHE.get(clazz); |
||||
if (fieldCacheSoftReference != null && fieldCacheSoftReference.get() != null) { |
||||
return fieldCacheSoftReference.get(); |
||||
} |
||||
synchronized (clazz) { |
||||
fieldCacheSoftReference = FIELD_CACHE.get(clazz); |
||||
if (fieldCacheSoftReference != null && fieldCacheSoftReference.get() != null) { |
||||
return fieldCacheSoftReference.get(); |
||||
} |
||||
declaredFields(clazz, convertAllFiled); |
||||
} |
||||
return FIELD_CACHE.get(clazz).get(); |
||||
} |
||||
|
||||
private static void declaredFields(Class clazz, Boolean convertAllFiled) { |
||||
List<Field> tempFieldList = new ArrayList<Field>(); |
||||
Class tempClass = clazz; |
||||
// When the parent class is null, it indicates that the parent class (Object class) has reached the top
|
||||
// level.
|
||||
while (tempClass != null && tempClass != BaseRowModel.class) { |
||||
Collections.addAll(tempFieldList, tempClass.getDeclaredFields()); |
||||
// Get the parent class and give it to yourself
|
||||
tempClass = tempClass.getSuperclass(); |
||||
} |
||||
// Screening of field
|
||||
List<Field> defaultFieldList = new ArrayList<Field>(); |
||||
Map<Integer, Field> customFiledMap = new TreeMap<Integer, Field>(); |
||||
List<Field> allFieldList = new ArrayList<Field>(); |
||||
Map<String, Field> ignoreMap = new HashMap<String, Field>(16); |
||||
|
||||
ExcelIgnoreUnannotated excelIgnoreUnannotated = |
||||
(ExcelIgnoreUnannotated)clazz.getAnnotation(ExcelIgnoreUnannotated.class); |
||||
for (Field field : tempFieldList) { |
||||
ExcelIgnore excelIgnore = field.getAnnotation(ExcelIgnore.class); |
||||
if (excelIgnore != null) { |
||||
ignoreMap.put(field.getName(), field); |
||||
continue; |
||||
} |
||||
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); |
||||
boolean noExcelProperty = excelProperty == null |
||||
&& ((convertAllFiled != null && !convertAllFiled) || excelIgnoreUnannotated != null); |
||||
if (noExcelProperty) { |
||||
ignoreMap.put(field.getName(), field); |
||||
continue; |
||||
} |
||||
boolean isStaticFinalOrTransient = |
||||
(Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) |
||||
|| Modifier.isTransient(field.getModifiers()); |
||||
if (excelProperty == null && isStaticFinalOrTransient) { |
||||
ignoreMap.put(field.getName(), field); |
||||
continue; |
||||
} |
||||
if (excelProperty == null || excelProperty.index() < 0) { |
||||
defaultFieldList.add(field); |
||||
allFieldList.add(field); |
||||
continue; |
||||
} |
||||
if (customFiledMap.containsKey(excelProperty.index())) { |
||||
throw new ExcelCommonException("The index of '" + customFiledMap.get(excelProperty.index()).getName() |
||||
+ "' and '" + field.getName() + "' must be inconsistent"); |
||||
} |
||||
customFiledMap.put(excelProperty.index(), field); |
||||
allFieldList.add(field); |
||||
} |
||||
|
||||
FIELD_CACHE.put(clazz, |
||||
new SoftReference<FieldCache>(new FieldCache(defaultFieldList, customFiledMap, allFieldList, ignoreMap))); |
||||
} |
||||
|
||||
private static class FieldCache { |
||||
private List<Field> defaultFieldList; |
||||
private Map<Integer, Field> customFiledMap; |
||||
private List<Field> allFieldList; |
||||
private Map<String, Field> ignoreMap; |
||||
|
||||
public FieldCache(List<Field> defaultFieldList, Map<Integer, Field> customFiledMap, List<Field> allFieldList, |
||||
Map<String, Field> ignoreMap) { |
||||
this.defaultFieldList = defaultFieldList; |
||||
this.customFiledMap = customFiledMap; |
||||
this.allFieldList = allFieldList; |
||||
this.ignoreMap = ignoreMap; |
||||
} |
||||
|
||||
public List<Field> getDefaultFieldList() { |
||||
return defaultFieldList; |
||||
} |
||||
|
||||
public Map<Integer, Field> getCustomFiledMap() { |
||||
return customFiledMap; |
||||
} |
||||
|
||||
public List<Field> getAllFieldList() { |
||||
return allFieldList; |
||||
} |
||||
|
||||
public Map<String, Field> getIgnoreMap() { |
||||
return ignoreMap; |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,110 @@
|
||||
package com.alibaba.easyexcel.test.temp; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.Ignore; |
||||
import org.junit.Test; |
||||
|
||||
import com.alibaba.easyexcel.test.demo.fill.FillData; |
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.alibaba.excel.ExcelWriter; |
||||
import com.alibaba.excel.write.metadata.WriteSheet; |
||||
import com.alibaba.excel.write.metadata.fill.FillConfig; |
||||
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; |
||||
|
||||
/** |
||||
* 写的填充写法 |
||||
* |
||||
* @since 2.1.1 |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Ignore |
||||
public class FillTempTest { |
||||
|
||||
/** |
||||
* 复杂的填充 |
||||
* |
||||
* @since 2.1.1 |
||||
*/ |
||||
@Test |
||||
public void complexFill() { |
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// {} 代表普通变量 {.} 代表是list的变量
|
||||
String templateFileName = "D:\\test\\complex.xlsx"; |
||||
|
||||
String fileName = TestFileUtil.getPath() + "complexFill" + System.currentTimeMillis() + ".xlsx"; |
||||
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
||||
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
||||
// 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
|
||||
// forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
|
||||
// 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
|
||||
// 如果数据量大 list不是最后一行 参照下一个
|
||||
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); |
||||
excelWriter.fill(data(), fillConfig, writeSheet); |
||||
excelWriter.fill(data(), fillConfig, writeSheet); |
||||
Map<String, Object> map = new HashMap<String, Object>(); |
||||
map.put("date", "2019年10月9日13:28:28"); |
||||
map.put("total", 1000); |
||||
excelWriter.fill(map, writeSheet); |
||||
excelWriter.finish(); |
||||
} |
||||
|
||||
/** |
||||
* 数据量大的复杂填充 |
||||
* <p> |
||||
* 这里的解决方案是 确保模板list为最后一行,然后再拼接table.还有03版没救,只能刚正面加内存。 |
||||
* |
||||
* @since 2.1.1 |
||||
*/ |
||||
@Test |
||||
public void complexFillWithTable() { |
||||
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
||||
// {} 代表普通变量 {.} 代表是list的变量
|
||||
// 这里模板 删除了list以后的数据,也就是统计的这一行
|
||||
String templateFileName = "D:\\test\\complex.xlsx"; |
||||
|
||||
String fileName = TestFileUtil.getPath() + "complexFillWithTable" + System.currentTimeMillis() + ".xlsx"; |
||||
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); |
||||
WriteSheet writeSheet = EasyExcel.writerSheet().build(); |
||||
// 直接写入数据
|
||||
excelWriter.fill(data(), writeSheet); |
||||
excelWriter.fill(data(), writeSheet); |
||||
|
||||
// 写入list之前的数据
|
||||
Map<String, Object> map = new HashMap<String, Object>(); |
||||
map.put("date", "2019年10月9日13:28:28"); |
||||
excelWriter.fill(map, writeSheet); |
||||
|
||||
// list 后面还有个统计 想办法手动写入
|
||||
// 这里偷懒直接用list 也可以用对象
|
||||
List<List<String>> totalListList = new ArrayList<List<String>>(); |
||||
List<String> totalList = new ArrayList<String>(); |
||||
totalListList.add(totalList); |
||||
totalList.add(null); |
||||
totalList.add(null); |
||||
totalList.add(null); |
||||
// 第四列
|
||||
totalList.add("统计:1000"); |
||||
// 这里是write 别和fill 搞错了
|
||||
excelWriter.write(totalListList, writeSheet); |
||||
excelWriter.finish(); |
||||
// 总体上写法比较复杂 但是也没有想到好的版本 异步的去写入excel 不支持行的删除和移动,也不支持备注这种的写入,所以也排除了可以
|
||||
// 新建一个 然后一点点复制过来的方案,最后导致list需要新增行的时候,后面的列的数据没法后移,后续会继续想想解决方案
|
||||
} |
||||
|
||||
private List<FillData> data() { |
||||
List<FillData> list = new ArrayList<FillData>(); |
||||
for (int i = 0; i < 10; i++) { |
||||
FillData fillData = new FillData(); |
||||
list.add(fillData); |
||||
fillData.setName("张三"); |
||||
fillData.setNumber(5.2); |
||||
} |
||||
return list; |
||||
} |
||||
} |
Loading…
Reference in new issue