Browse Source

新增jdk8的版本

pull/2077/head
Jiaju Zhuang 3 years ago
parent
commit
ad5fd4967a
  1. 65
      src/main/java/com/alibaba/excel/ExcelWriter.java
  2. 13
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java
  3. 5
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java
  4. 20
      src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java
  5. 17
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

65
src/main/java/com/alibaba/excel/ExcelWriter.java

@ -1,6 +1,7 @@
package com.alibaba.excel;
import java.util.Collection;
import java.util.function.Supplier;
import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.write.ExcelBuilder;
@ -41,25 +42,31 @@ public class ExcelWriter {
/**
* Write data to a sheet
*
* @param data
* Data to be written
* @param writeSheet
* Write to this sheet
* @param data Data to be written
* @param writeSheet Write to this sheet
* @return this current writer
*/
public ExcelWriter write(Collection<?> data, WriteSheet writeSheet) {
return write(data, writeSheet, null);
}
/**
* Write data to a sheet
*
* @param supplier Data to be written
* @param writeSheet Write to this sheet
* @return this current writer
*/
public ExcelWriter write(Supplier<Collection<?>> supplier, WriteSheet writeSheet) {
return write(supplier.get(), writeSheet, null);
}
/**
* Write value to a sheet
*
* @param data
* Data to be written
* @param writeSheet
* Write to this sheet
* @param writeTable
* Write to this table
* @param data Data to be written
* @param writeSheet Write to this sheet
* @param writeTable Write to this table
* @return this
*/
public ExcelWriter write(Collection<?> data, WriteSheet writeSheet, WriteTable writeTable) {
@ -67,6 +74,19 @@ public class ExcelWriter {
return this;
}
/**
* Write value to a sheet
*
* @param supplier Data to be written
* @param writeSheet Write to this sheet
* @param writeTable Write to this table
* @return this
*/
public ExcelWriter write(Supplier<Collection<?>> supplier, WriteSheet writeSheet, WriteTable writeTable) {
excelBuilder.addContent(supplier.get(), writeSheet, writeTable);
return this;
}
/**
* Fill value to a sheet
*
@ -91,6 +111,30 @@ public class ExcelWriter {
return this;
}
/**
* Fill value to a sheet
*
* @param supplier
* @param writeSheet
* @return
*/
public ExcelWriter fill(Supplier<Object> supplier, WriteSheet writeSheet) {
return fill(supplier.get(), null, writeSheet);
}
/**
* Fill value to a sheet
*
* @param supplier
* @param fillConfig
* @param writeSheet
* @return
*/
public ExcelWriter fill(Supplier<Object> supplier, FillConfig fillConfig, WriteSheet writeSheet) {
excelBuilder.fill(supplier.get(), fillConfig, writeSheet);
return this;
}
/**
* Close IO
*/
@ -102,7 +146,6 @@ public class ExcelWriter {
/**
* Prevents calls to {@link #finish} from freeing the cache
*
*/
@Override
protected void finalize() {

13
src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java

@ -1,6 +1,7 @@
package com.alibaba.excel.write.builder;
import java.util.Collection;
import java.util.function.Supplier;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.exception.ExcelGenerateException;
@ -74,6 +75,18 @@ public class ExcelWriterSheetBuilder extends AbstractExcelWriterParameterBuilder
excelWriter.finish();
}
public void doWrite(Supplier<Collection<?>> supplier) {
doWrite(supplier.get());
}
public void doFill(Supplier<Object> supplier) {
doFill(supplier.get());
}
public void doFill(Supplier<Object> supplier, FillConfig fillConfig) {
doFill(supplier.get(), fillConfig);
}
public ExcelWriterTableBuilder table() {
return table(null);
}

5
src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java

@ -1,6 +1,7 @@
package com.alibaba.excel.write.builder;
import java.util.Collection;
import java.util.function.Supplier;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.exception.ExcelGenerateException;
@ -55,6 +56,10 @@ public class ExcelWriterTableBuilder extends AbstractExcelWriterParameterBuilder
excelWriter.finish();
}
public void doWrite(Supplier<Collection<?>> supplier) {
doWrite(supplier.get());
}
@Override
protected WriteTable parameter() {
return writeTable;

20
src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java

@ -7,9 +7,6 @@ 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.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
@ -18,11 +15,14 @@ import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import org.junit.Ignore;
import org.junit.Test;
/**
* 写的填充写法
*
* @since 2.1.1
* @author Jiaju Zhuang
* @since 2.1.1
*/
@Ignore
public class FillTest {
@ -71,7 +71,17 @@ public class FillTest {
// 这里 会填充到第一个sheet, 然后文件流会自动关闭
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(data());
// 方案2 分多次 填充 会使用文件缓存(省内存)
// 方案2 分多次 填充 会使用文件缓存(省内存) jdk8
fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
EasyExcel.write(fileName)
.withTemplate(templateFileName)
.sheet()
.doFill(() -> {
// 分页查询数据
return data();
});
// 方案3 分多次 填充 会使用文件缓存(省内存)
fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();

17
src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

@ -59,14 +59,25 @@ public class WriteTest {
*/
@Test
public void simpleWrite() {
// 写法1
// 写法1 JDK8+
String fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
EasyExcel.write(fileName, DemoData.class)
.sheet("模板")
.doWrite(() -> {
// 分页查询数据
return data();
});
// 写法2
fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
// 写法3
fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写
ExcelWriter excelWriter = null;
try {
@ -329,7 +340,6 @@ public class WriteTest {
commentData.setRelativeLastColumnIndex(1);
commentData.setRelativeLastRowIndex(1);
// 设置公式
WriteCellData<String> formula = new WriteCellData<>();
writeCellDemoData.setFormulaData(formula);
@ -339,7 +349,6 @@ public class WriteTest {
// 这里只是例子 如果真的涉及到公式 能内存算好尽量内存算好 公式能不用尽量不用
formulaData.setFormulaValue("REPLACE(123456789,1,1,2)");
// 设置单个单元格的样式 当然样式 很多的话 也可以用注解等方式。
WriteCellData<String> writeCellStyle = new WriteCellData<>("单元格样式");
writeCellDemoData.setWriteCellStyle(writeCellStyle);

Loading…
Cancel
Save