我有从DataGridView导出到.txt文件中的数据有问题。DataGridView导出器在C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace TSQ
{
class TxtExporter : IExporter
{
public void Export(DataGridView dataGrid, Output output)
{
if (output == null)
return;
string filePath = output.TemplatePath;
try
{
using (StreamWriter sw = new StreamWriter(filePath))
{
for (int row = 0; row < dataGrid.Rows.Count; row++)
{
for (int col = 0; col < dataGrid.Columns.Count; col++)
{
sw.Write(dataGrid[col, row].Value.ToString());
sw.Write('\t');
}
sw.WriteLine();
//sw.Flush(); <- also doesn`t work if uncommented
}
}
}
catch (Exception)
{
//MessageBox.Show(exc.Message);
throw;
}
}
}
}
当我从数据库下载大数据到GridView控件(超过350K记录),我尝试将其导出为txt文件,之后的记录200K左右,我得到“OutOfMemoryException异常” ...... 你有什么想法如何从DataGridView导出大数据?
最好的问候, 迈克尔
您是否尝试过通过你的DataGridView循环和保存的内容一个StringBuilder(),然后使用的StreamWriter写一个字符串文本文件呢? –
[在C#窗体中从DataGridView写入文本文件]的可能重复(http://stackoverflow.com/questions/19311535/writing-to-a-text-file-from-datagridview-in-c-sharp- windows-form) –
我正在尝试使用剪贴板,但它的工作速度很慢,也会导致内存异常:( – Roofy