private static bool _AnalyzeStringType(FieldInfo fieldInfo, TableInfo tableInfo, DataTable dt, int columnIndex, FieldInfo parentField, out int nextFieldColumnIndex, out string errorString)
    {
        // 检查string型字段数据格式声明是否正确
        if (!"string(trim)".Equals(fieldInfo.DataTypeString) && !"string".Equals(fieldInfo.DataTypeString))
        {
            errorString          = string.Format("错误:string型字段定义非法,若要自动去除输入字符串的首尾空白字符请将数据类型声明为\"string(trim)\",否则声明为\"string\",而你输入的为\"{0}\"", fieldInfo.DataTypeString);
            nextFieldColumnIndex = columnIndex + 1;
            return(false);
        }

        fieldInfo.Data = new List <object>();

        if ("string(trim)".Equals(fieldInfo.DataTypeString, StringComparison.CurrentCultureIgnoreCase))
        {
            for (int row = ExcelTableSetting.DataFieldDataStartRowIndex; row < dt.Rows.Count; ++row)
            {
                // 如果本行该字段的父元素标记为无效则该数据也标为无效
                if (parentField != null && (bool)parentField.Data[row - ExcelTableSetting.DataFieldDataStartRowIndex] == false)
                {
                    fieldInfo.Data.Add(null);
                }
                else
                {
                    string str = dt.Rows[row][columnIndex].ToString().Trim();
                    AppLanguage.GetLanguageDictData(str);
                    fieldInfo.Data.Add(AppLanguage.GetNewLanguageText(str));
                }
            }
        }
        else
        {
            for (int row = ExcelTableSetting.DataFieldDataStartRowIndex; row < dt.Rows.Count; ++row)
            {
                if (parentField != null && (bool)parentField.Data[row - ExcelTableSetting.DataFieldDataStartRowIndex] == false)
                {
                    fieldInfo.Data.Add(null);
                }
                else
                {
                    string str = dt.Rows[row][columnIndex].ToString();
                    AppLanguage.GetLanguageDictData(str);
                    fieldInfo.Data.Add(AppLanguage.GetNewLanguageText(str));
                }
            }
        }

        errorString          = null;
        nextFieldColumnIndex = columnIndex + 1;
        return(true);
    }
Exemplo n.º 2
0
    /// <summary>
    /// 解析json型数据的定义,将json通过LitJson库解析出来
    /// </summary>
    private static bool _AnalyzeJsonType(FieldInfo fieldInfo, TableInfo tableInfo, DataTable dt, int columnIndex, FieldInfo parentField, out int nextFieldColumnIndex, out string errorString)
    {
        StringBuilder errorStringBuilder = new StringBuilder();

        fieldInfo.Data       = new List <object>();
        fieldInfo.JsonString = new List <string>();
        for (int row = ExcelTableSetting.DataFieldDataStartRowIndex; row < dt.Rows.Count; ++row)
        {
            // 如果本行该字段的父元素标记为无效则该数据也标为无效
            if (parentField != null && (bool)parentField.Data[row - ExcelTableSetting.DataFieldDataStartRowIndex] == false)
            {
                fieldInfo.Data.Add(null);
                fieldInfo.JsonString.Add(null);
            }
            else
            {
                string inputData = dt.Rows[row][columnIndex].ToString().Trim();
                AppLanguage.GetLanguageDictData(inputData);
                inputData = AppLanguage.GetNewLanguageText(inputData);
                if (string.IsNullOrEmpty(inputData))
                {
                    fieldInfo.Data.Add(null);
                    fieldInfo.JsonString.Add(null);
                }
                else if (inputData == "[]")
                {
                    fieldInfo.Data.Add(null);
                    fieldInfo.JsonString.Add("[]");
                }
                else
                {
                    fieldInfo.JsonString.Add(inputData);
                    try
                    {
                        try
                        {
                            LitJson.JsonData jsonData = LitJson.JsonMapper.ToObject(inputData);

                            object jsonData2 = JsonConvert.DeserializeObject(inputData);
                            // TestModel testModel = JsonConvert.DeserializeObject<TestModel>(inputData);
                            //fieldInfo.Data.Add(jsonData);
                            //Count = “jsonData.Count”引发了类型“System.InvalidOperationException”的异常
                            if (jsonData.Count > 0)
                            {
                                fieldInfo.Data.Add(jsonData);
                            }
                        }
                        catch
                        {
                            errorStringBuilder.AppendFormat("第{0}行所填json字符串({1})非法,原因为:不是合的json字符\n", row + ExcelTableSetting.DataFieldDataStartRowIndex + 1, inputData);
                        }
                    }
                    catch (JsonException exception)
                    {
                        errorStringBuilder.AppendFormat("第{0}行所填json字符串({1})非法,原因为:{2}\n", row + ExcelTableSetting.DataFieldDataStartRowIndex + 1, inputData, exception.Message);
                    }
                }
            }
        }

        nextFieldColumnIndex = columnIndex + 1;
        errorString          = errorStringBuilder.ToString();
        if (string.IsNullOrEmpty(errorString))
        {
            errorString = null;
            return(true);
        }
        else
        {
            errorString = string.Concat("以下行中所填json字符串非法:\n", errorString);
            return(false);
        }
    }