Browse Source

支持通过模板填充数据

pull/635/head
罗成 5 years ago
parent
commit
5331d8a6ca
  1. 8
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  2. 1
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  3. 16
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  4. 2
      src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java
  5. 37
      src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java
  6. 15
      src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java

8
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -360,15 +360,15 @@ public class WriteContextImpl implements WriteContext {
throwCanNotCloseIo(t); throwCanNotCloseIo(t);
} }
try { try {
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) { if (writeWorkbookHolder.getTempTemplateInputStream() != null) {
writeWorkbookHolder.getOutputStream().close(); writeWorkbookHolder.getTempTemplateInputStream().close();
} }
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwCanNotCloseIo(t);
} }
try { try {
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getTemplateInputStream() != null) { if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) {
writeWorkbookHolder.getTemplateInputStream().close(); writeWorkbookHolder.getOutputStream().close();
} }
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwCanNotCloseIo(t);

1
src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java

@ -21,7 +21,6 @@ import com.alibaba.excel.metadata.AbstractHolder;
import com.alibaba.excel.metadata.Font; import com.alibaba.excel.metadata.Font;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.TableStyle; import com.alibaba.excel.metadata.TableStyle;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.metadata.property.RowHeightProperty; import com.alibaba.excel.metadata.property.RowHeightProperty;
import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.CellWriteHandler;

16
src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java

@ -11,8 +11,6 @@ import java.util.Map;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.exception.ExcelGenerateException;
@ -60,6 +58,12 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
* If 'inputStream' and 'file' all not empty,file first * If 'inputStream' and 'file' all not empty,file first
*/ */
private File templateFile; private File templateFile;
/**
* Temporary template file stream.
* <p>
* A temporary file stream needs to be created in order not to modify the original template file.
*/
private InputStream tempTemplateInputStream;
/** /**
* Default true * Default true
*/ */
@ -182,6 +186,14 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
this.templateInputStream = templateInputStream; this.templateInputStream = templateInputStream;
} }
public InputStream getTempTemplateInputStream() {
return tempTemplateInputStream;
}
public void setTempTemplateInputStream(InputStream tempTemplateInputStream) {
this.tempTemplateInputStream = tempTemplateInputStream;
}
public File getTemplateFile() { public File getTemplateFile() {
return templateFile; return templateFile;
} }

2
src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java

@ -25,7 +25,7 @@ public class LockTest {
public void test() throws Exception { public void test() throws Exception {
List<Object> list = List<Object> list =
EasyExcel.read(new FileInputStream("D:\\test\\t222.xlsx")).sheet().headRowNumber(0).doReadSync(); EasyExcel.read(new FileInputStream("D:\\test\\testbug嘉惠.xlsx")).sheet().headRowNumber(0).doReadSync();
for (Object data : list) { for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data)); LOGGER.info("返回数据:{}", JSON.toJSONString(data));
} }

37
src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java

@ -1,6 +1,7 @@
package com.alibaba.easyexcel.test.temp.poi; package com.alibaba.easyexcel.test.temp.poi;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -19,6 +20,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.IoUtils;
import ch.qos.logback.core.util.FileUtil;
/** /**
* 测试poi * 测试poi
@ -58,6 +63,7 @@ public class PoiTest {
public void lastRowNum233() throws IOException { public void lastRowNum233() throws IOException {
String file = TestFileUtil.getPath() + "fill" + File.separator + "simple.xlsx"; String file = TestFileUtil.getPath() + "fill" + File.separator + "simple.xlsx";
Workbook xx = new XSSFWorkbook(file); Workbook xx = new XSSFWorkbook(file);
System.out.println(new File(file).exists());
SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(); SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook();
Sheet xssfSheet = xssfWorkbook.getXSSFWorkbook().getSheetAt(0); Sheet xssfSheet = xssfWorkbook.getXSSFWorkbook().getSheetAt(0);
@ -70,12 +76,39 @@ public class PoiTest {
xssfWorkbook.close(); xssfWorkbook.close();
} }
@Test
public void testread() throws IOException {
String file = TestFileUtil.getPath() + "fill" + File.separator + "simple.xlsx";
SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(new XSSFWorkbook(file));
Sheet xssfSheet = xssfWorkbook.getXSSFWorkbook().getSheetAt(0);
//
// Cell cell = xssfSheet.getRow(0).createCell(9);
String file1 = TestFileUtil.getPath() + "fill" + File.separator + "simple.xlsx";
SXSSFWorkbook xssfWorkbook1 = new SXSSFWorkbook(new XSSFWorkbook(file1));
Sheet xssfSheet1 = xssfWorkbook1.getXSSFWorkbook().getSheetAt(0);
// Cell cell1 = xssfSheet1.getRow(0).createCell(9);
xssfWorkbook.close();
xssfWorkbook1.close();
}
@Test
public void testreadRead() throws IOException {
String file = TestFileUtil.getPath() + "fill" + File.separator + "simple.xlsx";
FileUtils.readFileToByteArray(new File(file));
}
@Test @Test
public void lastRowNum2332222() throws IOException { public void lastRowNum2332222() throws IOException {
String file = TestFileUtil.getPath() + "fill" + File.separator + "simple.xlsx"; String file = TestFileUtil.getPath() + "fill" + File.separator + "simple.xlsx";
Workbook xx=new XSSFWorkbook(file); SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(new XSSFWorkbook(file));
SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook();
Sheet xssfSheet = xssfWorkbook.getXSSFWorkbook().getSheetAt(0); Sheet xssfSheet = xssfWorkbook.getXSSFWorkbook().getSheetAt(0);
Cell cell = xssfSheet.getRow(0).createCell(9); Cell cell = xssfSheet.getRow(0).createCell(9);

15
src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java

@ -2,6 +2,8 @@ package com.alibaba.easyexcel.test.temp.simple;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List; import java.util.List;
import org.junit.Ignore; import org.junit.Ignore;
@ -24,7 +26,18 @@ public class HgTest {
@Test @Test
public void hh() throws IOException { public void hh() throws IOException {
List<Object> list = List<Object> list =
EasyExcel.read(new FileInputStream("D:\\test\\折扣2007.xls")).headRowNumber(0).sheet().doReadSync(); EasyExcel.read(new FileInputStream("D:\\test\\嘉惠-中交建_2019-09-01_2019-09-30_1569055677522.xlsx")).headRowNumber(0).sheet().doReadSync();
for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data));
}
}
@Test
public void hh5() throws IOException {
URL url = new URL("http://hotelcontractfil.oss-cn-beijing.aliyuncs.com/2019/%E5%98%89%E6%83%A0-%E4%B8%AD%E4%BA%A4%E5%BB%BA_2019-09-01_2019-09-30_1569055677522.xlsx?Expires=1884415681&OSSAccessKeyId=LTAIGZDkqZfPArBr&Signature=Rf0gbO8vl3l%2Brj1KdyzHHMsUhCE%3D");
InputStream is = url.openStream();
List<Object> list =
EasyExcel.read(is).headRowNumber(0).sheet().doReadSync();
for (Object data : list) { for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data)); LOGGER.info("返回数据:{}", JSON.toJSONString(data));
} }

Loading…
Cancel
Save