mirror of https://github.com/alibaba/easyexcel
Jiaju Zhuang
5 years ago
33 changed files with 459 additions and 67 deletions
@ -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,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,42 @@
|
||||
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)); |
||||
|
||||
} |
||||
|
||||
@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,18 @@
|
||||
package com.alibaba.easyexcel.test.temp.read; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 临时测试 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
public class HeadReadData { |
||||
@ExcelProperty("头1") |
||||
private String h1; |
||||
@ExcelProperty({"头", "头2"}) |
||||
private String h2; |
||||
} |
@ -0,0 +1,25 @@
|
||||
package com.alibaba.easyexcel.test.temp.read; |
||||
|
||||
import java.io.File; |
||||
|
||||
import org.junit.Test; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.EasyExcel; |
||||
|
||||
/** |
||||
* 临时测试 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
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.xlsx"); |
||||
EasyExcel.read(file, HeadReadData.class, new HDListener()).sheet().doRead(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue