private bool eachValueInCol(int colNo, out int len, out int sn, out bool isUnicode, TypeCheckFn checkCell) { len = 0; isUnicode = false; sn = 0; IEnumerator rowEnumerator = _sheet.GetRowEnumerator(); moveToDataRow(rowEnumerator); bool hasValue = false; int i = 0; do { IRow xlRow = (HSSFRow)rowEnumerator.Current; ICell cell = xlRow.GetCell(colNo); string cValue = (cell == null) ? null : cell.ToString(); if (string.IsNullOrEmpty(cValue)) { cValue = null; } else { hasValue = true; } if (!checkCell(cValue)) { return(false); } if (cell != null) { string s = cell.ToString(); if (Regex.IsMatch(s, @"[\u007F-\uFFFD]+"))//匹配中文 @"[\u4e00-\u9fa5]+" { isUnicode = true; } int cLen = s.Length; if (cell.CellType.Equals(CellType.NUMERIC) && s.IndexOf(".") >= 0) { cLen = cell.CellType.Equals(CellType.STRING) ? s.Length : s.IndexOf("."); int cSn = cell.CellType.Equals(CellType.STRING) ? 0 : s.Length - s.LastIndexOf("."); if (cSn > sn) { sn = cSn; } } if (cLen > len) { len = cLen; } } i++; } while ((i < TYPE_TEST_COUNT) && rowEnumerator.MoveNext()); if (i < 1) { throw new XUserException(Lang.UploadFileNoData); } return(hasValue); //return true; }
private void readFieldDef(int colId, FieldDef fieldDef) { IEnumerator rowEnumerator = _sheet.GetRowEnumerator(); int sn = 0; int len = 0; bool isUnicode = false; bool isString = false; TypeCheckFn ckDate = delegate(string s) { DateTime d; return(DateTime.TryParse(s, out d) || DateTime.TryParse(s, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.AssumeLocal, out d) || string.IsNullOrEmpty(s)); }; TypeCheckFn ckBool = delegate(string s) { bool d; return(bool.TryParse(s, out d) || string.IsNullOrEmpty(s)); }; TypeCheckFn ckInt = delegate(string s) { int d; return(int.TryParse(s, out d) || string.IsNullOrEmpty(s)); }; TypeCheckFn ckNum = delegate(string s) { double d; return(double.TryParse(s, out d) || string.IsNullOrEmpty(s)); }; TypeCheckFn ckStr = delegate(string s) { return(true); }; if (eachValueInCol(colId, out len, out sn, out isUnicode, ckDate)) { fieldDef.Type = DatabaseAdmin.getInstance(_connName).typeOfDotNetType(typeof(DateTime)); len = 0; } else if (eachValueInCol(colId, out len, out sn, out isUnicode, ckBool)) { fieldDef.Type = DatabaseAdmin.getInstance(_connName).typeOfDotNetType(typeof(bool)); len = 0; } else if (eachValueInCol(colId, out len, out sn, out isUnicode, ckInt)) { fieldDef.Type = DatabaseAdmin.getInstance(_connName).typeOfDotNetType(typeof(int)); len = 0; } else if (eachValueInCol(colId, out len, out sn, out isUnicode, ckNum)) { fieldDef.Type = DatabaseAdmin.getInstance(_connName).typeOfDotNetType(typeof(double)); len = 24; } else { isString = true; eachValueInCol(colId, out len, out sn, out isUnicode, ckStr); if (isUnicode) { fieldDef.Type = DatabaseAdmin.getInstance(_connName).typeOfDbType(DbType.String); } else { fieldDef.Type = DatabaseAdmin.getInstance(_connName).typeOfDbType(DbType.AnsiString); } } if (isString && len < 1) { len = 255; fieldDef.Type = DatabaseAdmin.getInstance(_connName).typeOfDbType(DbType.String); } fieldDef.Length = len; fieldDef.Procesion = sn; }