将datagridview导出到csv

时间:2010-09-18 22:27:19

标签: c# datagridview

    DataTable myTable = new DataTable("components");
    DataColumn mydatcolumn;

    mydatcolumn = new DataColumn();
    mydatcolumn.DataType = System.Type.GetType("System.String");
    mydatcolumn.ColumnName = "Component";

    myTable.Columns.Add(mydatcolumn);

    mydatcolumn = new DataColumn();
    mydatcolumn.DataType = System.Type.GetType("System.String");
    mydatcolumn.ColumnName = "Service Category";

    myTable.Columns.Add(mydatcolumn);

    mydatcolumn = new DataColumn();
    mydatcolumn.DataType = System.Type.GetType("System.String");
    mydatcolumn.ColumnName = "Component Owner";

    myTable.Columns.Add(mydatcolumn);


    for (int i = 0; i < serviceList.Count; i++)
    {
        DataRow mydatarow;
        //DataRow mydatarow2;
        mydatarow = myTable.NewRow();
        mydatarow["component"] = ComponentList[i];
        //mydatarow2 = myTable.NewRow();
        mydatarow["Service Category"] = sc[i];
        myTable.Rows.Add(mydatarow);
    }
    componentRGOdataGridView.DataSource = myTable;    

private void addCoButton_Click(object sender, EventArgs e)
{

}

如上所述,我正在创建一个包含三列的DataTable,并在2 List&lt;&gt;中显示这些项目。 (使用此dataTable在datagridview上显示)。列表仅填充2列的行,第三列用于用户输入。在该按钮单击事件上,我只想在用户提供输入的文件中写入那些行,并忽略其余的行。我怎样才能做到这一点。 非常感谢

2 个答案:

答案 0 :(得分:0)

使用componentRGOdataGridView.Rows获取componentRGOdataGridView中的所有row
然后使用DataGridViewRow类的Cells属性来获取单元格的值 在此之后,您可以使用自己喜欢的csv编写器制作csv文件。

您可以尝试使用此代码将单元格的值打印到输出窗口。

    foreach (DataGridViewRow item in componentRGOdataGridView.Rows)
    {
        Debug.Write(item.Cells[0].Value.ToString());
        Debug.Write(item.Cells[1].Value.ToString());
        Debug.WriteLine(item.Cells[2].Value.ToString());
    }

答案 1 :(得分:0)

DataGridView控件提供DataGridView.GetClipboardContent()方法,该方法将所选单元格格式化为可放入Windows剪贴板的DataObject。然后,您可以将剪贴板的内容检索为TextDataFormat.CommaSeparatedValue。这会将剪贴板的内容作为CSV格式的字符串返回,该字符串可以写入文件。通过使用DataGridView提供的方法SelectAll(),您可以选择要复制到剪贴板中的整个DataGridView。您甚至可以通过将DataGridView.ClipboardCopyMode指定为枚举DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText来复制标题行。

见证以下代码的简单性:

void SaveDataGridViewToCSV(string filename)
{
    // Save the current state of the clipboard so we can restore it after we are done
    IDataObject objectSave = Clipboard.GetDataObject();

    // Choose whether to write header. Use EnableWithoutHeaderText instead to omit header.
    dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
    // 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));

    // Restore the current state of the clipboard so the effect is seamless
    if(objectSave != null) // If we try to set the Clipboard to an object that is null, it will throw...
    {
        Clipboard.SetDataObject(objectSave);
    }
}

请注意,在复制DataGridView之前,我还采取了保存剪贴板内容的预防措施,这样我就可以在完成后恢复它。

我相信这种方法更优越,因为您使用的是已经为您提供的框架,而不是通过循环遍历行和单元来重新发明轮子,这很容易出错。

希望这能解决你的问题...

相关问题