From 2fb107577b15f6900c2a84b22a12e229c3cec693 Mon Sep 17 00:00:00 2001 From: huaiyu Date: Wed, 25 Sep 2019 13:27:02 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=86=99=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=8A=A0=E5=AF=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/alibaba/excel/ExcelWriter.java | 8 +++ .../alibaba/excel/context/WriteContext.java | 13 +++++ .../excel/context/WriteContextImpl.java | 49 +++++++++++++++++++ .../alibaba/excel/write/ExcelBuilderImpl.java | 5 ++ .../builder/ExcelWriterSheetBuilder.java | 14 ++++++ .../metadata/holder/WriteWorkbookHolder.java | 5 ++ .../easyexcel/test/demo/write/WriteTest.java | 2 + 7 files changed, 96 insertions(+) diff --git a/src/main/java/com/alibaba/excel/ExcelWriter.java b/src/main/java/com/alibaba/excel/ExcelWriter.java index 65a99fe5..3a2dde55 100644 --- a/src/main/java/com/alibaba/excel/ExcelWriter.java +++ b/src/main/java/com/alibaba/excel/ExcelWriter.java @@ -331,4 +331,12 @@ public class ExcelWriter { public WriteContext writeContext() { return excelBuilder.writeContext(); } + + /** + * doEncrypt + * @param password + */ + public void doEncrypt(String password){ + excelBuilder.setPassword(password); + } } diff --git a/src/main/java/com/alibaba/excel/context/WriteContext.java b/src/main/java/com/alibaba/excel/context/WriteContext.java index e5613196..a5e6d585 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContext.java +++ b/src/main/java/com/alibaba/excel/context/WriteContext.java @@ -69,6 +69,19 @@ public interface WriteContext { */ void finish(); + /** + * isEncrypt + * @return + */ + boolean isEncrypt(); + + /** + * setPassword + * @param password + * @return + */ + void setPassword(String password); + /** * Current sheet * diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index c71d08d7..8972424b 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -1,8 +1,16 @@ package com.alibaba.excel.context; +import com.alibaba.excel.util.StringUtils; +import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Map; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.openxml4j.opc.PackageAccess; +import org.apache.poi.poifs.crypt.EncryptionInfo; +import org.apache.poi.poifs.crypt.EncryptionMode; +import org.apache.poi.poifs.crypt.Encryptor; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; @@ -243,6 +251,12 @@ public class WriteContextImpl implements WriteContext { } try { writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream()); + if (isEncrypt()){ + LOGGER.debug("start encrypt"); + encrypt(writeWorkbookHolder); + LOGGER.debug("finish encrypt"); + + } writeWorkbookHolder.getWorkbook().close(); } catch (Throwable t) { throwCanNotCloseIo(t); @@ -274,6 +288,16 @@ public class WriteContextImpl implements WriteContext { } } + @Override + public boolean isEncrypt() { + return !StringUtils.isEmpty(writeWorkbookHolder.getPassword()); + } + + @Override + public void setPassword(String password) { + writeWorkbookHolder.setPassword(password); + } + private void throwCanNotCloseIo(Throwable t) { throw new ExcelGenerateException("Can not close IO", t); } @@ -297,4 +321,29 @@ public class WriteContextImpl implements WriteContext { public Workbook getWorkbook() { return writeWorkbookHolder.getWorkbook(); } + + /** + * do encrypt + * @param writeWorkbookHolder + */ + private void encrypt(WriteWorkbookHolder writeWorkbookHolder){ + try { + POIFSFileSystem fs = new POIFSFileSystem(); + EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard); + + Encryptor enc = info.getEncryptor(); + enc.confirmPassword(writeWorkbookHolder.getPassword()); + + OPCPackage opc = OPCPackage.open(writeWorkbookHolder.getFile(), PackageAccess.READ_WRITE); + OutputStream os = enc.getDataStream(fs); + opc.save(os); + opc.close(); + //write the encrypted file back to the stream + FileOutputStream fos = new FileOutputStream(writeWorkbookHolder.getFile()); + fs.writeFilesystem(fos); + fos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index 3d8067a8..27f65905 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -88,6 +88,11 @@ public class ExcelBuilderImpl implements ExcelBuilder { } } + @Override + public void setPassword(String password) { + context.setPassword(password); + } + @Override public void finish() { if (context != null) { diff --git a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java index 8a9e0c66..893d2180 100644 --- a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java +++ b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java @@ -148,6 +148,20 @@ public class ExcelWriterSheetBuilder { excelWriter.finish(); } + /** + * write with password + * @param data + * @param password + */ + public void doWrite(List data,String password) { + if (excelWriter == null) { + throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet()' to call this method"); + } + excelWriter.write(data, build()); + excelWriter.doEncrypt(password); + excelWriter.finish(); + } + public ExcelWriterTableBuilder table() { return table(null); } diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java index ec364fb9..931a4502 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java @@ -97,6 +97,11 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { */ private Map hasBeenInitializedSheet; + /** + * password + */ + private String password; + public WriteWorkbookHolder(WriteWorkbook writeWorkbook) { super(writeWorkbook, null, writeWorkbook.getConvertAllFiled()); this.writeWorkbook = writeWorkbook; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index 76c3c716..a86f4e52 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -58,6 +58,8 @@ public class WriteTest { ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); excelWriter.write(data(), writeSheet); + // doEncrypt 进行加密 + //excelWriter.doEncrypt("123456"); /// 千万别忘记finish 会帮忙关闭流 excelWriter.finish(); } From 3b2c91594e2d2a2e41113d9a079d0f5d9ea74b30 Mon Sep 17 00:00:00 2001 From: huaiyu Date: Thu, 10 Oct 2019 15:11:36 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8E=9F=E6=9D=A5?= =?UTF-8?q?=E7=9A=84=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/alibaba/excel/ExcelWriter.java | 20 ++++++++++-------- .../com/alibaba/excel/write/ExcelBuilder.java | 9 ++++++++ .../alibaba/excel/write/ExcelBuilderImpl.java | 21 ++++++++++++++----- .../builder/ExcelWriterSheetBuilder.java | 3 +-- .../easyexcel/test/demo/write/WriteTest.java | 5 ++--- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/alibaba/excel/ExcelWriter.java b/src/main/java/com/alibaba/excel/ExcelWriter.java index 3a2dde55..4800f840 100644 --- a/src/main/java/com/alibaba/excel/ExcelWriter.java +++ b/src/main/java/com/alibaba/excel/ExcelWriter.java @@ -138,7 +138,17 @@ public class ExcelWriter { * @return this current writer */ public ExcelWriter write(List data, WriteSheet writeSheet) { - return write(data, writeSheet, null); + return write(data, writeSheet, (WriteTable)null); + } + + + public ExcelWriter write(List data, WriteSheet writeSheet, String password) { + return write(data, writeSheet, null, password); + } + + private ExcelWriter write(List data, WriteSheet writeSheet, WriteTable writeTable, String password) { + excelBuilder.addContent(data, writeSheet, writeTable, password); + return this; } /** @@ -331,12 +341,4 @@ public class ExcelWriter { public WriteContext writeContext() { return excelBuilder.writeContext(); } - - /** - * doEncrypt - * @param password - */ - public void doEncrypt(String password){ - excelBuilder.setPassword(password); - } } diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilder.java b/src/main/java/com/alibaba/excel/write/ExcelBuilder.java index 8a60444e..8a7d57da 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilder.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilder.java @@ -73,4 +73,13 @@ public interface ExcelBuilder { * Close io */ void finish(); + + /** + * add password + * @param data + * @param writeSheet + * @param writeTable + * @param password + */ + void addContent(List data, WriteSheet writeSheet, WriteTable writeTable, String password); } diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index 27f65905..19284475 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -88,11 +88,6 @@ public class ExcelBuilderImpl implements ExcelBuilder { } } - @Override - public void setPassword(String password) { - context.setPassword(password); - } - @Override public void finish() { if (context != null) { @@ -100,6 +95,22 @@ public class ExcelBuilderImpl implements ExcelBuilder { } } + @Override + public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable, String password) { + try { + context.currentSheet(writeSheet); + context.currentTable(writeTable); + context.setPassword(password); + doAddContent(data); + } catch (RuntimeException e) { + finish(); + throw e; + } catch (Throwable e) { + finish(); + throw new ExcelGenerateException(e); + } + } + @Override public void merge(int firstRow, int lastRow, int firstCol, int lastCol) { CellRangeAddress cra = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol); diff --git a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java index 893d2180..194fedb0 100644 --- a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java +++ b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java @@ -157,8 +157,7 @@ public class ExcelWriterSheetBuilder { if (excelWriter == null) { throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet()' to call this method"); } - excelWriter.write(data, build()); - excelWriter.doEncrypt(password); + excelWriter.write(data, build(), password); excelWriter.finish(); } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index a86f4e52..b2a1722c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -57,9 +57,8 @@ public class WriteTest { // 这里 需要指定写用哪个class去读 ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); - excelWriter.write(data(), writeSheet); - // doEncrypt 进行加密 - //excelWriter.doEncrypt("123456"); + excelWriter.write(data(), writeSheet,"12345"); + // 进行加密 /// 千万别忘记finish 会帮忙关闭流 excelWriter.finish(); } From 4f9bcc70739ec0f4190841ecd751429e15b18881 Mon Sep 17 00:00:00 2001 From: huaiyu Date: Thu, 10 Oct 2019 15:20:26 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=90=88=E5=B9=B6=E5=86=B2=E7=AA=81=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8E=9F=E6=9D=A5=E7=9A=84=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/alibaba/excel/write/ExcelBuilderImpl.java | 10 ++++++++-- .../write/metadata/holder/WriteWorkbookHolder.java | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index 19284475..c1a00c23 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -98,10 +98,16 @@ public class ExcelBuilderImpl implements ExcelBuilder { @Override public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable, String password) { try { - context.currentSheet(writeSheet); + if (data == null) { + return; + } + context.currentSheet(writeSheet, WriteTypeEnum.ADD); context.currentTable(writeTable); context.setPassword(password); - doAddContent(data); + if (excelWriteAddExecutor == null) { + excelWriteAddExecutor = new ExcelWriteAddExecutor(context); + } + excelWriteAddExecutor.add(data); } catch (RuntimeException e) { finish(); throw e; diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java index 931a4502..65559db0 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java @@ -261,4 +261,12 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { public HolderEnum holderType() { return HolderEnum.WORKBOOK; } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } }