帮写了个小程序,有些小细节
1.读取(弹出对话框供选择文件)
private void button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog file = new OpenFileDialog();
file.ShowDialog();
FileStream fs = new FileStream(file.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
//记录每次读取的一行记录
string strLine = "";
//记录每行记录中的各字段内容
string[] aryLine;
//读取第一行
sr.ReadLine();
strLine = strLine.Trim();
string[] separators = { "," }; // { ",", ".", "!", "?", ";", ":", " " };
aryLine = strLine.Split(separators, System.StringSplitOptions.RemoveEmptyEntries);
//逐行读取CSV中的数据
while ((strLine = sr.ReadLine()) != null)
{
strLine = strLine.Trim();
aryLine = strLine.Split(separators, System.StringSplitOptions.RemoveEmptyEntries);
if (aryLine.Length > 1)
{
/
}
}
label1.Text = file.FileName;
label1.Visible = true;
sr.Close();
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
2.写入(弹出对话框供选择文件)
private void button3_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog file = new SaveFileDialog();
file.Filter = "csv文件|*.csv";
file.InitialDirectory = "C:\\Users\\admin\\Desktop";
file.ShowDialog();
// 实例化流对象,以特定的编码向流中写入字符。
FileStream fs = new FileStream(file.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("gb2312"));
//写入首行
string sb = "nodeID,tagid";
sw.WriteLine(sb);
foreach (var i in XyToNodeDict)
{
/
foreach (var j in XyToTagDict)
{
}
sw.WriteLine(data);
}
label3.Text = file.FileName;
label3.Visible = true;
sw.Flush();
sw.Close();
sw.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}