匯入匯出csv檔案
參考了下列資料
CODE-將CSV檔案內容轉換成ADO.NET DataTable物件
改寫一下匯整如下
/// <summary>
/// Exports the datagrid to CSV.
/// </summary>
/// <param name="dataGridView1">The data grid view1.</param>
/// <param name="FileName">Name of the file.</param>
/// <param name="ColumnName">Name of the column.</param>
/// <param name="HasColumnName">if set to <c>true</c> [has column name].</param>
public static void ExportDatagridToCsv(DataGridView dataGridView1 , string FileName ,string[] ColumnName,bool HasColumnName)
{
string strValue = string.Empty;
//CSV 匯出的標題 要先塞一樣的格式字串 充當標題
if(HasColumnName == true)
strValue = string.Join(",", ColumnName);
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
for (int j = 0; j < dataGridView1.Rows[i].Cells.Count; j++)
{
if (!string.IsNullOrEmpty(dataGridView1[j, i].Value.ToString()))
{
if (j > 0)
strValue = strValue + "," + dataGridView1[j, i].Value.ToString();
else
{
if (string.IsNullOrEmpty(strValue))
strValue = dataGridView1[j, i].Value.ToString();
else
strValue = strValue + Environment.NewLine + dataGridView1[j, i].Value.ToString();
}
}
else
{
if (j > 0)
strValue = strValue + ",";
else
strValue = strValue + Environment.NewLine;
}
}
}
//存成檔案
string strFile = FileName;
if (!string.IsNullOrEmpty(strValue))
{
File.WriteAllText(strFile, strValue, Encoding.Default);
}
}
/// <summary>
/// Exports the data table to CSV.
/// </summary>
/// <param name="dt">The dt.</param>
/// <param name="FileName">Name of the file.</param>
/// <param name="ColumnName">Name of the column.</param>
/// <param name="HasColumnName">if set to <c>true</c> [has column name].</param>
public static void ExportDataTableToCsv(DataTable dt, string FileName, string[] ColumnName,bool HasColumnName)
{
string strValue = string.Empty;
//CSV 匯出的標題 要先塞一樣的格式字串 充當標題
if(HasColumnName == true)
strValue = string.Join(",", ColumnName);
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (!string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
if (j > 0)
strValue = strValue + "," + dt.Rows[i][j].ToString();
else
{
if (string.IsNullOrEmpty(strValue))
strValue = dt.Rows[i][j].ToString();
else
strValue = strValue + Environment.NewLine + dt.Rows[i][j].ToString();
}
}
else
{
if (j > 0)
strValue = strValue + ",";
else
strValue = strValue + Environment.NewLine;
}
}
}
//存成檔案
string strFile = FileName;
if (!string.IsNullOrEmpty(strValue))
{
File.WriteAllText(strFile, strValue, Encoding.Default);
}
}
/// <summary>
/// Reads the CSV to list.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <returns></returns>
public static List<string> ReadCsvToList(string FileName)
{
FileInfo fi = new FileInfo(FileName);
if(fi.Exists)
{
List<string> result = new List<string>();
using(StreamReader sr = new StreamReader(FileName))
{
while (sr.Peek() >= 0)
{
result.Add(sr.ReadLine());
}
}
return result;
}
else return null;
}
/// <summary>
/// Reads the CSV to data table.
/// </summary>
/// <param name="FileName">Name of the file.</param>
/// <param name="HasColumnName">if set to <c>true</c> [has column name].</param>
/// <returns></returns>
public static DataTable ReadCsvToDataTable(string FileName, bool HasColumnName)
{
List<string> Input = ReadCsvToList(FileName);
if(Input != null)
{
string[] sep = new string[]{","};
DataTable dt = new DataTable();
int StartCount = (HasColumnName == true) ? 1:0;
string[] ColumnName = Input[0].Split(sep,StringSplitOptions.None);
for (int i = 0; i < ColumnName.Length; i++)
dt.Columns.Add((HasColumnName == true) ? ColumnName[i] : "C" + i.ToString(), typeof(string));
for(int j= StartCount; j<Input.Count; j++)
{
string[] valuetemp = Input[j].Split(sep,StringSplitOptions.None);
dt.Rows.Add(valuetemp);
}
return dt;
}
else return null;
}
#endregion