简体   繁体   English

以C#Windows形式从DataGridView写入文本文件

[英]Writing to a text file from DataGridView in C# windows form

I want to write the data from my DataGridView to a textfile. 我想将数据从DataGridView写入文本文件。

The following code writes the data but it is not formatted correctly. 以下代码写入数据,但格式不正确。

StreamWriter sW = new StreamWriter(dirLocationString);

string lines = "";

for (int row = 0; row< numRows1; row++){
     for (int col = 0; col < 4; col++)
         {

          lines = lines + " " + dataGridView1.Rows[row].Cells[col].Value.ToString();

         }

      sW.WriteLine(lines);
}

   sW.Close();   

I want the output format to be like: 我希望输出格式如下:

AAAA, BBBB, CCCC, DDDD 

But instead it displays the following: 但是,它显示以下内容:

AAAA BBBB CCCC DDDD
AAAA BBBB CCCC DDDD AAAA BBBB CCCC DDDD
AAAA BBBB CCCC DDDD AAAA BBBB CCCC DDDD AAAA BBBB CCCC DDDD
and so on..

Init lines in first loop : 第一个循环中的初始化lines

StreamWriter sW = new StreamWriter(dirLocationString);

for (int row = 0; row< numRows1; row++){
    string lines = "";
    for (int col = 0; col < 4; col++)
    {
        lines += (string.IsNullOrEmpty(lines) ? " " : ", ") + dataGridView1.Rows[row].Cells[col].Value.ToString();
    }

    sW.WriteLine(lines);
}

sW.Close(); 

Unfortunately looping through rows and columns is prone to errors and not very succinct. 不幸的是,在行和列之间循环很容易出错,而且不太简洁。 Here is a little hack that takes advantage of Windows.Forms.Clipboard and DataGridView.GetClipboardContent() to do all the dirty work for you. 这是一个利用Windows.Forms.ClipboardDataGridView.GetClipboardContent()来为您完成所有肮脏工作的小技巧。 DataGridView.GetClipboardContent() returns all the selected data cells as a DataObject, which is how the Clipboard class is able to store different types of data and formatting. DataGridView.GetClipboardContent()将所有选定的数据单元作为DataObject返回,这就是Clipboard类能够存储不同类型的数据和格式的方式。 The contents of the clipboard are then written to a file using the File class. 然后使用File类将剪贴板的内容写入文件。 You say you want a text file, but I see commas the example of your desired output, so I am assuming you would like a CSV file. 您说您想要一个文本文件,但是我以逗号分隔所需输出的示例,因此我假设您想要一个CSV文件。 You can also write out a text file by changing the Clipboard.GetText parameter. 您还可以通过更改Clipboard.GetText参数写出文本文件。

void SaveDataGridViewToCSV(string Filename)
{
    // Choose whether to write header. Use EnableWithoutHeaderText instead to omit header.
    dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithAutoHeaderText;
    // Select all the cells
    dataGridView1.SelectAll();
    // Copy (set clipboard)
    Clipboard.SetDataObject(dataGridView1.GetClipboardContent());
    // Paste (get the clipboard and serialize it to a file)
    File.WriteAllText(Filename,Clipboard.GetText(TextDataFormat.CommaSeparatedValue));
}

Please note that an object must be serializable for it to be put on the Clipboard. 请注意,对象必须可序列化才能放置在剪贴板上。

For a tab-delimited file, use the TextDataFormat.Text enum in your call to Clipboard.GetText() . 对于制表符分隔的文件,请在对Clipboard.GetText()调用中使用TextDataFormat.Text枚举。 You can also output your DataGridView as HTML by using TextDataFormat.Html instead of TextDataFormat.CommaSeparatedValue , but there is extra header data you have to parse out. 您也可以通过使用TextDataFormat.Html而不是TextDataFormat.CommaSeparatedValue将DataGridView输出为HTML,但是还需要解析额外的标头数据。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM