📄 importcontactsfromcsv.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.get_Count(); i++)
{
string text2;
if (list2.get_Count() > i)
{
text2 = list2.get_Item(i);
}
else
{
text2 = "";
}
writer2.WriteElementString(list.get_Item(i), text2);
}
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 index;
List<string> list = new List<string>();
string text = "";
int startIndex = 0;
bool startIsComma = line.StartsWith("\"");
while ((index = line.IndexOf(this._split, startIndex)) >= 0)
{
text = text + line.Substring(startIndex, index - startIndex);
if (this.IsEnd(line, index + 1, startIsComma))
{
if (startIsComma)
{
list.Add(text.Remove(text.Length - 1, 1).Remove(0, 1));
}
else
{
list.Add(text);
}
text = "";
}
else
{
text = text + this._split;
}
startIndex = index + 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 + -