Browse Source

修改原来的写法

bugfix
huaiyu 5 years ago
parent
commit
3b2c91594e
  1. 20
      src/main/java/com/alibaba/excel/ExcelWriter.java
  2. 9
      src/main/java/com/alibaba/excel/write/ExcelBuilder.java
  3. 21
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  4. 3
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java
  5. 5
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

20
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;
} }
/** /**
@ -331,12 +341,4 @@ public class ExcelWriter {
public WriteContext writeContext() { public WriteContext writeContext() {
return excelBuilder.writeContext(); return excelBuilder.writeContext();
} }
/**
* doEncrypt
* @param password
*/
public void doEncrypt(String password){
excelBuilder.setPassword(password);
}
} }

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);
} }

21
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 @Override
public void finish() { public void finish() {
if (context != null) { 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 @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);

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

@ -157,8 +157,7 @@ public class ExcelWriterSheetBuilder {
if (excelWriter == null) { if (excelWriter == null) {
throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet()' to call this method"); throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet()' to call this method");
} }
excelWriter.write(data, build()); excelWriter.write(data, build(), password);
excelWriter.doEncrypt(password);
excelWriter.finish(); excelWriter.finish();
} }

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

@ -57,9 +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");
// doEncrypt 进行加密 // 进行加密
//excelWriter.doEncrypt("123456");
/// 千万别忘记finish 会帮忙关闭流 /// 千万别忘记finish 会帮忙关闭流
excelWriter.finish(); excelWriter.finish();
} }

Loading…
Cancel
Save