我必須將DataTable
的內容保存爲CSV
文件。在DataTable
一切都很好,數據來自SQL
查詢。但是,如果我在Excel
開拓CSV
我可以看到這一點:逗號和逗號之間的區別是什麼?
+---------------------+---------------------+------------------------------+------+------+----+
| DisplayName0 | Publisher0 | col3 | col4 | col5 | |
+---------------------+---------------------+------------------------------+------+------+----+
| Dropbox | Dropbox, Inc. | 432 | 415 | 17 | |
| Chef Client v12.4.3 | Chef Software | Inc. <[email protected]>"" | 193 | 180 | 13 |
| Cisco AnyConnect | Cisco Systems, Inc. | 836 | 824 | 12 | |
+---------------------+---------------------+------------------------------+------+------+----+
'Dropbox, Inc.' -> 'Dropbox, Inc.' //this is good
'Cisco Systems, Inc.' -> 'Cisco Systems, Inc.' //this is good too
'"Chef Software, Inc. <[email protected]>"' -> 'Chef Software' and ' Inc. <[email protected]>""' //what happened here?
導出DataTable
到CSV
我好老的代碼是:
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(dlg3.FileName);
string strHeader = "";
for (int s = 0; s < tbl.Columns.Count; s++)
{
strHeader += "\"" + tbl.Columns[s].ColumnName + "\",";
}
streamWriter.WriteLine(strHeader);
for (int m = 0; m < tbl.Rows.Count; m++)
{
string strRowValue = "";
for (int n = 0; n < tbl.Columns.Count; n++)
{
if (tbl.Rows[m][n].ToString() != "")
{
strRowValue += "\"" + tbl.Rows[m][n] + "\",";
}
else
{
strRowValue += tbl.Rows[m][n] + ",";
}
}
streamWriter.WriteLine(strRowValue);
}
streamWriter.Close();
所以,問題是爲什麼逗號收件箱不是「生成」新的單元格,而是在廚師客戶端中做的?
這就是爲什麼我的每一個單元格的值前後加上'「'基於這個Excel通常逃脫一切,但現在 – fishmong3r
請使用正確的CSV NuGet包,而不是 –
這可能。如果你包含你的程序生成的文本(鼠標而不是你對Excel如何解釋文本的解釋)。 – Richard