Exemplo n.º 1
0
    /// 初始化文本>>数据转换处理
    private void InitText()
    {
        /// 拆分数据
        _text = _text.Replace(_STR_R, _STR_N);
        _text = _text.Replace(_STR_N + _STR_N, _STR_N);
        string[] arr = _text.Split(_STR_N.ToCharArray());
        /// 去表头
        if (arr.Length > 0)
        {
            byte[] byt = System.Text.Encoding.UTF8.GetBytes(arr[0]);
            if (byt.Length >= 3 &&
                byt[0] == 0xEF &&
                byt[1] == 0xBB &&
                byt[2] == 0xBF)
            {
                arr[0] = System.Text.Encoding.UTF8.GetString(byt, 3, byt.Length - 3);
            }
        }
        /// 检验数据有效性
        if (arr.Length < 2)
        {
            UtilLog.LogError("表太短>>\n" + arr.Length);
            UtilLog.LogError(_text);
            return;
        }

        /// 生成字段key值
        string[] arrLabel = arr[0].Split(split);
        int      index    = 0;

        for (index = 0; index < arrLabel.Length; index++)
        {
            if (string.IsNullOrEmpty(arrLabel[index]))
            {
                continue;
            }
            if (_key.ContainsKey(arrLabel[index]))
            {
                UtilLog.LogError("表中有相同重复字段>>\n" + arr[0]);
                continue;
            }
            _key.Add(arrLabel[index], index);
        }
        /// 生成值内容 [属性有问题再改WILL]
        for (index = 3; index < arr.Length; index++)
        {
            /// 去掉无效行
            if (string.IsNullOrEmpty(arr[index]))
            {
                continue;
            }
            /// 三行 无字段 剔除
            if (arr[index].StartsWith(",,,"))
            {
                continue;
            }
            /// 拆分
            bool IsHead = arr[index][0] == ',';
            /// 使用正则拆分!!!难受
            System.Text.RegularExpressions.MatchCollection regex_math = regex.Matches(IsHead ? "&" + arr[index] : arr[index]);
            if (regex_math.Count <= 0)
            {
                continue;
            }
            string[] arrValue = new string[regex_math.Count];
            for (int i = 0; i < regex_math.Count; i++)
            {
                arrValue[i] = regex_math[i].Value;
                if (this._split_type == UtilCsvReaderType.comma)
                {
                    /// 去掉没用的双引号
                    if (arrValue[i].StartsWith(",\""))
                    {
                        arrValue[i] = arrValue[i].Substring(2, arrValue[i].Length - 3);
                        continue;
                    }
                    /// 不可能出现小于这个的情况
                    switch (arrValue[i][0])
                    {
                    case ',':
                    case '&':
                        arrValue[i] = arrValue[i].Substring(1);
                        break;

                    /// 头文件没有,号
                    case '"':
                        arrValue[i] = arrValue[i].Substring(1, arrValue[i].Length - 2);
                        break;
                    }
                }
                if (this._split_type == UtilCsvReaderType.tab)
                {
                    /// 去掉没用的双引号
                    if (arrValue[i].StartsWith("\t\""))
                    {
                        arrValue[i] = arrValue[i].Substring(2, arrValue[i].Length - 3);
                        continue;
                    }
                    /// 长度不够
                    if (arrValue[i].Length <= 0)
                    {
                        continue;
                    }
                    /// 不可能出现小于这个的情况
                    switch (arrValue[i][0])
                    {
                    case '\t':
                    case '&':
                        arrValue[i] = arrValue[i].Substring(1);
                        break;

                    /// 头文件没有,号
                    case '"':
                        arrValue[i] = arrValue[i].Substring(1, arrValue[i].Length - 2);
                        break;
                    }
                }
            }
            _keyValue.Add(arrValue);
        }
        /// 原有文本清空
        _text = null;
        arr   = null;
    }