⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 importcontactsfromcsv.cs

📁 飞信的收发使用csharp进行开发
💻 CS
字号:
namespace Imps.Client.Pc
{
    using Imps.Common;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;

    internal class ImportContactsFromCSV : IImportContacts
    {
        private string _split = ",";

        public string GetContactsXML(IWin32Window owner, string filePath)
        {
            StringWriter w = new StringWriter();
            XmlTextWriter writer2 = new XmlTextWriter(w);
            writer2.Formatting = Formatting.None;
            writer2.WriteStartElement("Contacts");
            using (StreamReader reader = new StreamReader(filePath, Encoding.Default, true))
            {
                string line = reader.ReadLine();
                if (line.Contains("\t"))
                {
                    this._split = "\t";
                }
                else
                {
                    this._split = ",";
                }
                List<string> list = this.SplitCSVLine(line);
                while (reader.Peek() >= 0)
                {
                    line = reader.ReadLine();
                    writer2.WriteStartElement("Contact");
                    List<string> list2 = this.SplitCSVLine(line);
                    for (int i = 0; i < list.Count; i++)
                    {
                        string str2;
                        if (list2.Count > i)
                        {
                            str2 = list2[i];
                        }
                        else
                        {
                            str2 = "";
                        }
                        writer2.WriteElementString(list[i], str2);
                    }
                    writer2.WriteEndElement();
                }
            }
            writer2.WriteEndElement();
            return w.ToString();
        }

        private bool IsEnd(string content, int index, bool startIsComma)
        {
            int num = 0;
            while ((content.Length > (index + 1)) && (content.Substring(index, 1) == "\""))
            {
                num++;
                index++;
            }
            return (((num == 2) && (content.Substring(index, 1) == this._split)) || (((num == 0) && !startIsComma) || ((num % 2) != 0)));
        }

        private List<string> SplitCSVLine(string line)
        {
            int num2;
            List<string> list = new List<string>();
            string item = "";
            int startIndex = 0;
            bool startIsComma = line.StartsWith("\"");
            while ((num2 = line.IndexOf(this._split, startIndex)) >= 0)
            {
                item = item + line.Substring(startIndex, num2 - startIndex);
                if (this.IsEnd(line, num2 + 1, startIsComma))
                {
                    if (startIsComma)
                    {
                        list.Add(item.Remove(item.Length - 1, 1).Remove(0, 1));
                    }
                    else
                    {
                        list.Add(item);
                    }
                    item = "";
                }
                else
                {
                    item = item + this._split;
                }
                startIndex = num2 + 1;
                if (line.Length > startIndex)
                {
                    startIsComma = line.Substring(startIndex, 1) == "\"";
                }
            }
            if (line.Length > startIndex)
            {
                if (!startIsComma)
                {
                    list.Add(line.Substring(startIndex));
                    return list;
                }
                list.Add(line.Substring(startIndex + 1, (line.Length - startIndex) - 2));
            }
            return list;
        }

        public string Filter
        {
            get
            {
                return "csv files (*.csv)|*.csv";
            }
        }

        public ImportContactType Type
        {
            get
            {
                return ImportContactType.CSV;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -