Подключаем через AddReference "Microsoft.Office.Interop.Excel"
...
using Excel = Microsoft.Office.Interopt.Excel;
...
private void btnExportToExcel_Click(object sender, EventArgs e)
{
dgv.SelectAll();
DataObject dataObj = dgv.GetClipboardContent();
DataObject dataObject = new DataObject();
Encoding enc = Encoding.Unicode;
byte[] bytes = enc.GetBytes(dataObj.GetText());
System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes);
dataObject.SetData("UnicodeText", false, stream);
Clipboard.SetDataObject(dataObject, true);
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();
{
dgv.SelectAll();
DataObject dataObj = dgv.GetClipboardContent();
DataObject dataObject = new DataObject();
Encoding enc = Encoding.Unicode;
byte[] bytes = enc.GetBytes(dataObj.GetText());
System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes);
dataObject.SetData("UnicodeText", false, stream);
Clipboard.SetDataObject(dataObject, true);
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);
}
}