mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-03 12:18:01 +08:00
change packge and add date format support
This commit is contained in:
parent
9ad2848bbd
commit
7103adc02e
@ -4,6 +4,7 @@ import cn.hutool.core.io.IORuntimeException;
|
|||||||
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.text.StrBuilder;
|
import cn.hutool.core.text.StrBuilder;
|
||||||
import cn.hutool.core.util.NumberUtil;
|
import cn.hutool.core.util.NumberUtil;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.poi.excel.cell.FormulaCellValue;
|
import cn.hutool.poi.excel.cell.FormulaCellValue;
|
||||||
import cn.hutool.poi.excel.sax.handler.RowHandler;
|
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) {
|
private void setCellType(Attributes attributes) {
|
||||||
// numFmtString的值
|
// numFmtString的值
|
||||||
numFmtString = "";
|
numFmtString = StrUtil.EMPTY;
|
||||||
this.cellDataType = CellDataType.of(AttributeName.t.getValue(attributes));
|
this.cellDataType = CellDataType.of(AttributeName.t.getValue(attributes));
|
||||||
|
|
||||||
// 获取单元格的xf索引,对应style.xml中cellXfs的子元素xf
|
// 获取单元格的xf索引,对应style.xml中cellXfs的子元素xf
|
||||||
if (null != this.stylesTable) {
|
if (null != this.stylesTable) {
|
||||||
final String xfIndexStr = AttributeName.s.getValue(attributes);
|
final String xfIndexStr = AttributeName.s.getValue(attributes);
|
||||||
if (null != xfIndexStr) {
|
if (null != xfIndexStr) {
|
||||||
int xfIndex = Integer.parseInt(xfIndexStr);
|
this.xssfCellStyle = stylesTable.getStyleAt(Integer.parseInt(xfIndexStr));
|
||||||
this.xssfCellStyle = stylesTable.getStyleAt(xfIndex);
|
|
||||||
numFmtString = xssfCellStyle.getDataFormatString();
|
|
||||||
// 单元格存储格式的索引,对应style.xml中的numFmts元素的子元素索引
|
// 单元格存储格式的索引,对应style.xml中的numFmts元素的子元素索引
|
||||||
int numFmtIndex = xssfCellStyle.getDataFormat();
|
final int numFmtIndex = xssfCellStyle.getDataFormat();
|
||||||
if (numFmtString == null) {
|
this.numFmtString = ObjectUtil.defaultIfNull(
|
||||||
numFmtString = BuiltinFormats.getBuiltinFormat(numFmtIndex);
|
xssfCellStyle.getDataFormatString(),
|
||||||
} else if (CellDataType.NUMBER == this.cellDataType && org.apache.poi.ss.usermodel.DateUtil.isADateFormat(numFmtIndex, numFmtString)) {
|
BuiltinFormats.getBuiltinFormat(numFmtIndex));
|
||||||
|
if (CellDataType.NUMBER == this.cellDataType && ExcelSaxUtil.isDateFormat(numFmtIndex, numFmtString)) {
|
||||||
cellDataType = CellDataType.DATE;
|
cellDataType = CellDataType.DATE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,6 +183,25 @@ public class ExcelSaxUtil {
|
|||||||
public static boolean isDateFormat(CellValueRecordInterface cell, FormatTrackingHSSFListener formatListener){
|
public static boolean isDateFormat(CellValueRecordInterface cell, FormatTrackingHSSFListener formatListener){
|
||||||
final int formatIndex = formatListener.getFormatIndex(cell);
|
final int formatIndex = formatListener.getFormatIndex(cell);
|
||||||
final String formatString = formatListener.getFormatString(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);
|
return org.apache.poi.ss.usermodel.DateUtil.isADateFormat(formatIndex, formatString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ public class ExcelSaxReadTest {
|
|||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void readBySaxTest2() {
|
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() {
|
private RowHandler createRowHandler() {
|
Loading…
Reference in New Issue
Block a user