protected static void CreateCSVFile(DataTable dtImport, string sPath)
{
StreamWriter sw = new StreamWriter(sPath, false, System.Text.Encoding.UTF8);
int iCol = dtImport.Columns.Count;
if (dtImport.Columns.Count > 0)
{
sw.Write(dtImport.Columns[0]);
}
for (int i = 1; i < dtImport.Columns.Count; i++)
{
sw.Write("," + dtImport.Columns[i]);
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dtImport.Rows)
{
if (dtImport.Columns.Count > 0 && !Convert.IsDBNull(dr[0]))
{
sw.Write(Convert.ToString(dr[0]));
}
for (int i = 1; i < iCol; i++)
{
sw.Write("," + Convert.ToString(dr[i]));
}
sw.Write(sw.NewLine);
}
sw.Close();
}
留言
張貼留言