I copied the code from one visual project to another project, and it shows a lot of errors in new project. The code works fine in original project, both projects run in same machine/same visual studio. I mean right now old project can compile in the same machine, but old project was built in a different machine last year, but that should have nothing to do with it. While there are a lot of errors for new project.
I tried to follow "potential fix" to fix some errors (I actually don't quite understand), there are still some errors left (without suggesting potential fix), now I cannot compile.
Here is the code, and I got the code from last year's post: exporting-datagridview-table-to-excel-file
private void pbxExcelExport_Click(object sender, EventArgs e)
{
copyAlltoClipboard();
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 Microsoft.Office.Interop.Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
//Delete first blank column (row header column from DataGridView
((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Range["A1"]).EntireColumn.Delete(null);
dgvUsers.ClearSelection();
}
private void copyAlltoClipboard()
{
//Copy title row (Field name)
dgvUsers.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
dgvUsers.SelectAll();
DataObject dataObj = dgvUsers.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}