So I am trying to add an "Export to .XLS" option for record data. I have the following code segments for handling the exporting of the data from the DataGridView, however when it opens the Excel file instead of the data being present Cell A1 simply contains the string "System.Drawing."
Can anyone point out what I'm doing incorrectly? Thanks.
private void copyGridToClipboard(DataGridView dg)
{
dg.SelectAll();
dg.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
DataObject dataObj = dg.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}
private void myButton_Click(object sender, EventArgs e)
{
copyGridToClipboard(myDataGridView);
Excel.Application xlexcel;
Excel.Workbook xlWorkbook;
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);
Microsoft.Office.Interop.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);
}