This is my code, but its taking time to save record in excel. In SQL Server table have 11201 rows and 80 columns.

please suggest

 private void button1_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
            app.Visible = true;
            worksheet = workbook.Sheets["Sheet1"];
            worksheet = workbook.ActiveSheet;
            worksheet.Name = "Exported from gridview";
            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
            }
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                }
            }
            workbook.SaveAs("d:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            workbook.Close();
            app.Quit();



            MessageBox.Show("Its saved");
        }

Edited 3 Years Ago by Dani: Formatting fixed

Well, you're making it do 11201 * 80 reads from the grid view so that simply will take time. Is the problem that it is timing out or are you simply looking for a way to make it faster?

Well, you're making it do 11201 * 80 reads from the grid view so that simply will take time. Is the problem that it is timing out or are you simply looking for a way to make it faster?

I am looking faster way.....

Do you even need the grid view? I doubt you are viewing 11000+ rows on the screen. A direct export from the database to XML would be much faster. This is assuming the file doesn't NEED to be a .xls however. If you did that you would, of course, end up with an XML file (which can be opened in excel).

This article has been dead for over six months. Start a new discussion instead.