Guest User

Guest User

  • Tech Writer
  • 2
  • 776

Append DatagridView content to Excel file

Feb 24 2015 9:34 PM

In my Windows Form I have 3 DataGridView placed within a TabControl. All I can do as of now is to export the DataGridView content to a new excel file with 3 sheets. But then I am also ask to append the content of each DataGridView to an Excel master file with 3 sheets as well. Any Idea on how to do this?

Ive tried doing copy paste of DataGridView.It works but when it paste the content of DataGridView to the masterfile it places the 1st column of DataGridView to the 2nd column in the excel file leaving the 1st column in excel empty and it does not paste the DataGridView content to the sheet I want it to be pasted.

Here is the code I use to copy the DataGridView content:

<pre lang="c#">&lt;pre&gt;private void copyAlltoClipboard2()
        {
            dataGridView4.SelectAll();
            DataObject dataObj = dataGridView4.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }</pre>This one is for pasting :

<pre lang="c#">string path = "C:\\Users\\Administrator\\Desktop\\SAMPLE EXCEL\\MH.xlsx";
                        mWorkBook = oXL.Workbooks.Open(path, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                        mWorkSheets = mWorkBook.Worksheets;
                        mWSheet3 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkBook.Sheets["BBH"];
                        copyAlltoClipboard2();

                        Excel.Range last = mWSheet3.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
                        Excel.Range range = mWSheet3.get_Range("A1", last);

                        mWSheet3.PasteSpecial(range, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);</pre>


Answers (1)