是否有任何形式的语句将数据行转换为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. DataTable To CSV
- 2. CSS forces div to next line
- 3. vim C++ break line
- 4. C# - Highlight Highlight Line
- 5. 如何通过“com.intellij.openapi”调用“Go to Line”?
- 6. pandas:export ndarray to csv
- 7. import csv to sql
- 8. HTML to CSV PHP
- 9. Dictionary to CSV - Python
- 10. Dictionary to CSV
- 11. csv to OrderedDict
- 12. Prinft line by line of file单线程C
- 13. Rails 4 download to CSV
- 14. Groupby to csv文件
- 15. .Net Entity Framework to CSV
- 16. Javascript/convert json to csv
- 17. C#的DataRow空检查
- 18. #line关键字在C
- 19. C#Json反序列化异常(“Error conversion value”id“to type'Eng_Tab.JsonData'。Path'[0]',line 1,position 5.”)
- 20. datatable to Excel in c#
- 21. 在c中使用Linq选择DataRow#
- 22. D3 Stack Line Chart
- 23. text to csv search function javascript
- 24. BeautifulSoup Absoute URLs Print to CSV
- 25. Linq to CSV按列选择
- 26. line 3 divs on one line
- 27. Linq to Datarow,选择多列作为独特?
- 28. 最快的方式从datarow C#
- 29. LINQ-to-SQL:使用CSV进行搜索
- 30. 从C#中的DataRow []中删除行
你有没有看这个问题[在.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