Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

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

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);
Posted
Updated 24-Feb-15 15:18pm
v2

1 solution

see the link & also set EnableEventValidation="false" to the form

http://www.aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900