Browse Source

支持通过模板填充数据

developing
罗成 5 years ago
parent
commit
5331d8a6ca
  1. 28
      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. 39
      src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java
  6. 15
      src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java

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

@ -83,7 +83,7 @@ public class WriteContextImpl implements WriteContext {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof WorkbookWriteHandler) {
((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate();
((WorkbookWriteHandler) writeHandler).beforeWorkbookCreate();
}
}
}
@ -95,7 +95,7 @@ public class WriteContextImpl implements WriteContext {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof WorkbookWriteHandler) {
((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(writeWorkbookHolder);
((WorkbookWriteHandler) writeHandler).afterWorkbookCreate(writeWorkbookHolder);
}
}
}
@ -149,7 +149,7 @@ public class WriteContextImpl implements WriteContext {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof SheetWriteHandler) {
((SheetWriteHandler)writeHandler).beforeSheetCreate(writeWorkbookHolder, writeSheetHolder);
((SheetWriteHandler) writeHandler).beforeSheetCreate(writeWorkbookHolder, writeSheetHolder);
}
}
}
@ -161,7 +161,7 @@ public class WriteContextImpl implements WriteContext {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof SheetWriteHandler) {
((SheetWriteHandler)writeHandler).afterSheetCreate(writeWorkbookHolder, writeSheetHolder);
((SheetWriteHandler) writeHandler).afterSheetCreate(writeWorkbookHolder, writeSheetHolder);
}
}
if (null != writeWorkbookHolder.getWriteWorkbook().getWriteHandler()) {
@ -204,7 +204,7 @@ public class WriteContextImpl implements WriteContext {
// Combined head
addMergedRegionToCurrentSheet(excelWriteHeadProperty, newRowIndex);
for (int relativeRowIndex = 0, i = newRowIndex; i < excelWriteHeadProperty.getHeadRowNumber() + newRowIndex;
i++, relativeRowIndex++) {
i++, relativeRowIndex++) {
beforeRowCreate(newRowIndex, relativeRowIndex);
Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i);
afterRowCreate(row, relativeRowIndex);
@ -219,7 +219,7 @@ public class WriteContextImpl implements WriteContext {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) {
((RowWriteHandler)writeHandler).beforeRowCreate(writeSheetHolder, writeTableHolder, rowIndex,
((RowWriteHandler) writeHandler).beforeRowCreate(writeSheetHolder, writeTableHolder, rowIndex,
relativeRowIndex, true);
}
}
@ -232,7 +232,7 @@ public class WriteContextImpl implements WriteContext {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) {
((RowWriteHandler)writeHandler).afterRowCreate(writeSheetHolder, writeTableHolder, row,
((RowWriteHandler) writeHandler).afterRowCreate(writeSheetHolder, writeTableHolder, row,
relativeRowIndex, true);
}
}
@ -264,7 +264,7 @@ public class WriteContextImpl implements WriteContext {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) {
((CellWriteHandler)writeHandler).beforeCellCreate(writeSheetHolder, writeTableHolder, row, head,
((CellWriteHandler) writeHandler).beforeCellCreate(writeSheetHolder, writeTableHolder, row, head,
relativeRowIndex, true);
}
}
@ -277,7 +277,7 @@ public class WriteContextImpl implements WriteContext {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) {
((CellWriteHandler)writeHandler).afterCellCreate(writeSheetHolder, writeTableHolder, null, cell, head,
((CellWriteHandler) writeHandler).afterCellCreate(writeSheetHolder, writeTableHolder, null, cell, head,
relativeRowIndex, true);
}
}
@ -354,21 +354,21 @@ public class WriteContextImpl implements WriteContext {
try {
Workbook workbook = writeWorkbookHolder.getWorkbook();
if (workbook instanceof SXSSFWorkbook) {
((SXSSFWorkbook)workbook).dispose();
((SXSSFWorkbook) workbook).dispose();
}
} catch (Throwable t) {
throwCanNotCloseIo(t);
}
try {
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) {
writeWorkbookHolder.getOutputStream().close();
if (writeWorkbookHolder.getTempTemplateInputStream() != null) {
writeWorkbookHolder.getTempTemplateInputStream().close();
}
} catch (Throwable t) {
throwCanNotCloseIo(t);
}
try {
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getTemplateInputStream() != null) {
writeWorkbookHolder.getTemplateInputStream().close();
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) {
writeWorkbookHolder.getOutputStream().close();
}
} catch (Throwable 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.Head;
import com.alibaba.excel.metadata.TableStyle;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.metadata.property.RowHeightProperty;
import com.alibaba.excel.util.CollectionUtils;
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.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.exception.ExcelGenerateException;
@ -60,6 +58,12 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
* If 'inputStream' and 'file' all not empty,file first
*/
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
*/
@ -182,6 +186,14 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
this.templateInputStream = templateInputStream;
}
public InputStream getTempTemplateInputStream() {
return tempTemplateInputStream;
}
public void setTempTemplateInputStream(InputStream tempTemplateInputStream) {
this.tempTemplateInputStream = tempTemplateInputStream;
}
public File getTemplateFile() {
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 {
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) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data));
}

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

@ -1,6 +1,7 @@
package com.alibaba.easyexcel.test.temp.poi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
@ -19,6 +20,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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
@ -57,7 +62,8 @@ public class PoiTest {
@Test
public void lastRowNum233() throws IOException {
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();
Sheet xssfSheet = xssfWorkbook.getXSSFWorkbook().getSheetAt(0);
@ -70,12 +76,39 @@ public class PoiTest {
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
public void lastRowNum2332222() throws IOException {
String file = TestFileUtil.getPath() + "fill" + File.separator + "simple.xlsx";
Workbook xx=new XSSFWorkbook(file);
SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook();
SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(new XSSFWorkbook(file));
Sheet xssfSheet = xssfWorkbook.getXSSFWorkbook().getSheetAt(0);
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.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import org.junit.Ignore;
@ -24,7 +26,18 @@ public class HgTest {
@Test
public void hh() throws IOException {
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) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data));
}

Loading…
Cancel
Save