Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Commit 2c8918b

Browse files
committed
修复填充的时候有数字会异常
1 parent 60f7ab1 commit 2c8918b

26 files changed

Lines changed: 450 additions & 63 deletions

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.alibaba</groupId>
66
<artifactId>easyexcel</artifactId>
7-
<version>2.1.0-beta2</version>
7+
<version>2.1.0-beta3</version>
88
<packaging>jar</packaging>
99
<name>easyexcel</name>
1010

‎src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,44 +129,50 @@ public void finish() {
129129
}
130130
ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder();
131131

132+
Throwable throwable = null;
133+
132134
try {
133135
if (readWorkbookHolder.getReadCache() != null) {
134136
readWorkbookHolder.getReadCache().destroy();
135137
}
136138
} catch (Throwable t) {
137-
throwCanNotCloseIo(t);
139+
throwable = t;
138140
}
139141
try {
140142
if (readWorkbookHolder.getOpcPackage() != null) {
141143
readWorkbookHolder.getOpcPackage().revert();
142144
}
143145
} catch (Throwable t) {
144-
throwCanNotCloseIo(t);
146+
throwable = t;
145147
}
146148
try {
147149
if (readWorkbookHolder.getPoifsFileSystem() != null) {
148150
readWorkbookHolder.getPoifsFileSystem().close();
149151
}
150152
} catch (Throwable t) {
151-
throwCanNotCloseIo(t);
153+
throwable = t;
152154
}
153155
try {
154156
if (analysisContext.readWorkbookHolder().getAutoCloseStream()
155157
&& readWorkbookHolder.getInputStream() != null) {
156158
readWorkbookHolder.getInputStream().close();
157159
}
158160
} catch (Throwable t) {
159-
throwCanNotCloseIo(t);
161+
throwable = t;
160162
}
161163
try {
162164
if (readWorkbookHolder.getTempFile() != null) {
163165
FileUtils.delete(readWorkbookHolder.getTempFile());
164166
}
165167
} catch (Throwable t) {
166-
throwCanNotCloseIo(t);
168+
throwable = t;
167169
}
168170

169171
clearEncrypt03();
172+
173+
if (throwable != null) {
174+
throw new ExcelAnalysisException("Can not close IO", throwable);
175+
}
170176
}
171177

172178
private void clearEncrypt03() {
@@ -177,10 +183,6 @@ private void clearEncrypt03() {
177183
Biff8EncryptionKey.setCurrentUserPassword(null);
178184
}
179185

180-
private void throwCanNotCloseIo(Throwable t) {
181-
throw new ExcelAnalysisException("Can not close IO", t);
182-
}
183-
184186
@Override
185187
public ExcelReadExecutor excelExecutor() {
186188
return excelReadExecutor;

‎src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.apache.poi.openxml4j.opc.OPCPackage;
1616
import org.apache.poi.openxml4j.opc.PackageAccess;
1717
import org.apache.poi.openxml4j.opc.PackagePart;
18-
import org.apache.poi.ss.usermodel.Sheet;
1918
import org.apache.poi.xssf.eventusermodel.XSSFReader;
2019
import org.apache.poi.xssf.model.StylesTable;
2120
import org.apache.poi.xssf.usermodel.XSSFRelation;
@@ -35,7 +34,6 @@
3534
import com.alibaba.excel.util.CollectionUtils;
3635
import com.alibaba.excel.util.FileUtils;
3736
import com.alibaba.excel.util.SheetUtils;
38-
import com.alibaba.excel.util.StringUtils;
3937

4038
/**
4139
*

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public void initHead(ExcelWriteHeadProperty excelWriteHeadProperty) {
179179
Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i);
180180
WriteHandlerUtils.afterRowCreate(this, row, relativeRowIndex, Boolean.TRUE);
181181
addOneRowOfHeadDataToExcel(row, excelWriteHeadProperty.getHeadMap(), relativeRowIndex);
182+
WriteHandlerUtils.afterRowDispose(this, row, relativeRowIndex, Boolean.FALSE);
182183
}
183184
}
184185

@@ -197,8 +198,7 @@ private void addOneRowOfHeadDataToExcel(Row row, Map<Integer, Head> headMap, int
197198
Cell cell = row.createCell(columnIndex);
198199
WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE);
199200
cell.setCellValue(head.getHeadNameList().get(relativeRowIndex));
200-
CellData cellData = null;
201-
WriteHandlerUtils.afterCellDispose(this, cellData, cell, head, relativeRowIndex, Boolean.TRUE);
201+
WriteHandlerUtils.afterCellDispose(this, (CellData)null, cell, head, relativeRowIndex, Boolean.TRUE);
202202
}
203203
}
204204

@@ -261,20 +261,21 @@ public void finish() {
261261
if (writeWorkbookHolder == null) {
262262
return;
263263
}
264+
Throwable throwable = null;
264265

265266
boolean isOutputStreamEncrypt = false;
266267
try {
267268
isOutputStreamEncrypt = doOutputStreamEncrypt07();
268269
} catch (Throwable t) {
269-
throwCanNotCloseIo(t);
270+
throwable = t;
270271
}
271272

272273
if (!isOutputStreamEncrypt) {
273274
try {
274275
writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream());
275276
writeWorkbookHolder.getWorkbook().close();
276277
} catch (Throwable t) {
277-
throwCanNotCloseIo(t);
278+
throwable = t;
278279
}
279280
}
280281

@@ -284,22 +285,22 @@ public void finish() {
284285
((SXSSFWorkbook)workbook).dispose();
285286
}
286287
} catch (Throwable t) {
287-
throwCanNotCloseIo(t);
288+
throwable = t;
288289
}
289290

290291
try {
291292
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) {
292293
writeWorkbookHolder.getOutputStream().close();
293294
}
294295
} catch (Throwable t) {
295-
throwCanNotCloseIo(t);
296+
throwable = t;
296297
}
297298

298299
if (!isOutputStreamEncrypt) {
299300
try {
300301
doFileEncrypt07();
301302
} catch (Throwable t) {
302-
throwCanNotCloseIo(t);
303+
throwable = t;
303304
}
304305
}
305306

@@ -308,20 +309,20 @@ public void finish() {
308309
writeWorkbookHolder.getTempTemplateInputStream().close();
309310
}
310311
} catch (Throwable t) {
311-
throwCanNotCloseIo(t);
312+
throwable = t;
312313
}
313314

314315
clearEncrypt03();
315316

317+
if (throwable != null) {
318+
throw new ExcelGenerateException("Can not close IO", throwable);
319+
}
320+
316321
if (LOGGER.isDebugEnabled()) {
317322
LOGGER.debug("Finished write.");
318323
}
319324
}
320325

321-
private void throwCanNotCloseIo(Throwable t) {
322-
throw new ExcelGenerateException("Can not close IO", t);
323-
}
324-
325326
@Override
326327
public Sheet getCurrentSheet() {
327328
return writeSheetHolder.getSheet();

‎src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
import com.alibaba.excel.annotation.ExcelProperty;
1818
import com.alibaba.excel.annotation.format.DateTimeFormat;
1919
import com.alibaba.excel.annotation.format.NumberFormat;
20-
import com.alibaba.excel.annotation.write.style.ColumnWidth;
2120
import com.alibaba.excel.converters.AutoConverter;
2221
import com.alibaba.excel.converters.Converter;
2322
import com.alibaba.excel.enums.HeadKindEnum;
2423
import com.alibaba.excel.exception.ExcelCommonException;
2524
import com.alibaba.excel.exception.ExcelGenerateException;
2625
import com.alibaba.excel.metadata.Head;
26+
import com.alibaba.excel.metadata.Holder;
2727
import com.alibaba.excel.util.StringUtils;
28+
import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder;
2829

2930
/**
3031
* Define the header attribute of excel
@@ -46,7 +47,6 @@ public class ExcelHeadProperty {
4647
* The number of rows in the line with the most rows
4748
*/
4849
private int headRowNumber;
49-
5050
/**
5151
* Configuration header information
5252
*/
@@ -64,7 +64,7 @@ public class ExcelHeadProperty {
6464
*/
6565
private Map<String, Field> ignoreMap;
6666

67-
public ExcelHeadProperty(Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
67+
public ExcelHeadProperty(Holder holder, Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
6868
this.headClazz = headClazz;
6969
headMap = new TreeMap<Integer, Head>();
7070
contentPropertyMap = new TreeMap<Integer, ExcelContentProperty>();
@@ -73,14 +73,21 @@ public ExcelHeadProperty(Class headClazz, List<List<String>> head, Boolean conve
7373
headKind = HeadKindEnum.NONE;
7474
headRowNumber = 0;
7575
if (head != null && !head.isEmpty()) {
76+
int headIndex = 0;
7677
for (int i = 0; i < head.size(); i++) {
77-
headMap.put(i, new Head(i, null, head.get(i), Boolean.FALSE, Boolean.TRUE));
78-
contentPropertyMap.put(i, null);
78+
if (holder instanceof AbstractWriteHolder) {
79+
if (((AbstractWriteHolder)holder).ignore(null, i)) {
80+
continue;
81+
}
82+
}
83+
headMap.put(headIndex, new Head(headIndex, null, head.get(i), Boolean.FALSE, Boolean.TRUE));
84+
contentPropertyMap.put(headIndex, null);
85+
headIndex++;
7986
}
8087
headKind = HeadKindEnum.STRING;
8188
} else {
8289
// convert headClazz to head
83-
initColumnProperties(convertAllFiled);
90+
initColumnProperties(holder, convertAllFiled);
8491
}
8592
initHeadRowNumber();
8693
if (LOGGER.isDebugEnabled()) {
@@ -108,7 +115,7 @@ private void initHeadRowNumber() {
108115
}
109116
}
110117

111-
private void initColumnProperties(Boolean convertAllFiled) {
118+
private void initColumnProperties(Holder holder, Boolean convertAllFiled) {
112119
if (headClazz == null) {
113120
return;
114121
}
@@ -161,20 +168,36 @@ private void initColumnProperties(Boolean convertAllFiled) {
161168
int index = 0;
162169
for (Field field : defaultFieldList) {
163170
while (customFiledMap.containsKey(index)) {
164-
initOneColumnProperty(index, customFiledMap.get(index), Boolean.TRUE);
165171
customFiledMap.remove(index);
172+
if (!initOneColumnProperty(holder, index, customFiledMap.get(index), Boolean.TRUE)) {
173+
index++;
174+
}
175+
}
176+
if (!initOneColumnProperty(holder, index, field, Boolean.FALSE)) {
166177
index++;
167178
}
168-
initOneColumnProperty(index, field, Boolean.FALSE);
169-
index++;
170179
}
171180
for (Map.Entry<Integer, Field> entry : customFiledMap.entrySet()) {
172-
initOneColumnProperty(entry.getKey(), entry.getValue(), Boolean.TRUE);
181+
initOneColumnProperty(holder, entry.getKey(), entry.getValue(), Boolean.TRUE);
173182
}
174183
headKind = HeadKindEnum.CLASS;
175184
}
176185

177-
private void initOneColumnProperty(int index, Field field, Boolean forceIndex) {
186+
/**
187+
* Initialization column property
188+
*
189+
* @param holder
190+
* @param index
191+
* @param field
192+
* @param forceIndex
193+
* @return Ignore current field
194+
*/
195+
private boolean initOneColumnProperty(Holder holder, int index, Field field, Boolean forceIndex) {
196+
if (holder instanceof AbstractWriteHolder) {
197+
if (((AbstractWriteHolder)holder).ignore(field.getName(), index)) {
198+
return true;
199+
}
200+
}
178201
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
179202
List<String> tmpHeadList = new ArrayList<String>();
180203
boolean notForceName = excelProperty == null || excelProperty.value().length <= 0
@@ -206,6 +229,7 @@ private void initOneColumnProperty(int index, Field field, Boolean forceIndex) {
206229
headMap.put(index, head);
207230
contentPropertyMap.put(index, excelContentProperty);
208231
fieldNameContentPropertyMap.put(field.getName(), excelContentProperty);
232+
return false;
209233
}
210234

211235
public Class getHeadClazz() {

‎src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public AbstractReadHolder(ReadBasicParameter readBasicParameter, AbstractReadHol
7070
}
7171

7272
// Initialization property
73-
this.excelReadHeadProperty = new ExcelReadHeadProperty(getClazz(), getHead(), convertAllFiled);
73+
this.excelReadHeadProperty = new ExcelReadHeadProperty(this, getClazz(), getHead(), convertAllFiled);
7474
if (readBasicParameter.getHeadRowNumber() == null) {
7575
if (parentAbstractReadHolder == null) {
7676
if (excelReadHeadProperty.hasHead()) {

‎src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44

5+
import com.alibaba.excel.metadata.Holder;
56
import com.alibaba.excel.metadata.property.ExcelHeadProperty;
67

78
/**
@@ -11,7 +12,7 @@
1112
*/
1213
public class ExcelReadHeadProperty extends ExcelHeadProperty {
1314

14-
public ExcelReadHeadProperty(Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
15-
super(headClazz, head, convertAllFiled);
15+
public ExcelReadHeadProperty(Holder holder, Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
16+
super(holder, headClazz, head, convertAllFiled);
1617
}
1718
}

‎src/main/java/com/alibaba/excel/util/WorkBookUtil.java‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,21 @@ public static void createWorkBook(WriteWorkbookHolder writeWorkbookHolder) throw
3131
if (writeWorkbookHolder.getTempTemplateInputStream() != null) {
3232
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(writeWorkbookHolder.getTempTemplateInputStream());
3333
writeWorkbookHolder.setCachedWorkbook(xssfWorkbook);
34-
writeWorkbookHolder.setWorkbook(new SXSSFWorkbook(xssfWorkbook, ROW_ACCESS_WINDOW_SIZE));
34+
if (writeWorkbookHolder.getInMemory()) {
35+
writeWorkbookHolder.setWorkbook(xssfWorkbook);
36+
} else {
37+
writeWorkbookHolder.setWorkbook(new SXSSFWorkbook(xssfWorkbook, ROW_ACCESS_WINDOW_SIZE));
38+
}
3539
return;
3640
}
37-
SXSSFWorkbook sxssWorkbook = new SXSSFWorkbook(ROW_ACCESS_WINDOW_SIZE);
38-
writeWorkbookHolder.setCachedWorkbook(sxssWorkbook);
39-
writeWorkbookHolder.setWorkbook(sxssWorkbook);
41+
Workbook workbook = null;
42+
if (writeWorkbookHolder.getInMemory()) {
43+
workbook = new XSSFWorkbook();
44+
} else {
45+
workbook = new SXSSFWorkbook(ROW_ACCESS_WINDOW_SIZE);
46+
}
47+
writeWorkbookHolder.setCachedWorkbook(workbook);
48+
writeWorkbookHolder.setWorkbook(workbook);
4049
return;
4150
}
4251
HSSFWorkbook hssfWorkbook;

0 commit comments

Comments
 (0)