Browse Source

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

developing
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.metadata.CellData;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.util.PositionUtils;
import com.alibaba.excel.util.StringUtils;
/** /**
* Cell Handler * Cell Handler
@ -18,6 +19,8 @@ import com.alibaba.excel.util.PositionUtils;
*/ */
public class CellTagHandler extends AbstractXlsxTagHandler { public class CellTagHandler extends AbstractXlsxTagHandler {
private static final int DEFAULT_FORMAT_INDEX = 0;
@Override @Override
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) {
XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
@ -36,15 +39,18 @@ public class CellTagHandler extends AbstractXlsxTagHandler {
// Put in data transformation information // Put in data transformation information
String dateFormatIndex = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_S); String dateFormatIndex = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_S);
if (dateFormatIndex != null) { Integer dateFormatIndexInteger;
int dateFormatIndexInteger = Integer.parseInt(dateFormatIndex); if (StringUtils.isEmpty(dateFormatIndex)) {
XSSFCellStyle xssfCellStyle = dateFormatIndexInteger = DEFAULT_FORMAT_INDEX;
xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable().getStyleAt(dateFormatIndexInteger); } else {
int dataFormat = xssfCellStyle.getDataFormat(); dateFormatIndexInteger = Integer.parseInt(dateFormatIndex);
xlsxReadSheetHolder.getTempCellData().setDataFormat(dataFormat);
xlsxReadSheetHolder.getTempCellData().setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat,
xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale()));
} }
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 // 21
"h:mm:ss", "h:mm:ss",
// 22 // 22
// The official documentation shows "m/d/yy h:mm", but the actual test is "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", "yyyy-m-d h:mm",
// 23-26 No specific correspondence found in the official documentation. // 23-26 No specific correspondence found in the official documentation.
// 23 // 23
null, null,
@ -236,8 +236,8 @@ public class BuiltinFormats {
// 21 // 21
"h:mm:ss", "h:mm:ss",
// 22 // 22
// The official documentation shows "m/d/yy h:mm", but the actual test is "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", "yyyy-m-d h:mm",
// 23-26 No specific correspondence found in the official documentation. // 23-26 No specific correspondence found in the official documentation.
// 23 // 23
null, 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.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.util.StringUtils;
/** /**
* @author jipengfei * @author jipengfei
@ -45,17 +46,29 @@ public enum ExcelTypeEnum {
if (!file.exists()) { if (!file.exists()) {
throw new ExcelAnalysisException("File " + file.getAbsolutePath() + " not 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(); String fileName = file.getName();
if (fileName.endsWith(XLSX.getValue())) { if (fileName.endsWith(XLSX.getValue())) {
return XLSX; return XLSX;
} else if (fileName.endsWith(XLS.getValue())) { } else if (fileName.endsWith(XLS.getValue())) {
return XLS; return XLS;
} }
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); if (StringUtils.isEmpty(readWorkbook.getPassword())) {
try { BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
return recognitionExcelType(bufferedInputStream); try {
} finally { return recognitionExcelType(bufferedInputStream);
bufferedInputStream.close(); } finally {
bufferedInputStream.close();
}
} }
} }
if (!inputStream.markSupported()) { 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); Assert.assertEquals(list.size(), 1);
ConverterData data = list.get(0); ConverterData data = list.get(0);
try { 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) { } catch (ParseException e) {
throw new ExcelCommonException("Test Exception", 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.getStringBoolean(), "true");
Assert.assertEquals(data.getStringString(), "测试"); Assert.assertEquals(data.getStringString(), "测试");
Assert.assertEquals(data.getStringError(), "#VALUE!"); 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(); double doubleStringFormulaNumber = new BigDecimal(data.getStringFormulaNumber()).doubleValue();
Assert.assertEquals(doubleStringFormulaNumber, 2.0, 0.0); Assert.assertEquals(doubleStringFormulaNumber, 2.0, 0.0);
Assert.assertEquals(data.getStringFormulaString(), "1测试"); 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.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
/** /**
* *
@ -45,7 +44,6 @@ public class DateFormatTest {
private void readCn(File file) { private void readCn(File file) {
List<DateFormatData> list = List<DateFormatData> list =
EasyExcel.read(file, DateFormatData.class, null).locale(Locale.CHINA).sheet().doReadSync(); EasyExcel.read(file, DateFormatData.class, null).locale(Locale.CHINA).sheet().doReadSync();
System.out.println(JSON.toJSONString(list));
for (DateFormatData data : list) { for (DateFormatData data : list) {
Assert.assertEquals(data.getDateStringCn(), data.getDate()); Assert.assertEquals(data.getDateStringCn(), data.getDate());
Assert.assertEquals(data.getNumberStringCn(), data.getNumber()); 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.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.core.simple.SimpleData; 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.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.FileUtils;
/** /**
* *
@ -51,12 +47,12 @@ public class EncryptDataTest {
} }
@Test @Test
public void t01ReadAndWriteStream07() throws Exception { public void t03ReadAndWriteStream07() throws Exception {
readAndWriteStream(file07OutputStream, ExcelTypeEnum.XLSX); readAndWriteStream(file07OutputStream, ExcelTypeEnum.XLSX);
} }
@Test @Test
public void t02ReadAndWriteStream03() throws Exception { public void t04ReadAndWriteStream03() throws Exception {
readAndWriteStream(file03OutputStream, ExcelTypeEnum.XLS); readAndWriteStream(file03OutputStream, ExcelTypeEnum.XLS);
} }
@ -72,8 +68,8 @@ public class EncryptDataTest {
fileOutputStream.close(); fileOutputStream.close();
FileInputStream fileInputStream = new FileInputStream(file); FileInputStream fileInputStream = new FileInputStream(file);
EasyExcel.read(fileInputStream, EncryptData.class, new EncryptDataListener()).password("123456") EasyExcel.read(fileInputStream, EncryptData.class, new EncryptDataListener()).password("123456").sheet()
.excelType(excelType).sheet().doRead(); .doRead();
} }
private List<SimpleData> data() { private List<SimpleData> data() {

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

@ -24,7 +24,7 @@ public class CommentTest {
@Test @Test
public void comment() throws Exception { 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(); List<Map<Integer, CellData>> datas = EasyExcel.read(file).doReadAllSync();
for (Map<Integer, CellData> data : datas) { for (Map<Integer, CellData> data : datas) {
LOGGER.info("数据:{}", JSON.toJSONString(data)); 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