Skip to content

Append DatagridView content to Excel file

Guest User

Asked Feb 24, 2015, 4:34 PM EST1 answer
Share
LinkedInX

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:

Code
<pre>private void copyAlltoClipboard2()
        {
            dataGridView4.SelectAll();
            DataObject dataObj = dataGridView4.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }
This one is for pasting :

Python
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();

Python
                        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);

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.