Browse Source

重写主流程,代码更加优雅 测试完毕

2.1.x
Jiaju Zhuang 5 years ago
parent
commit
26c6de095b
  1. 22
      src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java
  2. 8
      src/main/java/com/alibaba/excel/constant/BuiltinFormats.java
  3. 23
      src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java
  4. 2
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java
  5. 2
      src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java
  6. 2
      src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java
  7. 12
      src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java
  8. 2
      src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java
  9. BIN
      src/test/resources/dataformat/dataformat.xls
  10. BIN
      src/test/resources/dataformat/dataformat.xlsx

22
src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java

@ -10,6 +10,7 @@ import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
import com.alibaba.excel.util.PositionUtils;
import com.alibaba.excel.util.StringUtils;
/**
* Cell Handler
@ -18,6 +19,8 @@ import com.alibaba.excel.util.PositionUtils;
*/
public class CellTagHandler extends AbstractXlsxTagHandler {
private static final int DEFAULT_FORMAT_INDEX = 0;
@Override
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) {
XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
@ -36,15 +39,18 @@ public class CellTagHandler extends AbstractXlsxTagHandler {
// Put in data transformation information
String dateFormatIndex = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_S);
if (dateFormatIndex != null) {
int dateFormatIndexInteger = Integer.parseInt(dateFormatIndex);
XSSFCellStyle xssfCellStyle =
xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable().getStyleAt(dateFormatIndexInteger);
int dataFormat = xssfCellStyle.getDataFormat();
xlsxReadSheetHolder.getTempCellData().setDataFormat(dataFormat);
xlsxReadSheetHolder.getTempCellData().setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat,
xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale()));
Integer dateFormatIndexInteger;
if (StringUtils.isEmpty(dateFormatIndex)) {
dateFormatIndexInteger = DEFAULT_FORMAT_INDEX;
} else {
dateFormatIndexInteger = Integer.parseInt(dateFormatIndex);
}
XSSFCellStyle xssfCellStyle =
xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable().getStyleAt(dateFormatIndexInteger);
int dataFormat = xssfCellStyle.getDataFormat();
xlsxReadSheetHolder.getTempCellData().setDataFormat(dataFormat);
xlsxReadSheetHolder.getTempCellData().setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat,
xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale()));
}
}

8
src/main/java/com/alibaba/excel/constant/BuiltinFormats.java

@ -64,8 +64,8 @@ public class BuiltinFormats {
// 21
"h:mm:ss",
// 22
// The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy/m/d h:mm".
"yyyy/m/d h:mm",
// The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy-m-d h:mm".
"yyyy-m-d h:mm",
// 23-26 No specific correspondence found in the official documentation.
// 23
null,
@ -236,8 +236,8 @@ public class BuiltinFormats {
// 21
"h:mm:ss",
// 22
// The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy/m/d h:mm".
"yyyy/m/d h:mm",
// The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy-m-d h:mm".
"yyyy-m-d h:mm",
// 23-26 No specific correspondence found in the official documentation.
// 23
null,

23
src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java

@ -10,6 +10,7 @@ import org.apache.poi.poifs.filesystem.FileMagic;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.util.StringUtils;
/**
* @author jipengfei
@ -45,17 +46,29 @@ public enum ExcelTypeEnum {
if (!file.exists()) {
throw new ExcelAnalysisException("File " + file.getAbsolutePath() + " not exists.");
}
// If there is a password, use the FileMagic first
if (!StringUtils.isEmpty(readWorkbook.getPassword())) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
try {
return recognitionExcelType(bufferedInputStream);
} finally {
bufferedInputStream.close();
}
}
// Use the name to determine the type
String fileName = file.getName();
if (fileName.endsWith(XLSX.getValue())) {
return XLSX;
} else if (fileName.endsWith(XLS.getValue())) {
return XLS;
}
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
try {
return recognitionExcelType(bufferedInputStream);
} finally {
bufferedInputStream.close();
if (StringUtils.isEmpty(readWorkbook.getPassword())) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
try {
return recognitionExcelType(bufferedInputStream);
} finally {
bufferedInputStream.close();
}
}
}
if (!inputStream.markSupported()) {

2
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java

@ -32,7 +32,7 @@ public class ConverterDataListener extends AnalysisEventListener<ConverterData>
Assert.assertEquals(list.size(), 1);
ConverterData data = list.get(0);
try {
Assert.assertEquals(data.getDate(), DateUtils.parseDate("2020-01-01 01:01:01"));
Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01"), data.getDate());
} catch (ParseException e) {
throw new ExcelCommonException("Test Exception", e);
}

2
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java

@ -64,7 +64,7 @@ public class ReadAllConverterDataListener extends AnalysisEventListener<ReadAllC
Assert.assertEquals(data.getStringBoolean(), "true");
Assert.assertEquals(data.getStringString(), "测试");
Assert.assertEquals(data.getStringError(), "#VALUE!");
Assert.assertEquals(data.getStringNumberDate(), "2020-01-01 01:01:01");
Assert.assertEquals("2020-1-1 1:01", data.getStringNumberDate());
double doubleStringFormulaNumber = new BigDecimal(data.getStringFormulaNumber()).doubleValue();
Assert.assertEquals(doubleStringFormulaNumber, 2.0, 0.0);
Assert.assertEquals(data.getStringFormulaString(), "1测试");

2
src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java

@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
/**
*
@ -45,7 +44,6 @@ public class DateFormatTest {
private void readCn(File file) {
List<DateFormatData> list =
EasyExcel.read(file, DateFormatData.class, null).locale(Locale.CHINA).sheet().doReadSync();
System.out.println(JSON.toJSONString(list));
for (DateFormatData data : list) {
Assert.assertEquals(data.getDateStringCn(), data.getDate());
Assert.assertEquals(data.getNumberStringCn(), data.getNumber());

12
src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java

@ -6,19 +6,15 @@ import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.core.simple.SimpleData;
import com.alibaba.easyexcel.test.core.simple.SimpleDataListener;
import com.alibaba.easyexcel.test.core.simple.SimpleDataSheetNameListener;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.FileUtils;
/**
*
@ -51,12 +47,12 @@ public class EncryptDataTest {
}
@Test
public void t01ReadAndWriteStream07() throws Exception {
public void t03ReadAndWriteStream07() throws Exception {
readAndWriteStream(file07OutputStream, ExcelTypeEnum.XLSX);
}
@Test
public void t02ReadAndWriteStream03() throws Exception {
public void t04ReadAndWriteStream03() throws Exception {
readAndWriteStream(file03OutputStream, ExcelTypeEnum.XLS);
}
@ -72,8 +68,8 @@ public class EncryptDataTest {
fileOutputStream.close();
FileInputStream fileInputStream = new FileInputStream(file);
EasyExcel.read(fileInputStream, EncryptData.class, new EncryptDataListener()).password("123456")
.excelType(excelType).sheet().doRead();
EasyExcel.read(fileInputStream, EncryptData.class, new EncryptDataListener()).password("123456").sheet()
.doRead();
}
private List<SimpleData> data() {

2
src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java

@ -24,7 +24,7 @@ public class CommentTest {
@Test
public void comment() throws Exception {
File file = new File("D:\\test\\listHead07.xlsx");
File file = new File("D:\\test\\d1.xlsx");
List<Map<Integer, CellData>> datas = EasyExcel.read(file).doReadAllSync();
for (Map<Integer, CellData> data : datas) {
LOGGER.info("数据:{}", JSON.toJSONString(data));

BIN
src/test/resources/dataformat/dataformat.xls

Binary file not shown.

BIN
src/test/resources/dataformat/dataformat.xlsx

Binary file not shown.
Loading…
Cancel
Save