• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2448
  • Last Modified:

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

0
Adnan
Asked:
Adnan
  • 4
  • 3
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now