change packge and add date format support

This commit is contained in:
Looly 2020-12-05 16:37:49 +08:00
parent 9ad2848bbd
commit 7103adc02e
11 changed files with 28 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.cell.FormulaCellValue;
import cn.hutool.poi.excel.sax.handler.RowHandler;
@ -410,21 +411,20 @@ public class Excel07SaxReader extends DefaultHandler implements ExcelSaxReader<E
*/
private void setCellType(Attributes attributes) {
// numFmtString的值
numFmtString = "";
numFmtString = StrUtil.EMPTY;
this.cellDataType = CellDataType.of(AttributeName.t.getValue(attributes));
// 获取单元格的xf索引对应style.xml中cellXfs的子元素xf
if (null != this.stylesTable) {
final String xfIndexStr = AttributeName.s.getValue(attributes);
if (null != xfIndexStr) {
int xfIndex = Integer.parseInt(xfIndexStr);
this.xssfCellStyle = stylesTable.getStyleAt(xfIndex);
numFmtString = xssfCellStyle.getDataFormatString();
this.xssfCellStyle = stylesTable.getStyleAt(Integer.parseInt(xfIndexStr));
// 单元格存储格式的索引对应style.xml中的numFmts元素的子元素索引
int numFmtIndex = xssfCellStyle.getDataFormat();
if (numFmtString == null) {
numFmtString = BuiltinFormats.getBuiltinFormat(numFmtIndex);
} else if (CellDataType.NUMBER == this.cellDataType && org.apache.poi.ss.usermodel.DateUtil.isADateFormat(numFmtIndex, numFmtString)) {
final int numFmtIndex = xssfCellStyle.getDataFormat();
this.numFmtString = ObjectUtil.defaultIfNull(
xssfCellStyle.getDataFormatString(),
BuiltinFormats.getBuiltinFormat(numFmtIndex));
if (CellDataType.NUMBER == this.cellDataType && ExcelSaxUtil.isDateFormat(numFmtIndex, numFmtString)) {
cellDataType = CellDataType.DATE;
}
}

View File

@ -183,6 +183,25 @@ public class ExcelSaxUtil {
public static boolean isDateFormat(CellValueRecordInterface cell, FormatTrackingHSSFListener formatListener){
final int formatIndex = formatListener.getFormatIndex(cell);
final String formatString = formatListener.getFormatString(cell);
return isDateFormat(formatIndex, formatString);
}
/**
* 判断日期格式
*
* @param formatIndex 格式索引一般用于内建格式
* @param formatString 格式字符串
* @return 是否为日期格式
* @since 5.5.3
*/
public static boolean isDateFormat(int formatIndex, String formatString){
// https://blog.csdn.net/u014342130/article/details/50619503
// issue#1283@Github
if(formatIndex == 28 || formatIndex == 31){
// 28 -> m月d日
// 31 -> yyyy年m月d日
return true;
}
return org.apache.poi.ss.usermodel.DateUtil.isADateFormat(formatIndex, formatString);
}

View File

@ -65,7 +65,7 @@ public class ExcelSaxReadTest {
@Test
@Ignore
public void readBySaxTest2() {
ExcelUtil.readBySax("e:/B23_20180404164901240.xlsx", 2, (sheetIndex, rowIndex, rowList) -> Console.log(rowList));
ExcelUtil.readBySax("d:/test/default.xlsx", -1, (sheetIndex, rowIndex, rowList) -> Console.log(rowList));
}
private RowHandler createRowHandler() {