Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Export Gird View to Excel in C#: Is Very Slow for Large Amount Data

$
0
0

Hi Dears 

I Have a Class Code for Exporting Grid View to Excel File , it work find but for Large amount data , it is very slow 

is any one help me ??

My Code is : 

class GridviewToExcel
    {
        public static void Form(DataGridView grv)
        {
            Excel.Application app = new Excel.Application();
            //app.Visible = true;
            Excel.Workbook wb = app.Workbooks.Add(1);
            Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
            // changing the name of active sheet
            ws.Name = "Exported";

            ws.Rows.HorizontalAlignment = HorizontalAlignment.Center;
            // storing header part in Excel
            for (int i = 1; i < grv.Columns.Count + 1; i++)
            {
                ws.Cells[1, i] = grv.Columns[i - 1].HeaderText;
            }

            // storing Each row and column value to excel sheet
            for (int i = 0; i < grv.Rows.Count; i++)
            {
                for (int j = 0; j < grv.Columns.Count; j++)
                {
                    ws.Cells[i + 2, j + 1] = grv.Rows[i].Cells[j].Value.ToString();
                }
            }
            // sizing the columns
            ws.Cells.EntireColumn.AutoFit();
            app.Visible = true;
        }
    }

thanks before 

farzad khajeh


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>