mirror of https://github.com/alibaba/easyexcel
zhuangjiaju
5 years ago
51 changed files with 817 additions and 1256 deletions
@ -0,0 +1,23 @@
|
||||
package com.alibaba.excel.exception; |
||||
|
||||
/** |
||||
* Throw the exception when you need to stop |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ExcelAnalysisStopException extends ExcelAnalysisException { |
||||
|
||||
public ExcelAnalysisStopException() {} |
||||
|
||||
public ExcelAnalysisStopException(String message) { |
||||
super(message); |
||||
} |
||||
|
||||
public ExcelAnalysisStopException(String message, Throwable cause) { |
||||
super(message, cause); |
||||
} |
||||
|
||||
public ExcelAnalysisStopException(Throwable cause) { |
||||
super(cause); |
||||
} |
||||
} |
@ -1,44 +0,0 @@
|
||||
package com.alibaba.excel.parameter; |
||||
|
||||
import java.io.OutputStream; |
||||
|
||||
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
|
||||
/** |
||||
* {@link com.alibaba.excel.ExcelWriter} |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
@Deprecated |
||||
public class ExcelWriteParam { |
||||
|
||||
/** |
||||
*/ |
||||
private OutputStream outputStream; |
||||
|
||||
/** |
||||
*/ |
||||
private ExcelTypeEnum type; |
||||
|
||||
public ExcelWriteParam(OutputStream outputStream, ExcelTypeEnum type) { |
||||
this.outputStream = outputStream; |
||||
this.type = type; |
||||
|
||||
} |
||||
|
||||
public OutputStream getOutputStream() { |
||||
return outputStream; |
||||
} |
||||
|
||||
public void setOutputStream(OutputStream outputStream) { |
||||
this.outputStream = outputStream; |
||||
} |
||||
|
||||
public ExcelTypeEnum getType() { |
||||
return type; |
||||
} |
||||
|
||||
public void setType(ExcelTypeEnum type) { |
||||
this.type = type; |
||||
} |
||||
} |
@ -1,189 +0,0 @@
|
||||
package com.alibaba.easyexcel.test; |
||||
|
||||
import com.alibaba.easyexcel.test.listen.ExcelListener; |
||||
import com.alibaba.easyexcel.test.model.ReadModel; |
||||
import com.alibaba.easyexcel.test.model.ReadModel2; |
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
import com.alibaba.excel.ExcelReader; |
||||
import com.alibaba.excel.metadata.Sheet; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.util.List; |
||||
|
||||
public class ReadTest { |
||||
|
||||
|
||||
/** |
||||
* 07版本excel读数据量少于1千行数据,内部采用回调方法. |
||||
* |
||||
* @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 |
||||
*/ |
||||
@Test |
||||
public void simpleReadListStringV2007() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx"); |
||||
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, 0)); |
||||
inputStream.close(); |
||||
print(data); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 07版本excel读数据量少于1千行数据自动转成javamodel,内部采用回调方法. |
||||
* |
||||
* @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 |
||||
*/ |
||||
@Test |
||||
public void simpleReadJavaModelV2007() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx"); |
||||
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class)); |
||||
inputStream.close(); |
||||
print(data); |
||||
} |
||||
|
||||
/** |
||||
* 07版本excel读数据量大于1千行,内部采用回调方法. |
||||
* |
||||
* @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 |
||||
*/ |
||||
@Test |
||||
public void saxReadListStringV2007() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx"); |
||||
ExcelListener excelListener = new ExcelListener(); |
||||
EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1), excelListener); |
||||
inputStream.close(); |
||||
|
||||
} |
||||
/** |
||||
* 07版本excel读数据量大于1千行,内部采用回调方法. |
||||
* |
||||
* @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 |
||||
*/ |
||||
@Test |
||||
public void saxReadJavaModelV2007() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx"); |
||||
ExcelListener excelListener = new ExcelListener(); |
||||
EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener); |
||||
inputStream.close(); |
||||
} |
||||
|
||||
/** |
||||
* 07版本excel读取sheet |
||||
* |
||||
* @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 |
||||
*/ |
||||
@Test |
||||
public void saxReadSheetsV2007() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx"); |
||||
ExcelListener excelListener = new ExcelListener(); |
||||
ExcelReader excelReader = EasyExcelFactory.getReader(inputStream,excelListener); |
||||
List<Sheet> sheets = excelReader.getSheets(); |
||||
System.out.println("llll****"+sheets); |
||||
System.out.println(); |
||||
for (Sheet sheet:sheets) { |
||||
if(sheet.getSheetNo() ==1) { |
||||
excelReader.read(sheet); |
||||
}else if(sheet.getSheetNo() ==2){ |
||||
sheet.setHeadLineMun(1); |
||||
sheet.setClazz(ReadModel.class); |
||||
excelReader.read(sheet); |
||||
}else if(sheet.getSheetNo() ==3){ |
||||
sheet.setHeadLineMun(1); |
||||
sheet.setClazz(ReadModel2.class); |
||||
excelReader.read(sheet); |
||||
} |
||||
} |
||||
inputStream.close(); |
||||
} |
||||
|
||||
|
||||
|
||||
/** |
||||
* 03版本excel读数据量少于1千行数据,内部采用回调方法. |
||||
* |
||||
* @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 |
||||
*/ |
||||
@Test |
||||
public void simpleReadListStringV2003() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls"); |
||||
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, 0)); |
||||
inputStream.close(); |
||||
print(data); |
||||
} |
||||
|
||||
/** |
||||
* 03版本excel读数据量少于1千行数据转成javamodel,内部采用回调方法. |
||||
* |
||||
* @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 |
||||
*/ |
||||
@Test |
||||
public void simpleReadJavaModelV2003() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls"); |
||||
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class)); |
||||
inputStream.close(); |
||||
print(data); |
||||
} |
||||
|
||||
/** |
||||
* 03版本excel读数据量大于1千行数据,内部采用回调方法. |
||||
* |
||||
* @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 |
||||
*/ |
||||
@Test |
||||
public void saxReadListStringV2003() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls"); |
||||
ExcelListener excelListener = new ExcelListener(); |
||||
EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1), excelListener); |
||||
inputStream.close(); |
||||
} |
||||
|
||||
/** |
||||
* 03版本excel读数据量大于1千行数据转成javamodel,内部采用回调方法. |
||||
* |
||||
* @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 |
||||
*/ |
||||
@Test |
||||
public void saxReadJavaModelV2003() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls"); |
||||
ExcelListener excelListener = new ExcelListener(); |
||||
EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener); |
||||
inputStream.close(); |
||||
} |
||||
|
||||
/** |
||||
* 00版本excel读取sheet |
||||
* |
||||
* @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 |
||||
*/ |
||||
@Test |
||||
public void saxReadSheetsV2003() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls"); |
||||
ExcelListener excelListener = new ExcelListener(); |
||||
ExcelReader excelReader = EasyExcelFactory.getReader(inputStream,excelListener); |
||||
List<Sheet> sheets = excelReader.getSheets(); |
||||
System.out.println(); |
||||
for (Sheet sheet:sheets) { |
||||
if(sheet.getSheetNo() == 1) { |
||||
excelReader.read(sheet); |
||||
}else { |
||||
sheet.setHeadLineMun(2); |
||||
sheet.setClazz(ReadModel.class); |
||||
excelReader.read(sheet); |
||||
} |
||||
} |
||||
inputStream.close(); |
||||
} |
||||
|
||||
|
||||
public void print(List<Object> datas){ |
||||
int i=0; |
||||
for (Object ob:datas) { |
||||
System.out.println(i++); |
||||
System.out.println(ob); |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,194 +0,0 @@
|
||||
package com.alibaba.easyexcel.test; |
||||
|
||||
import com.alibaba.easyexcel.test.listen.AfterExcelWriteHandlerImpl; |
||||
import com.alibaba.easyexcel.test.model.WriteModel; |
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
import com.alibaba.excel.ExcelWriter; |
||||
import com.alibaba.excel.metadata.Sheet; |
||||
import com.alibaba.excel.metadata.Table; |
||||
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
import org.junit.Test; |
||||
|
||||
import java.io.*; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import static com.alibaba.easyexcel.test.util.DataUtil.*; |
||||
|
||||
public class WriteTest { |
||||
|
||||
@Test |
||||
public void writeV2007() throws IOException { |
||||
OutputStream out = new FileOutputStream("/Users/jipengfei/2007.xlsx"); |
||||
ExcelWriter writer = EasyExcelFactory.getWriter(out); |
||||
//写第一个sheet, sheet1 数据全是List<String> 无模型映射关系
|
||||
Sheet sheet1 = new Sheet(1, 3); |
||||
sheet1.setSheetName("第一个sheet"); |
||||
|
||||
//设置列宽 设置每列的宽度
|
||||
Map columnWidth = new HashMap(); |
||||
columnWidth.put(0,10000);columnWidth.put(1,40000);columnWidth.put(2,10000);columnWidth.put(3,10000); |
||||
sheet1.setColumnWidthMap(columnWidth); |
||||
sheet1.setHead(createTestListStringHead()); |
||||
//or 设置自适应宽度
|
||||
//sheet1.setAutoWidth(Boolean.TRUE);
|
||||
writer.write1(createTestListObject(), sheet1); |
||||
|
||||
//写第二个sheet sheet2 模型上打有表头的注解,合并单元格
|
||||
Sheet sheet2 = new Sheet(2, 3, WriteModel.class, "第二个sheet", null); |
||||
sheet2.setTableStyle(createTableStyle()); |
||||
//writer.write1(null, sheet2);
|
||||
writer.write(createTestListJavaMode(), sheet2); |
||||
//需要合并单元格
|
||||
writer.merge(5,20,1,1); |
||||
|
||||
//写第三个sheet包含多个table情况
|
||||
Sheet sheet3 = new Sheet(3, 0); |
||||
sheet3.setSheetName("第三个sheet"); |
||||
Table table1 = new Table(1); |
||||
table1.setHead(createTestListStringHead()); |
||||
writer.write1(createTestListObject(), sheet3, table1); |
||||
|
||||
//写sheet2 模型上打有表头的注解
|
||||
Table table2 = new Table(2); |
||||
table2.setTableStyle(createTableStyle()); |
||||
table2.setClazz(WriteModel.class); |
||||
writer.write(createTestListJavaMode(), sheet3, table2); |
||||
|
||||
writer.finish(); |
||||
out.close(); |
||||
|
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void writeV2007WithTemplate() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("temp.xlsx"); |
||||
OutputStream out = new FileOutputStream("/Users/jipengfei/2007.xlsx"); |
||||
ExcelWriter writer = EasyExcelFactory.getWriterWithTemp(inputStream,out,ExcelTypeEnum.XLSX,true); |
||||
//写第一个sheet, sheet1 数据全是List<String> 无模型映射关系
|
||||
Sheet sheet1 = new Sheet(1, 3); |
||||
sheet1.setSheetName("第一个sheet"); |
||||
sheet1.setStartRow(20); |
||||
|
||||
//设置列宽 设置每列的宽度
|
||||
Map columnWidth = new HashMap(); |
||||
columnWidth.put(0,10000);columnWidth.put(1,40000);columnWidth.put(2,10000);columnWidth.put(3,10000); |
||||
sheet1.setColumnWidthMap(columnWidth); |
||||
sheet1.setHead(createTestListStringHead()); |
||||
//or 设置自适应宽度
|
||||
//sheet1.setAutoWidth(Boolean.TRUE);
|
||||
writer.write1(createTestListObject(), sheet1); |
||||
|
||||
//写第二个sheet sheet2 模型上打有表头的注解,合并单元格
|
||||
Sheet sheet2 = new Sheet(2, 3, WriteModel.class, "第二个sheet", null); |
||||
sheet2.setTableStyle(createTableStyle()); |
||||
sheet2.setStartRow(20); |
||||
writer.write(createTestListJavaMode(), sheet2); |
||||
|
||||
//写第三个sheet包含多个table情况
|
||||
Sheet sheet3 = new Sheet(3, 0); |
||||
sheet3.setSheetName("第三个sheet"); |
||||
sheet3.setStartRow(30); |
||||
Table table1 = new Table(1); |
||||
table1.setHead(createTestListStringHead()); |
||||
writer.write1(createTestListObject(), sheet3, table1); |
||||
|
||||
//写sheet2 模型上打有表头的注解
|
||||
Table table2 = new Table(2); |
||||
table2.setTableStyle(createTableStyle()); |
||||
table2.setClazz(WriteModel.class); |
||||
writer.write(createTestListJavaMode(), sheet3, table2); |
||||
|
||||
writer.finish(); |
||||
out.close(); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void writeV2007WithTemplateAndHandler() throws IOException { |
||||
InputStream inputStream = TestFileUtil.getResourcesFileInputStream("temp.xlsx"); |
||||
OutputStream out = new FileOutputStream("/Users/jipengfei/2007.xlsx"); |
||||
ExcelWriter writer = EasyExcelFactory.getWriterWithTempAndHandler(inputStream,out,ExcelTypeEnum.XLSX,true, |
||||
new AfterExcelWriteHandlerImpl()); |
||||
//写第一个sheet, sheet1 数据全是List<String> 无模型映射关系
|
||||
Sheet sheet1 = new Sheet(1, 3); |
||||
sheet1.setSheetName("第一个sheet"); |
||||
sheet1.setStartRow(20); |
||||
|
||||
//设置列宽 设置每列的宽度
|
||||
Map columnWidth = new HashMap(); |
||||
columnWidth.put(0,10000);columnWidth.put(1,40000);columnWidth.put(2,10000);columnWidth.put(3,10000); |
||||
sheet1.setColumnWidthMap(columnWidth); |
||||
sheet1.setHead(createTestListStringHead()); |
||||
//or 设置自适应宽度
|
||||
//sheet1.setAutoWidth(Boolean.TRUE);
|
||||
writer.write1(createTestListObject(), sheet1); |
||||
|
||||
//写第二个sheet sheet2 模型上打有表头的注解,合并单元格
|
||||
Sheet sheet2 = new Sheet(2, 3, WriteModel.class, "第二个sheet", null); |
||||
sheet2.setTableStyle(createTableStyle()); |
||||
sheet2.setStartRow(20); |
||||
writer.write(createTestListJavaMode(), sheet2); |
||||
|
||||
//写第三个sheet包含多个table情况
|
||||
Sheet sheet3 = new Sheet(3, 0); |
||||
sheet3.setSheetName("第三个sheet"); |
||||
sheet3.setStartRow(30); |
||||
Table table1 = new Table(1); |
||||
table1.setHead(createTestListStringHead()); |
||||
writer.write1(createTestListObject(), sheet3, table1); |
||||
|
||||
//写sheet2 模型上打有表头的注解
|
||||
Table table2 = new Table(2); |
||||
table2.setTableStyle(createTableStyle()); |
||||
table2.setClazz(WriteModel.class); |
||||
writer.write(createTestListJavaMode(), sheet3, table2); |
||||
|
||||
writer.finish(); |
||||
out.close(); |
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
@Test |
||||
public void writeV2003() throws IOException { |
||||
OutputStream out = new FileOutputStream("/Users/jipengfei/2003.xls"); |
||||
ExcelWriter writer = EasyExcelFactory.getWriter(out, ExcelTypeEnum.XLS,true); |
||||
//写第一个sheet, sheet1 数据全是List<String> 无模型映射关系
|
||||
Sheet sheet1 = new Sheet(1, 3); |
||||
sheet1.setSheetName("第一个sheet"); |
||||
|
||||
//设置列宽 设置每列的宽度
|
||||
Map columnWidth = new HashMap(); |
||||
columnWidth.put(0,10000);columnWidth.put(1,40000);columnWidth.put(2,10000);columnWidth.put(3,10000); |
||||
sheet1.setColumnWidthMap(columnWidth); |
||||
sheet1.setHead(createTestListStringHead()); |
||||
//or 设置自适应宽度
|
||||
//sheet1.setAutoWidth(Boolean.TRUE);
|
||||
writer.write1(createTestListObject(), sheet1); |
||||
|
||||
//写第二个sheet sheet2 模型上打有表头的注解,合并单元格
|
||||
Sheet sheet2 = new Sheet(2, 3, WriteModel.class, "第二个sheet", null); |
||||
sheet2.setTableStyle(createTableStyle()); |
||||
writer.write(createTestListJavaMode(), sheet2); |
||||
|
||||
//写第三个sheet包含多个table情况
|
||||
Sheet sheet3 = new Sheet(3, 0); |
||||
sheet3.setSheetName("第三个sheet"); |
||||
Table table1 = new Table(1); |
||||
table1.setHead(createTestListStringHead()); |
||||
writer.write1(createTestListObject(), sheet3, table1); |
||||
|
||||
//写sheet2 模型上打有表头的注解
|
||||
Table table2 = new Table(2); |
||||
table2.setTableStyle(createTableStyle()); |
||||
table2.setClazz(WriteModel.class); |
||||
writer.write(createTestListJavaMode(), sheet3, table2); |
||||
|
||||
writer.finish(); |
||||
out.close(); |
||||
} |
||||
} |
@ -0,0 +1,143 @@
|
||||
package com.alibaba.easyexcel.test.core.compatibility; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileOutputStream; |
||||
import java.io.InputStream; |
||||
import java.io.OutputStream; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.BeforeClass; |
||||
import org.junit.Test; |
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
import com.alibaba.excel.ExcelReader; |
||||
import com.alibaba.excel.ExcelWriter; |
||||
import com.alibaba.excel.metadata.Sheet; |
||||
|
||||
/** |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class CompatibilityParameterDataTest { |
||||
|
||||
private static File file; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
file = TestFileUtil.createNewFile("compatibilityParameter.xlsx"); |
||||
} |
||||
|
||||
@Test |
||||
public void T01ReadAndWrite() throws Exception { |
||||
readAndWrite1(file); |
||||
readAndWrite2(file); |
||||
readAndWrite3(file); |
||||
readAndWrite4(file); |
||||
readAndWrite5(file); |
||||
readAndWrite6(file); |
||||
} |
||||
|
||||
private void readAndWrite1(File file) throws Exception { |
||||
OutputStream out = new FileOutputStream(file); |
||||
ExcelWriter writer = EasyExcelFactory.getWriter(out); |
||||
Sheet sheet1 = new Sheet(1, 0); |
||||
sheet1.setSheetName("第一个sheet"); |
||||
writer.write0(data(), sheet1); |
||||
writer.finish(); |
||||
out.close(); |
||||
|
||||
InputStream inputStream = new FileInputStream(file); |
||||
EasyExcelFactory.readBySax(inputStream, new Sheet(1, 0), new CompatibilityDataListener()); |
||||
inputStream.close(); |
||||
} |
||||
|
||||
private void readAndWrite2(File file) throws Exception { |
||||
OutputStream out = new FileOutputStream(file); |
||||
ExcelWriter writer = EasyExcelFactory.getWriter(out, null, false); |
||||
Sheet sheet1 = new Sheet(1, 0); |
||||
sheet1.setSheetName("第一个sheet"); |
||||
writer.write0(data(), sheet1); |
||||
writer.finish(); |
||||
out.close(); |
||||
|
||||
InputStream inputStream = new FileInputStream(file); |
||||
EasyExcelFactory.readBySax(inputStream, new Sheet(1, 0), new CompatibilityDataListener()); |
||||
inputStream.close(); |
||||
} |
||||
|
||||
private void readAndWrite3(File file) throws Exception { |
||||
OutputStream out = new FileOutputStream(file); |
||||
ExcelWriter writer = new ExcelWriter(out, null); |
||||
Sheet sheet1 = new Sheet(1, 0); |
||||
sheet1.setSheetName("第一个sheet"); |
||||
writer.write0(data(), sheet1); |
||||
writer.finish(); |
||||
out.close(); |
||||
|
||||
InputStream inputStream = new FileInputStream(file); |
||||
ExcelReader excelReader = new ExcelReader(inputStream, null, null, new CompatibilityDataListener()); |
||||
excelReader.read(new Sheet(1, 0)); |
||||
inputStream.close(); |
||||
|
||||
} |
||||
|
||||
private void readAndWrite4(File file) throws Exception { |
||||
OutputStream out = new FileOutputStream(file); |
||||
ExcelWriter writer = new ExcelWriter(null, out, null, null); |
||||
Sheet sheet1 = new Sheet(1, 0); |
||||
sheet1.setSheetName("第一个sheet"); |
||||
writer.write0(data(), sheet1, null); |
||||
writer.finish(); |
||||
out.close(); |
||||
|
||||
InputStream inputStream = new FileInputStream(file); |
||||
ExcelReader excelReader = new ExcelReader(inputStream, null, new CompatibilityDataListener()); |
||||
excelReader.read(new Sheet(1, 0)); |
||||
inputStream.close(); |
||||
} |
||||
|
||||
private void readAndWrite5(File file) throws Exception { |
||||
OutputStream out = new FileOutputStream(file); |
||||
ExcelWriter writer = EasyExcelFactory.getWriterWithTemp(null, out, null, false); |
||||
Sheet sheet1 = new Sheet(1, 0); |
||||
sheet1.setSheetName("第一个sheet"); |
||||
writer.write0(data(), sheet1, null); |
||||
writer.finish(); |
||||
out.close(); |
||||
|
||||
InputStream inputStream = new FileInputStream(file); |
||||
ExcelReader excelReader = EasyExcelFactory.getReader(inputStream, new CompatibilityDataListener()); |
||||
excelReader.read(new Sheet(1, 0)); |
||||
inputStream.close(); |
||||
} |
||||
|
||||
private void readAndWrite6(File file) throws Exception { |
||||
OutputStream out = new FileOutputStream(file); |
||||
ExcelWriter writer = EasyExcelFactory.getWriterWithTempAndHandler(null, out, null, false, null); |
||||
Sheet sheet1 = new Sheet(1, 0); |
||||
sheet1.setSheetName("第一个sheet"); |
||||
writer.write0(data(), sheet1, null); |
||||
writer.finish(); |
||||
out.close(); |
||||
|
||||
InputStream inputStream = new FileInputStream(file); |
||||
ExcelReader excelReader = EasyExcelFactory.getReader(inputStream, new CompatibilityDataListener()); |
||||
excelReader.read(new Sheet(1, 0)); |
||||
inputStream.close(); |
||||
} |
||||
|
||||
private List<CompatibilityData> data() { |
||||
List<CompatibilityData> list = new ArrayList<CompatibilityData>(); |
||||
for (int i = 0; i < 10; i++) { |
||||
CompatibilityData data = new CompatibilityData(); |
||||
data.setString0("字符串0" + i); |
||||
data.setString1("字符串1" + i); |
||||
list.add(data); |
||||
} |
||||
return list; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,14 @@
|
||||
package com.alibaba.easyexcel.test.core.compatibility; |
||||
|
||||
import com.alibaba.excel.metadata.BaseRowModel; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Data |
||||
public class CompatibilityReadData extends BaseRowModel { |
||||
private String string0; |
||||
private String string1; |
||||
} |
@ -0,0 +1,14 @@
|
||||
package com.alibaba.easyexcel.test.core.exception; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Data |
||||
public class ExceptionData { |
||||
@ExcelProperty("姓名") |
||||
private String name; |
||||
} |
@ -0,0 +1,48 @@
|
||||
package com.alibaba.easyexcel.test.core.exception; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Assert; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ExceptionDataListener extends AnalysisEventListener<ExceptionData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionData.class); |
||||
List<ExceptionData> list = new ArrayList<ExceptionData>(); |
||||
|
||||
@Override |
||||
public void onException(Exception exception, AnalysisContext context) { |
||||
LOGGER.info("抛出异常,忽略:{}", exception.getMessage()); |
||||
} |
||||
|
||||
@Override |
||||
public boolean hasNext(AnalysisContext context) { |
||||
return list.size() != 8; |
||||
} |
||||
|
||||
@Override |
||||
public void invoke(ExceptionData data, AnalysisContext context) { |
||||
list.add(data); |
||||
if (list.size() == 5) { |
||||
int i = 5 / 0; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.size(), 8); |
||||
Assert.assertEquals(list.get(0).getName(), "姓名0"); |
||||
Assert.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); |
||||
Assert.assertEquals( |
||||
context.readSheetHolder().getExcelReadHeadProperty().getHeadMap().get(0).getHeadNameList().get(0), "姓名"); |
||||
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); |
||||
} |
||||
} |
@ -0,0 +1,58 @@
|
||||
package com.alibaba.easyexcel.test.core.exception; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileOutputStream; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.BeforeClass; |
||||
import org.junit.FixMethodOrder; |
||||
import org.junit.Test; |
||||
import org.junit.runners.MethodSorters; |
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
|
||||
/** |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
||||
public class ExceptionDataTest { |
||||
|
||||
private static File file07; |
||||
private static File file03; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
file07 = TestFileUtil.createNewFile("simple07.xlsx"); |
||||
file03 = TestFileUtil.createNewFile("simple03.xls"); |
||||
} |
||||
|
||||
@Test |
||||
public void T01ReadAndWrite07() throws Exception { |
||||
readAndWrite(file07); |
||||
} |
||||
|
||||
@Test |
||||
public void T02ReadAndWrite03() throws Exception { |
||||
readAndWrite(file03); |
||||
} |
||||
|
||||
private void readAndWrite(File file) throws Exception { |
||||
EasyExcelFactory.write(new FileOutputStream(file), ExceptionData.class).sheet().doWrite(data()).finish(); |
||||
EasyExcelFactory.read(new FileInputStream(file), ExceptionData.class, new ExceptionDataListener()).sheet() |
||||
.doRead().finish(); |
||||
} |
||||
|
||||
private List<ExceptionData> data() { |
||||
List<ExceptionData> list = new ArrayList<ExceptionData>(); |
||||
for (int i = 0; i < 10; i++) { |
||||
ExceptionData simpleData = new ExceptionData(); |
||||
simpleData.setName("姓名" + i); |
||||
list.add(simpleData); |
||||
} |
||||
return list; |
||||
} |
||||
} |
@ -0,0 +1,14 @@
|
||||
package com.alibaba.easyexcel.test.core.parameter; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Data |
||||
public class ParameterData { |
||||
@ExcelProperty("姓名") |
||||
private String name; |
||||
} |
@ -0,0 +1,35 @@
|
||||
package com.alibaba.easyexcel.test.core.parameter; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Assert; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ParameterDataListener extends AnalysisEventListener<ParameterData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ParameterDataListener.class); |
||||
List<ParameterData> list = new ArrayList<ParameterData>(); |
||||
|
||||
@Override |
||||
public void invoke(ParameterData data, AnalysisContext context) { |
||||
list.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.size(), 10); |
||||
Assert.assertEquals(list.get(0).getName(), "姓名0"); |
||||
Assert.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); |
||||
Assert.assertEquals( |
||||
context.readSheetHolder().getExcelReadHeadProperty().getHeadMap().get(0).getHeadNameList().get(0), "姓名"); |
||||
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); |
||||
} |
||||
} |
@ -0,0 +1,135 @@
|
||||
package com.alibaba.easyexcel.test.core.parameter; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.BeforeClass; |
||||
import org.junit.FixMethodOrder; |
||||
import org.junit.Test; |
||||
import org.junit.runners.MethodSorters; |
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
import com.alibaba.excel.ExcelReader; |
||||
import com.alibaba.excel.ExcelWriter; |
||||
import com.alibaba.excel.cache.MapCache; |
||||
import com.alibaba.excel.converters.string.StringStringConverter; |
||||
import com.alibaba.excel.read.metadata.ReadSheet; |
||||
import com.alibaba.excel.write.metadata.WriteSheet; |
||||
import com.alibaba.excel.write.metadata.WriteTable; |
||||
|
||||
/** |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
||||
public class ParameterDataTest { |
||||
|
||||
private static File file; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
file = TestFileUtil.createNewFile("parameter07.xlsx"); |
||||
} |
||||
|
||||
@Test |
||||
public void T01ReadAndWrite() throws Exception { |
||||
readAndWrite1(); |
||||
readAndWrite2(); |
||||
readAndWrite3(); |
||||
readAndWrite4(); |
||||
readAndWrite5(); |
||||
readAndWrite6(); |
||||
readAndWrite7(); |
||||
} |
||||
|
||||
private void readAndWrite1() { |
||||
EasyExcelFactory.write(file.getPath()).head(ParameterData.class).sheet().doWrite(data()).finish(); |
||||
EasyExcelFactory.read(file.getPath()).head(ParameterData.class) |
||||
.registerReadListener(new ParameterDataListener()).sheet().doRead().finish(); |
||||
} |
||||
|
||||
private void readAndWrite2() { |
||||
EasyExcelFactory.write(file.getPath(), ParameterData.class).sheet().doWrite(data()).finish(); |
||||
EasyExcelFactory.read(file.getPath(), ParameterData.class, new ParameterDataListener()).sheet().doRead() |
||||
.finish(); |
||||
} |
||||
|
||||
private void readAndWrite3() throws Exception { |
||||
EasyExcelFactory.write(new FileOutputStream(file)).head(ParameterData.class).sheet().doWrite(data()).finish(); |
||||
EasyExcelFactory.read(file.getPath()).head(ParameterData.class) |
||||
.registerReadListener(new ParameterDataListener()).sheet().doRead().finish(); |
||||
} |
||||
|
||||
private void readAndWrite4() throws Exception { |
||||
EasyExcelFactory.write(new FileOutputStream(file), ParameterData.class).sheet().doWrite(data()).finish(); |
||||
EasyExcelFactory.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class).sheet().doRead() |
||||
.finish(); |
||||
} |
||||
|
||||
private void readAndWrite5() throws Exception { |
||||
ExcelWriter excelWriter = EasyExcelFactory.write(new FileOutputStream(file)).head(ParameterData.class) |
||||
.relativeHeadRowIndex(0).build(); |
||||
WriteSheet writeSheet = EasyExcelFactory.writerSheet(0).relativeHeadRowIndex(0).needHead(Boolean.FALSE).build(); |
||||
WriteTable writeTable = EasyExcelFactory.writerTable(0).relativeHeadRowIndex(0).needHead(Boolean.TRUE).build(); |
||||
excelWriter.write(data(), writeSheet, writeTable); |
||||
excelWriter.finish(); |
||||
|
||||
ExcelReader excelReader = |
||||
EasyExcelFactory.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class) |
||||
.mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build(); |
||||
ReadSheet readSheet = EasyExcelFactory.readSheet().head(ParameterData.class).use1904windowing(Boolean.FALSE) |
||||
.headRowNumber(1).sheetNo(0).sheetName("0").build(); |
||||
excelReader.read(readSheet); |
||||
excelReader.finish(); |
||||
|
||||
excelReader = EasyExcelFactory.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class) |
||||
.mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build(); |
||||
excelReader.read(); |
||||
excelReader.finish(); |
||||
} |
||||
|
||||
private void readAndWrite6() throws Exception { |
||||
ExcelWriter excelWriter = EasyExcelFactory.write(new FileOutputStream(file)).head(ParameterData.class) |
||||
.relativeHeadRowIndex(0).build(); |
||||
WriteSheet writeSheet = EasyExcelFactory.writerSheet(0).relativeHeadRowIndex(0).needHead(Boolean.FALSE).build(); |
||||
WriteTable writeTable = EasyExcelFactory.writerTable(0).registerConverter(new StringStringConverter()) |
||||
.relativeHeadRowIndex(0).needHead(Boolean.TRUE).build(); |
||||
excelWriter.write(data(), writeSheet, writeTable); |
||||
excelWriter.finish(); |
||||
|
||||
ExcelReader excelReader = |
||||
EasyExcelFactory.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class) |
||||
.mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build(); |
||||
ReadSheet readSheet = EasyExcelFactory.readSheet("0").head(ParameterData.class).use1904windowing(Boolean.FALSE) |
||||
.headRowNumber(1).sheetNo(0).build(); |
||||
excelReader.read(readSheet); |
||||
excelReader.finish(); |
||||
|
||||
excelReader = EasyExcelFactory.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class) |
||||
.mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build(); |
||||
excelReader.read(); |
||||
excelReader.finish(); |
||||
} |
||||
|
||||
private void readAndWrite7() throws Exception { |
||||
EasyExcelFactory.write(file, ParameterData.class).registerConverter(new StringStringConverter()).sheet() |
||||
.registerConverter(new StringStringConverter()).needHead(Boolean.FALSE).table(0).needHead(Boolean.TRUE) |
||||
.doWrite(data()).finish(); |
||||
EasyExcelFactory.read(file.getPath()).head(ParameterData.class) |
||||
.registerReadListener(new ParameterDataListener()).sheet().registerConverter(new StringStringConverter()) |
||||
.doRead().finish(); |
||||
} |
||||
|
||||
private List<ParameterData> data() { |
||||
List<ParameterData> list = new ArrayList<ParameterData>(); |
||||
for (int i = 0; i < 10; i++) { |
||||
ParameterData simpleData = new ParameterData(); |
||||
simpleData.setName("姓名" + i); |
||||
list.add(simpleData); |
||||
} |
||||
return list; |
||||
} |
||||
} |
@ -1,59 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.listen; |
||||
|
||||
import com.alibaba.excel.event.WriteHandler; |
||||
import org.apache.poi.ss.usermodel.*; |
||||
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; |
||||
import org.apache.poi.xssf.usermodel.XSSFRichTextString; |
||||
|
||||
public class AfterExcelWriteHandlerImpl implements WriteHandler { |
||||
|
||||
//
|
||||
CellStyle cellStyle; |
||||
|
||||
@Override |
||||
public void sheet(int sheetNo, Sheet sheet) { |
||||
Workbook workbook = sheet.getWorkbook(); |
||||
//要锁定单元格需先为此表单设置保护密码,设置之后此表单默认为所有单元格锁定,可使用setLocked(false)为指定单元格设置不锁定。
|
||||
//设置表单保护密码
|
||||
sheet.protectSheet("your password"); |
||||
//创建样式
|
||||
cellStyle = workbook.createCellStyle(); |
||||
//设置是否锁
|
||||
cellStyle.setLocked(false); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void row(int rowNum, Row row) { |
||||
Workbook workbook = row.getSheet().getWorkbook(); |
||||
//设置行高
|
||||
row.setHeight((short)20); |
||||
} |
||||
|
||||
@Override |
||||
public void cell(int cellNum, Cell cell) { |
||||
Workbook workbook = cell.getSheet().getWorkbook(); |
||||
Sheet currentSheet = cell.getSheet(); |
||||
if (cellNum == 4 && cell.getRowIndex() == 30) { |
||||
//设置样式
|
||||
//注意:样式最好采用公用样式,样式在创建sheet后创建,如果有多个样式也需要在创建sheet时候创建后面直接使用,不要每个Cell Create 一个样式,不然会导致报错 The maximum number
|
||||
// of Cell Styles was exceeded.
|
||||
cell.setCellStyle(cellStyle); |
||||
|
||||
|
||||
|
||||
//设置备注
|
||||
Drawing draw = currentSheet.createDrawingPatriarch(); |
||||
Comment comment = draw.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, 4, 25, 9, 30)); |
||||
XSSFRichTextString rtf = new XSSFRichTextString("添加批注内容收到货死的死哦多胡搜idsad是否会杜甫的范德萨发!1111"); |
||||
Font commentFormatter = workbook.createFont(); |
||||
commentFormatter.setFontName("宋体"); |
||||
//设置字体大小
|
||||
commentFormatter.setFontHeightInPoints((short)9); |
||||
rtf.applyFont(commentFormatter); |
||||
comment.setString(rtf); |
||||
comment.setAuthor("ceshi"); |
||||
cell.setCellComment(comment); |
||||
} |
||||
} |
||||
} |
@ -1,33 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.listen; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class ExcelListener extends AnalysisEventListener { |
||||
|
||||
|
||||
private List<Object> data = new ArrayList<Object>(); |
||||
|
||||
@Override |
||||
public void invoke(Object object, AnalysisContext context) { |
||||
System.out.println(context.getCurrentSheet()); |
||||
data.add(object); |
||||
if(data.size()>=100){ |
||||
doSomething(); |
||||
data = new ArrayList<Object>(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
doSomething(); |
||||
} |
||||
public void doSomething(){ |
||||
for (Object o:data) { |
||||
System.out.println(o); |
||||
} |
||||
} |
||||
} |
@ -1,28 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.model; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.metadata.BaseRowModel; |
||||
|
||||
public class BaseReadModel extends BaseRowModel { |
||||
@ExcelProperty(index = 0) |
||||
protected String str; |
||||
|
||||
@ExcelProperty(index = 1) |
||||
protected Float ff; |
||||
public String getStr() { |
||||
return str; |
||||
} |
||||
|
||||
|
||||
public void setStr(String str) { |
||||
this.str = str; |
||||
} |
||||
|
||||
public Float getFf() { |
||||
return ff; |
||||
} |
||||
|
||||
public void setFf(Float ff) { |
||||
this.ff = ff; |
||||
} |
||||
} |
@ -1,28 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.model; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.metadata.BaseRowModel; |
||||
|
||||
public class BaseWriteModel extends BaseRowModel { |
||||
@ExcelProperty(value = {"表头1","表头1","表头31"},index = 0) |
||||
protected String p1; |
||||
|
||||
@ExcelProperty(value = {"表头1","表头1","表头32"},index = 1) |
||||
protected String p2; |
||||
|
||||
public String getP1() { |
||||
return p1; |
||||
} |
||||
|
||||
public void setP1(String p1) { |
||||
this.p1 = p1; |
||||
} |
||||
|
||||
public String getP2() { |
||||
return p2; |
||||
} |
||||
|
||||
public void setP2(String p2) { |
||||
this.p2 = p2; |
||||
} |
||||
} |
@ -1,114 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.model; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.util.Date; |
||||
|
||||
public class ReadModel extends BaseReadModel { |
||||
|
||||
@ExcelProperty(index = 2) |
||||
private Integer mm; |
||||
|
||||
@ExcelProperty(index = 3) |
||||
private BigDecimal money; |
||||
|
||||
@ExcelProperty(index = 4) |
||||
private Long times; |
||||
|
||||
@ExcelProperty(index = 5) |
||||
private Double activityCode; |
||||
|
||||
@ExcelProperty(index = 6,format = "yyyy-MM-dd") |
||||
private Date date; |
||||
|
||||
@ExcelProperty(index = 7) |
||||
private String lx; |
||||
|
||||
@ExcelProperty(index = 8) |
||||
private String name; |
||||
|
||||
@ExcelProperty(index = 18) |
||||
private String kk; |
||||
|
||||
|
||||
public Integer getMm() { |
||||
return mm; |
||||
} |
||||
|
||||
public void setMm(Integer mm) { |
||||
this.mm = mm; |
||||
} |
||||
|
||||
public BigDecimal getMoney() { |
||||
return money; |
||||
} |
||||
|
||||
public void setMoney(BigDecimal money) { |
||||
this.money = money; |
||||
} |
||||
|
||||
public Long getTimes() { |
||||
return times; |
||||
} |
||||
|
||||
public void setTimes(Long times) { |
||||
this.times = times; |
||||
} |
||||
|
||||
public Double getActivityCode() { |
||||
return activityCode; |
||||
} |
||||
|
||||
public void setActivityCode(Double activityCode) { |
||||
this.activityCode = activityCode; |
||||
} |
||||
|
||||
public Date getDate() { |
||||
return date; |
||||
} |
||||
|
||||
public void setDate(Date date) { |
||||
this.date = date; |
||||
} |
||||
|
||||
public String getLx() { |
||||
return lx; |
||||
} |
||||
|
||||
public void setLx(String lx) { |
||||
this.lx = lx; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public String getKk() { |
||||
return kk; |
||||
} |
||||
|
||||
public void setKk(String kk) { |
||||
this.kk = kk; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "JavaModel{" + |
||||
"str='" + str + '\'' + |
||||
", ff=" + ff + |
||||
", mm=" + mm + |
||||
", money=" + money + |
||||
", times=" + times + |
||||
", activityCode=" + activityCode + |
||||
", date=" + date + |
||||
", lx='" + lx + '\'' + |
||||
", name='" + name + '\'' + |
||||
", kk='" + kk + '\'' + |
||||
'}'; |
||||
} |
||||
} |
@ -1,136 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.model; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.metadata.BaseRowModel; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.util.Date; |
||||
|
||||
public class ReadModel2 extends BaseRowModel { |
||||
@ExcelProperty(index = 0) |
||||
private String str; |
||||
|
||||
@ExcelProperty(index = 1) |
||||
private Float ff; |
||||
|
||||
@ExcelProperty(index = 2) |
||||
private Integer mm; |
||||
|
||||
@ExcelProperty(index = 3) |
||||
private BigDecimal money; |
||||
|
||||
@ExcelProperty(index = 4) |
||||
private Long times; |
||||
|
||||
@ExcelProperty(index = 5) |
||||
private Double activityCode; |
||||
|
||||
@ExcelProperty(index = 6,format = "yyyy-MM-dd") |
||||
private Date date; |
||||
|
||||
@ExcelProperty(index = 7) |
||||
private String lx; |
||||
|
||||
@ExcelProperty(index = 8) |
||||
private String name; |
||||
|
||||
@ExcelProperty(index = 18) |
||||
private String kk; |
||||
|
||||
public String getStr() { |
||||
return str; |
||||
} |
||||
|
||||
|
||||
public void setStr(String str) { |
||||
this.str = str; |
||||
} |
||||
|
||||
public Float getFf() { |
||||
return ff; |
||||
} |
||||
|
||||
public void setFf(Float ff) { |
||||
this.ff = ff; |
||||
} |
||||
|
||||
public Integer getMm() { |
||||
return mm; |
||||
} |
||||
|
||||
public void setMm(Integer mm) { |
||||
this.mm = mm; |
||||
} |
||||
|
||||
public BigDecimal getMoney() { |
||||
return money; |
||||
} |
||||
|
||||
public void setMoney(BigDecimal money) { |
||||
this.money = money; |
||||
} |
||||
|
||||
public Long getTimes() { |
||||
return times; |
||||
} |
||||
|
||||
public void setTimes(Long times) { |
||||
this.times = times; |
||||
} |
||||
|
||||
public Double getActivityCode() { |
||||
return activityCode; |
||||
} |
||||
|
||||
public void setActivityCode(Double activityCode) { |
||||
this.activityCode = activityCode; |
||||
} |
||||
|
||||
public Date getDate() { |
||||
return date; |
||||
} |
||||
|
||||
public void setDate(Date date) { |
||||
this.date = date; |
||||
} |
||||
|
||||
public String getLx() { |
||||
return lx; |
||||
} |
||||
|
||||
public void setLx(String lx) { |
||||
this.lx = lx; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public String getKk() { |
||||
return kk; |
||||
} |
||||
|
||||
public void setKk(String kk) { |
||||
this.kk = kk; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "JavaModel2{" + |
||||
"str='" + str + '\'' + |
||||
", ff=" + ff + |
||||
", mm=" + mm + |
||||
", money=" + money + |
||||
", times=" + times + |
||||
", activityCode=" + activityCode + |
||||
", date=" + date + |
||||
", lx='" + lx + '\'' + |
||||
", name='" + name + '\'' + |
||||
", kk='" + kk + '\'' + |
||||
'}'; |
||||
} |
||||
} |
@ -1,131 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.model; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.util.Date; |
||||
|
||||
public class WriteModel extends BaseWriteModel { |
||||
|
||||
|
||||
|
||||
@ExcelProperty(value = {"表头3","表头3","表头3"},index = 2) |
||||
private int p3; |
||||
|
||||
@ExcelProperty(value = {"表头1","表头4","表头4"},index = 3) |
||||
private long p4; |
||||
|
||||
@ExcelProperty(value = {"表头5","表头51","表头52"},index = 4) |
||||
private String p5; |
||||
|
||||
@ExcelProperty(value = {"表头6","表头61","表头611"},index = 5) |
||||
private float p6; |
||||
|
||||
@ExcelProperty(value = {"表头6","表头61","表头612"},index = 6) |
||||
private BigDecimal p7; |
||||
|
||||
@ExcelProperty(value = {"表头6","表头62","表头621"},index = 7, format = "yyyy-MM-dd") |
||||
private Date p8; |
||||
|
||||
@ExcelProperty(value = {"表头6","表头62","表头622"},index = 8) |
||||
private String p9; |
||||
|
||||
@ExcelProperty(value = {"表头6","表头62","表头622"},index = 9) |
||||
private double p10; |
||||
|
||||
public String getP1() { |
||||
return p1; |
||||
} |
||||
|
||||
public void setP1(String p1) { |
||||
this.p1 = p1; |
||||
} |
||||
|
||||
public String getP2() { |
||||
return p2; |
||||
} |
||||
|
||||
public void setP2(String p2) { |
||||
this.p2 = p2; |
||||
} |
||||
|
||||
public int getP3() { |
||||
return p3; |
||||
} |
||||
|
||||
public void setP3(int p3) { |
||||
this.p3 = p3; |
||||
} |
||||
|
||||
public long getP4() { |
||||
return p4; |
||||
} |
||||
|
||||
public void setP4(long p4) { |
||||
this.p4 = p4; |
||||
} |
||||
|
||||
public String getP5() { |
||||
return p5; |
||||
} |
||||
|
||||
public void setP5(String p5) { |
||||
this.p5 = p5; |
||||
} |
||||
|
||||
public float getP6() { |
||||
return p6; |
||||
} |
||||
|
||||
public void setP6(float p6) { |
||||
this.p6 = p6; |
||||
} |
||||
|
||||
public BigDecimal getP7() { |
||||
return p7; |
||||
} |
||||
|
||||
public void setP7(BigDecimal p7) { |
||||
this.p7 = p7; |
||||
} |
||||
|
||||
public Date getP8() { |
||||
return p8; |
||||
} |
||||
|
||||
public void setP8(Date p8) { |
||||
this.p8 = p8; |
||||
} |
||||
|
||||
public String getP9() { |
||||
return p9; |
||||
} |
||||
|
||||
public void setP9(String p9) { |
||||
this.p9 = p9; |
||||
} |
||||
|
||||
public double getP10() { |
||||
return p10; |
||||
} |
||||
|
||||
public void setP10(double p10) { |
||||
this.p10 = p10; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "JavaModel1{" + |
||||
"p1='" + p1 + '\'' + |
||||
", p2='" + p2 + '\'' + |
||||
", p3=" + p3 + |
||||
", p4=" + p4 + |
||||
", p5='" + p5 + '\'' + |
||||
", p6=" + p6 + |
||||
", p7=" + p7 + |
||||
", p8=" + p8 + |
||||
", p9='" + p9 + '\'' + |
||||
", p10=" + p10 + |
||||
'}'; |
||||
} |
||||
} |
@ -1,93 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.util; |
||||
|
||||
import com.alibaba.easyexcel.test.model.WriteModel; |
||||
import com.alibaba.excel.metadata.Font; |
||||
import com.alibaba.excel.metadata.TableStyle; |
||||
import org.apache.poi.ss.usermodel.IndexedColors; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.util.ArrayList; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
public class DataUtil { |
||||
|
||||
|
||||
public static List<List<Object>> createTestListObject() { |
||||
List<List<Object>> object = new ArrayList<List<Object>>(); |
||||
for (int i = 0; i < 1000; i++) { |
||||
List<Object> da = new ArrayList<Object>(); |
||||
da.add("字符串"+i); |
||||
da.add(Long.valueOf(187837834l+i)); |
||||
da.add(Integer.valueOf(2233+i)); |
||||
da.add(Double.valueOf(2233.00+i)); |
||||
da.add(Float.valueOf(2233.0f+i)); |
||||
da.add(new Date()); |
||||
da.add(new BigDecimal("3434343433554545"+i)); |
||||
da.add(Short.valueOf((short)i)); |
||||
object.add(da); |
||||
} |
||||
return object; |
||||
} |
||||
|
||||
public static List<List<String>> createTestListStringHead(){ |
||||
//写sheet3 模型上没有注解,表头数据动态传入
|
||||
List<List<String>> head = new ArrayList<List<String>>(); |
||||
List<String> headCoulumn1 = new ArrayList<String>(); |
||||
List<String> headCoulumn2 = new ArrayList<String>(); |
||||
List<String> headCoulumn3 = new ArrayList<String>(); |
||||
List<String> headCoulumn4 = new ArrayList<String>(); |
||||
List<String> headCoulumn5 = new ArrayList<String>(); |
||||
|
||||
headCoulumn1.add("第一列");headCoulumn1.add("第一列");headCoulumn1.add("第一列"); |
||||
headCoulumn2.add("第一列");headCoulumn2.add("第一列");headCoulumn2.add("第一列"); |
||||
|
||||
headCoulumn3.add("第二列");headCoulumn3.add("第二列");headCoulumn3.add("第二列"); |
||||
headCoulumn4.add("第三列");headCoulumn4.add("第三列2");headCoulumn4.add("第三列2"); |
||||
headCoulumn5.add("第一列");headCoulumn5.add("第3列");headCoulumn5.add("第4列"); |
||||
|
||||
head.add(headCoulumn1); |
||||
head.add(headCoulumn2); |
||||
head.add(headCoulumn3); |
||||
head.add(headCoulumn4); |
||||
head.add(headCoulumn5); |
||||
return head; |
||||
} |
||||
|
||||
public static List<WriteModel> createTestListJavaMode(){ |
||||
List<WriteModel> model1s = new ArrayList<WriteModel>(); |
||||
for (int i = 0; i <10000 ; i++) { |
||||
WriteModel model1 = new WriteModel(); |
||||
model1.setP1("第一列,第行"); |
||||
model1.setP2("121212jjj"); |
||||
model1.setP3(33+i); |
||||
model1.setP4(44); |
||||
model1.setP5("555"); |
||||
model1.setP6(666.2f); |
||||
model1.setP7(new BigDecimal("454545656343434"+i)); |
||||
model1.setP8(new Date()); |
||||
model1.setP9("llll9999>&&&&&6666^^^^"); |
||||
model1.setP10(1111.77+i); |
||||
model1s.add(model1); |
||||
} |
||||
return model1s; |
||||
} |
||||
|
||||
public static TableStyle createTableStyle() { |
||||
TableStyle tableStyle = new TableStyle(); |
||||
Font headFont = new Font(); |
||||
headFont.setBold(true); |
||||
headFont.setFontHeightInPoints((short)22); |
||||
headFont.setFontName("楷体"); |
||||
tableStyle.setTableHeadFont(headFont); |
||||
tableStyle.setTableHeadBackGroundColor(IndexedColors.BLUE); |
||||
|
||||
Font contentFont = new Font(); |
||||
contentFont.setBold(true); |
||||
contentFont.setFontHeightInPoints((short)22); |
||||
contentFont.setFontName("黑体"); |
||||
tableStyle.setTableContentFont(contentFont); |
||||
tableStyle.setTableContentBackGroundColor(IndexedColors.GREEN); |
||||
return tableStyle; |
||||
} |
||||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue