Link to home
Start Free TrialLog in
Avatar of Adnan
AdnanFlag for Norway

asked on

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

Avatar of SameerJagdale
SameerJagdale
Flag of United States of America image

Avatar of Adnan

ASKER

hmm to complex to understand, i have tried but not sucecceded, any other sugestions?
ASKER CERTIFIED SOLUTION
Avatar of SameerJagdale
SameerJagdale
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adnan

ASKER

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

        }
Avatar of Adnan

ASKER

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();
           
        }
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.
Avatar of Adnan

ASKER

thanks.. ;)