How to export all columns from datagridview to excel.....?!

Hi

I want to export all my columns from datagridview to excel..., if i export all columns from a dataset it works fine, but i want to export columns from my gridview....?
 how can i do that?
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
 
            excel.Application.Workbooks.Add(true);
            //System.Data.DataTable table = dsReconItems1.Tables[0];
            int ColumnIndex = 0;
            foreach (DataGridViewColumn col in ReconManuallyGridView.Columns)
            {
                ColumnIndex++;
                System.Data.DataTable table = (DataTable)col;
                excel.Cells[1, ColumnIndex] = table;
            }
excel.Visible = true;
            Worksheet worksheet = (Worksheet)excel.ActiveSheet;
            worksheet.Activate();
 
        }

Open in new window

AdnanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SameerJagdaleCommented:
0
AdnanAuthor Commented:
hmm to complex to understand, i have tried but not sucecceded, any other sugestions?
0
SameerJagdaleCommented:
http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html
download the code and check (there is a download code link)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

AdnanAuthor Commented:
Oki, i will check the link, but when i execute my code i get error message saying : Exception from HRESULT: 0x800A03EC.....?

private void ExcelTransactions()
        {
            Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();

            excel.Application.Workbooks.Add(true);
            //DsReconItems.MatchItemRow currentAddMIRow = dsReconItems1.MatchItem.FindByMatchItem_ID(matchItem_ID);
            int ColumnIndex = 0;
            //System.Data.DataTable myTable = new System.Data.DataTable();
            //myTable = (System.Data.DataTable)dsReconItems1.Tables[0];


            //for (ColumnIndex = 0; ColumnIndex < ReconManuallyGridView.Columns.Count; ColumnIndex++)
            //foreach (DataColumn col in myTable.Columns)
            foreach (DataGridViewColumn col in ReconManuallyGridView.Columns)
            {
                ColumnIndex++;
                //System.Data.DataTable table = (DataTable)col;

                excel.Cells[1, ColumnIndex] = col;
            }
           
           
            int rowIndex = 0;
           
            excel.Visible = true;
            Worksheet worksheet = (Worksheet)excel.ActiveSheet;
            worksheet.Activate();

        }
0
AdnanAuthor Commented:
Here is the correct answer and solution to what iam was asking you experts about, but gess what? i resolved it by my self... ;)


private void ExcelTransactions()
        {

            System.Data.DataTable table = dsReconItems1.Tables[0];
           
            Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
            excel.Application.Workbooks.Add(true);

            Worksheet worksheet = (Worksheet)excel.ActiveSheet;
         
            int ColumnIndex = 0;
            foreach (DataColumn col in table.Columns)
            {
                ColumnIndex++;
                //excel.Cells[1, ColumnIndex] = col.ColumnName;
                // col.ColumnName = "VDate";
                worksheet.Cells[1, ColumnIndex = 1] = "VDate";
                worksheet.Cells[1, ColumnIndex = 2] = "Ref";
                worksheet.Cells[1, ColumnIndex = 3] = "Amount";
                worksheet.Cells[1, ColumnIndex = 4] = "Text";
                worksheet.Cells[1, ColumnIndex = 5] = "Comment";
                worksheet.Cells[1, ColumnIndex = 6] = "AccountNo";

                Microsoft.Office.Interop.Excel.Range myCell1 = (Microsoft.Office.Interop.Excel.Range)worksheet.get_Range("A1","B1");
                Microsoft.Office.Interop.Excel.Range myCell2 = (Microsoft.Office.Interop.Excel.Range)worksheet.get_Range("C1", "D1");
                Microsoft.Office.Interop.Excel.Range myCell3 = (Microsoft.Office.Interop.Excel.Range)worksheet.get_Range("E1", "F1");

                Range rg = worksheet.get_Range("A1", "F1");
                rg.Select();
                //bold
                rg.Font.Bold = true;
                // font face
                rg.Font.Name = "Arial";
                //font size
                rg.Font.Size = 8;
                // text wrap
                rg.WrapText = true;
                // cell allignment
                rg.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
                // color index
                rg.Interior.ColorIndex = 15;
                // font weight
                rg.Borders.Weight = 4;
                // Border Line style
                //rg.Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.
                // row height
                rg.Cells.RowHeight = 15;

                myCell1.Font.Bold = true;
                myCell2.Font.Bold = true;
                myCell3.Font.Bold = true;
               

            }
            int rowIndex = 0;

            foreach (Guid matchItem_ID in checkedMatchItems)
            {
                DsReconItems.MatchItemRow currentAddMIRow = dsReconItems1.MatchItem.FindByMatchItem_ID(matchItem_ID);
                if (currentAddMIRow != null)
                {
                    rowIndex++;
                    ColumnIndex = 0;
                    worksheet.Cells[rowIndex + 1, ColumnIndex = 1] = currentAddMIRow.VDate;
                    worksheet.Cells[rowIndex + 1, ColumnIndex = 2] = currentAddMIRow.Ref;
                    worksheet.Cells[rowIndex + 1, ColumnIndex = 3] = currentAddMIRow.Amount;
                    //worksheet.Cells[rowIndex + 2, ColumnIndex = 4] = currentAddMIRow.AllText;
                    //worksheet.Cells[rowIndex + 2, ColumnIndex = 5] = currentAddMIRow.AllText;
                    worksheet.Cells[rowIndex + 1, ColumnIndex = 6] = currentAddMIRow.AccountNo;
                    //foreach (DataRow row in table.Rows)
                    //{
                    //    rowIndex++;
                    //    ColumnIndex = 0;
                    //    worksheet.Cells[rowIndex + 1, ColumnIndex = 1] = currentAddMIRow.VDate;
                    //    //foreach (DataColumn col in table.Columns)
                    //    //{
                    //    //    ColumnIndex++;
                    //    //    worksheet.Cells[rowIndex + 1, ColumnIndex = 1] = currentAddMIRow.VDate;
                    //    //}
                    //}

                }
            }

            excel.Visible = true;
            worksheet.Activate();
           
        }
0
SameerJagdaleCommented:
I think I did provided the links that should have resolved the original issue -
>>Hi

>>I want to export all my columns from datagridview to excel...,

I deserve some points for the links.
0
AdnanAuthor Commented:
thanks.. ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.