Before Width: | Height: | Size: 5.0 KiB |
Before Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 4.8 KiB |
Before Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 4.5 KiB |
Before Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.7 KiB |
Before Width: | Height: | Size: 811 B |
Before Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 2.7 KiB |
Before Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 5.4 KiB |
Before Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 4.5 KiB |
Before Width: | Height: | Size: 8.8 KiB |
Before Width: | Height: | Size: 5.0 KiB |
Before Width: | Height: | Size: 6.5 KiB |
Before Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 5.4 KiB |
Before Width: | Height: | Size: 3.4 KiB |
Before Width: | Height: | Size: 4.5 KiB |
Before Width: | Height: | Size: 6.6 KiB |
Before Width: | Height: | Size: 6.7 KiB |
Before Width: | Height: | Size: 4.7 KiB |
Before Width: | Height: | Size: 66 KiB |
@ -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,52 @@
|
||||
package com.alibaba.excel.converters.url; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.net.URL; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.util.IoUtils; |
||||
|
||||
/** |
||||
* Url and image converter |
||||
* |
||||
* @since 2.1.1 |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class UrlImageConverter implements Converter<URL> { |
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return URL.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.IMAGE; |
||||
} |
||||
|
||||
@Override |
||||
public URL convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
throw new UnsupportedOperationException("Cannot convert images to url."); |
||||
} |
||||
|
||||
@Override |
||||
public CellData convertToExcelData(URL value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) throws IOException { |
||||
InputStream inputStream = null; |
||||
try { |
||||
inputStream = value.openStream(); |
||||
byte[] bytes = IoUtils.toByteArray(inputStream); |
||||
return new CellData(bytes); |
||||
} finally { |
||||
if (inputStream != null) { |
||||
inputStream.close(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
@ -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,15 @@
|
||||
package com.alibaba.easyexcel.test.demo.read; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 假设这个是你的DAO存储。当然还要这个类让spring管理,当然你不用需要存储,也不需要这个类。 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public class DemoDAO { |
||||
|
||||
public void save(List<DemoData> list) { |
||||
// 如果是mybatis,尽量别直接调用多次insert,自己写一个mapper里面新增一个方法batchInsert,所有数据一次性插入
|
||||
} |
||||
} |
@ -0,0 +1,20 @@
|
||||
package com.alibaba.easyexcel.test.demo.web; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.springframework.stereotype.Repository; |
||||
|
||||
import com.alibaba.easyexcel.test.demo.read.DemoData; |
||||
|
||||
/** |
||||
* 假设这个是你的DAO存储。当然还要这个类让spring管理,当然你不用需要存储,也不需要这个类。 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Repository |
||||
public class UploadDAO { |
||||
|
||||
public void save(List<UploadData> list) { |
||||
// 如果是mybatis,尽量别直接调用多次insert,自己写一个mapper里面新增一个方法batchInsert,所有数据一次性插入
|
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -0,0 +1,43 @@
|
||||
package com.alibaba.easyexcel.test.temp.read; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* 模板的读取类 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class HDListener extends AnalysisEventListener<HeadReadData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(HDListener.class); |
||||
/** |
||||
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 |
||||
*/ |
||||
private static final int BATCH_COUNT = 5; |
||||
|
||||
@Override |
||||
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { |
||||
LOGGER.info("HEAD:{}", JSON.toJSONString(headMap)); |
||||
LOGGER.info("total:{}", context.readSheetHolder().getTotal()); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void invoke(HeadReadData data, AnalysisContext context) { |
||||
LOGGER.info("index:{}", context.readRowHolder().getRowIndex()); |
||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
LOGGER.info("所有数据解析完成!"); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,20 @@
|
||||
package com.alibaba.easyexcel.test.temp.read; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
import lombok.experimental.Accessors; |
||||
|
||||
/** |
||||
* 临时测试 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
@Accessors(chain = true) |
||||
public class HeadReadData { |
||||
@ExcelProperty("头1") |
||||
private String h1; |
||||
@ExcelProperty({"头", "头2"}) |
||||
private String h2; |
||||
} |
@ -0,0 +1,28 @@
|
||||
package com.alibaba.easyexcel.test.temp.read; |
||||
|
||||
import java.io.File; |
||||
|
||||
import org.junit.Ignore; |
||||
import org.junit.Test; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.EasyExcel; |
||||
|
||||
/** |
||||
* 临时测试 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Ignore |
||||
public class HeadReadTest { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(HeadReadTest.class); |
||||
|
||||
@Test |
||||
public void test() throws Exception { |
||||
File file = new File("D:\\test\\headt1.xls"); |
||||
EasyExcel.read(file, HeadReadData.class, new HDListener()).sheet(0).doRead(); |
||||
|
||||
} |
||||
|
||||
} |