Browse Source

change packge and add date format support

Looly 5 years ago
parent
commit
7103adc02e

+ 8 - 8
hutool-poi/src/main/java/cn/hutool/poi/excel/sax/Excel07SaxReader.java

@@ -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;
 				}
 			}

+ 19 - 0
hutool-poi/src/main/java/cn/hutool/poi/excel/sax/ExcelSaxUtil.java

@@ -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);
 	}
 

hutool-poi/src/test/java/cn/hutool/poi/excel/test/BigExcelWriteTest.java → hutool-poi/src/test/java/cn/hutool/poi/excel/BigExcelWriteTest.java


hutool-poi/src/test/java/cn/hutool/poi/excel/test/CellUtilTest.java → hutool-poi/src/test/java/cn/hutool/poi/excel/CellUtilTest.java


hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelReadTest.java → hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelReadTest.java


+ 1 - 1
hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelSaxReadTest.java

@@ -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() {

hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelUtilTest.java → hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelUtilTest.java


hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelWriteTest.java → hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelWriteTest.java


hutool-poi/src/test/java/cn/hutool/poi/excel/test/OrderExcel.java → hutool-poi/src/test/java/cn/hutool/poi/excel/OrderExcel.java


hutool-poi/src/test/java/cn/hutool/poi/excel/test/TestBean.java → hutool-poi/src/test/java/cn/hutool/poi/excel/TestBean.java


hutool-poi/src/test/java/cn/hutool/poi/word/test/WordWriterTest.java → hutool-poi/src/test/java/cn/hutool/poi/word/WordWriterTest.java