Browse Source

Merge remote-tracking branch 'origin/2.1.0-beta1' into 2.1.0-beta1

bugfix
Jiaju Zhuang 5 years ago
parent
commit
ba1e407758
  1. 12
      src/main/java/com/alibaba/excel/ExcelWriter.java
  2. 13
      src/main/java/com/alibaba/excel/context/WriteContext.java
  3. 49
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  4. 9
      src/main/java/com/alibaba/excel/write/ExcelBuilder.java
  5. 22
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  6. 13
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java
  7. 13
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  8. 3
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

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

@ -138,7 +138,17 @@ public class ExcelWriter {
* @return this current writer * @return this current writer
*/ */
public ExcelWriter write(List data, WriteSheet writeSheet) { 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;
} }
/** /**

13
src/main/java/com/alibaba/excel/context/WriteContext.java

@ -69,6 +69,19 @@ public interface WriteContext {
*/ */
void finish(); void finish();
/**
* isEncrypt
* @return
*/
boolean isEncrypt();
/**
* setPassword
* @param password
* @return
*/
void setPassword(String password);
/** /**
* Current sheet * Current sheet
* *

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

@ -1,8 +1,16 @@
package com.alibaba.excel.context; package com.alibaba.excel.context;
import com.alibaba.excel.util.StringUtils;
import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Map; 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.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
@ -243,6 +251,12 @@ public class WriteContextImpl implements WriteContext {
} }
try { try {
writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream()); writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream());
if (isEncrypt()){
LOGGER.debug("start encrypt");
encrypt(writeWorkbookHolder);
LOGGER.debug("finish encrypt");
}
writeWorkbookHolder.getWorkbook().close(); writeWorkbookHolder.getWorkbook().close();
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(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) { private void throwCanNotCloseIo(Throwable t) {
throw new ExcelGenerateException("Can not close IO", t); throw new ExcelGenerateException("Can not close IO", t);
} }
@ -297,4 +321,29 @@ public class WriteContextImpl implements WriteContext {
public Workbook getWorkbook() { public Workbook getWorkbook() {
return writeWorkbookHolder.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();
}
}
} }

9
src/main/java/com/alibaba/excel/write/ExcelBuilder.java

@ -73,4 +73,13 @@ public interface ExcelBuilder {
* Close io * Close io
*/ */
void finish(); void finish();
/**
* add password
* @param data
* @param writeSheet
* @param writeTable
* @param password
*/
void addContent(List data, WriteSheet writeSheet, WriteTable writeTable, String password);
} }

22
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java

@ -95,6 +95,28 @@ public class ExcelBuilderImpl implements ExcelBuilder {
} }
} }
@Override
public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable, String password) {
try {
if (data == null) {
return;
}
context.currentSheet(writeSheet, WriteTypeEnum.ADD);
context.currentTable(writeTable);
context.setPassword(password);
if (excelWriteAddExecutor == null) {
excelWriteAddExecutor = new ExcelWriteAddExecutor(context);
}
excelWriteAddExecutor.add(data);
} catch (RuntimeException e) {
finish();
throw e;
} catch (Throwable e) {
finish();
throw new ExcelGenerateException(e);
}
}
@Override @Override
public void merge(int firstRow, int lastRow, int firstCol, int lastCol) { public void merge(int firstRow, int lastRow, int firstCol, int lastCol) {
CellRangeAddress cra = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol); CellRangeAddress cra = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);

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

@ -159,6 +159,19 @@ public class ExcelWriterSheetBuilder {
excelWriter.finish(); 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(), password);
excelWriter.finish();
}
public ExcelWriterTableBuilder table() { public ExcelWriterTableBuilder table() {
return table(null); return table(null);
} }

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

@ -97,6 +97,11 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
*/ */
private Map<Integer, WriteSheetHolder> hasBeenInitializedSheet; private Map<Integer, WriteSheetHolder> hasBeenInitializedSheet;
/**
* password
*/
private String password;
public WriteWorkbookHolder(WriteWorkbook writeWorkbook) { public WriteWorkbookHolder(WriteWorkbook writeWorkbook) {
super(writeWorkbook, null, writeWorkbook.getConvertAllFiled()); super(writeWorkbook, null, writeWorkbook.getConvertAllFiled());
this.writeWorkbook = writeWorkbook; this.writeWorkbook = writeWorkbook;
@ -256,4 +261,12 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
public HolderEnum holderType() { public HolderEnum holderType() {
return HolderEnum.WORKBOOK; return HolderEnum.WORKBOOK;
} }
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
} }

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

@ -57,7 +57,8 @@ public class WriteTest {
// 这里 需要指定写用哪个class去读 // 这里 需要指定写用哪个class去读
ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(data(), writeSheet); excelWriter.write(data(), writeSheet,"12345");
// 进行加密
/// 千万别忘记finish 会帮忙关闭流 /// 千万别忘记finish 会帮忙关闭流
excelWriter.finish(); excelWriter.finish();
} }

Loading…
Cancel
Save