如何将大数据保存到文件
我有大约 50,000 个条目的列表,这些条目填充在 wpf 的数据网格中。现在我想将列表中的数据保存到一个文件中,该文件可以是文本,或者最好是 CSV。由于列表太大。我实现的方法有一个问题,可能是简单的文本文件写入,或者是将内容从数据网格复制到剪贴板,然后返回字符串,然后使用 StreamReader 将该字符串复制到文件的方法。即使在后台工作,也需要大约 4-5 分钟。
有什么方法可以快速保存大量列表到文件中吗?
我在 WPF
代码中使用 DataGrid
dataGrid1.SelectAllCells();
dataGrid1.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dataGrid1);
String result = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
///Never reach to step Below thread stays on above line
dataGrid1.UnselectAllCells();
Clipboard.Clear();
StreamWriter file = new System.IO.StreamWriter(SavePageRankToPDF.FileName);
file.WriteLine(result);
file.Close();
I have list of maybe 50,000 entries that are populated in datagrid in wpf. Now I want to save the data in the list to a file that may be text, or preferably CSV. As list is too big. There is a problem that my implemented method that may be simple text file writing or the method to copy the contents from the datagrid to clipboard and then back to string, and then that string to file using StreamReader. It consumes approx 4-5 minutes even it is in background worker.
Is there any way that I can save huge list to file quickly?
I am using DataGrid in WPF
CODE
dataGrid1.SelectAllCells();
dataGrid1.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dataGrid1);
String result = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
///Never reach to step Below thread stays on above line
dataGrid1.UnselectAllCells();
Clipboard.Clear();
StreamWriter file = new System.IO.StreamWriter(SavePageRankToPDF.FileName);
file.WriteLine(result);
file.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不迭代数据表并构建 csv 文件,而不是使用剪贴板呢?
更新
以下是一些示例:
将 DataTable 转换为 CSV 流
将 DataSet\DataTable 转换为 CSV
Instead of using the clipboard, why not iterate through the datatable and build the csv file.
Update
Here are some examples:
Convert DataTable to CSV stream
Converting DataSet\DataTable to CSV
有用的一件事是在将数据网格用于显示目的时不要将所有数据加载到数据网格中。使用分页是一个好主意:仅将计算或显示所需的数据加载到数据网格中。如果用户想要查看/使用更多数据,请返回您的数据源并获取更多数据。您的应用程序不仅运行得更快,而且使用的内存也会少得多。
One thing that will help is to not load ALL of your data into the datagrid when using it for display purposes. It'd be a good idea to use paging: only load the data into the datagrid that will be needed for calculations or display purposes. If the user wants to see/use more data, go back to your data source and get more of the data. Not only will your app run faster, you'll use much less memory.