是否有任何形式的語句將數據行轉換爲CSV行,以便將其寫入.csv文件? (帶或不帶之間的步驟)Datarow to CSV line C#
4
A
回答
1
一個快速谷歌搜索把這個了: http://www.rcs-solutions.com/blog/2009/01/15/ConvertDataTableToCSVViaExtensionMethod.aspx
這是有點過時。沒有原生的方法來完成它,所以你需要使用循環結構遍歷數據表,但是我建議使用LINQ方法,因爲它會更乾淨。
+0
讀它,看起來很有前景 – Dashzapp 2012-03-08 07:04:32
+0
鏈接被破壞。 – 2017-11-01 00:05:39
3
嘗試用下面的代碼:
StringBuilder sb = new StringBuilder();
var columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName).ToArray();
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dt.Rows)
{
var fields = row.ItemArray.Select(field => field.ToString()).ToArray();
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText("test.csv", sb.ToString());
0
我看this前些天:
根據您可以編寫代碼,像這樣的快速入門:
[DelimitedRecord(",")]
public class Customer
{
public int CustId;
public string Name;
public decimal Balance;
}
FileHelperEngine engine = new FileHelperEngine(typeof(Customer));
Customer[] res = engine.ReadFile("FileIn.txt") as Customer[];
engine.WriteFile("FileOut.txt", res);
相關問題
- 1. Line to Circle轉換
- 2. as3 array tostring to textfiled line by line
- 3. 將dataRow導出到csv
- 4. DataTable To CSV
- 5. Add Contract Line to Contract CRM 2011
- 6. python convert multiline to single line
- 7. CSS forces div to next line
- 8. Python xml to csv
- 9. C#Line In Capture
- 10. vim C++ break line
- 11. C# - Highlight Highlight Line
- 12. 如何通過「com.intellij.openapi」調用「Go to Line」?
- 13. cellArray to csv
- 14. HTML to CSV PHP
- 15. csv to OrderedDict
- 16. pandas:export ndarray to csv
- 17. Dictionary to CSV
- 18. Weka Predictions to CSV
- 19. Easy csv to excel
- 20. Dictionary to CSV - Python
- 21. VB.Net Class to CSV
- 22. Doctrine entity to CSV
- 23. import csv to sql
- 24. C#Excel datarow比較值
- 25. 如何:* .csv - > line - > someArray - >修改
- 26. Prinft line by line of file單線程C
- 27. C#:WriteFile with Line Breaks
- 28. Linq To CSV的性能問題
- 29. convert .xls to .csv java
- 30. netCDF to * .csv without Loops(!)
你有沒有看這個問題[在.net中編寫CSV文件](http://stackoverflow.com/questions/1684667/writing-a-csv-file-in-net)(特別是[這個答案](http://stackoverflow.com/a/2315185/164966)),在這和之間[這些示例](http://msdn.microsoft.com/zh-cn/library/bb386916.aspx)Linq到DataSets,您可能可以在10行代碼中編寫導出。 – R0MANARMY 2012-03-08 07:08:31