将dataGridView数据导出为ex​​cel时出错

时间:2018-04-03 10:07:54

标签: c# excel

这是我的代码:

public virtual void CopyToClipboard()
{
    gridView1.SelectAll();
    DataObject dataObj = gridView1.GetClipboardContent();
    if (dataObj != null)
        Clipboard.SetDataObject(dataObj);            
}

private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    CopyToClipboard();
    Microsoft.Office.Interop.Excel.Application xlexcel;
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;
    xlexcel = new Excel.Application();
    xlexcel.Visible = true;
    xlWorkBook = xlexcel.Workbooks.Add(misValue);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
    CR.Select();
    xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
}

这就是错误:

  

错误2' DevExpress.XtraGrid.Views.Grid.GridView'不包含' GetClipboardContent'的定义没有扩展方法' GetClipboardContent'接受类型' DevExpress.XtraGrid.Views.Grid.GridView'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

我只需要将gridview的数据导出到excel文件,只导出我在网格中看到的内容。

1 个答案:

答案 0 :(得分:0)

GetClipboardContent是一种DataGridView方法,您使用的是DevExpress.XtraGrid。您可以使用CopyToClipboard