Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7667913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:13:28+00:00 2026-05-31T15:13:28+00:00

im attempting to export the datagridview to excel(.xls) in a winforms application using visual

  • 0

im attempting to export the datagridview to excel(.xls) in a winforms application using visual studio 2010 in C#, the problem being it is taking forever to save, so far i have 4220 rows and 20 columns. Is there a faster way to do this. NOTE: I am populating the datagridview from the saved excel file. I appreciate your help….my save code is as follows:

private void btnSave_Click(object sender, EventArgs e)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        // Get the Header from dataGridView
        for (int h = 1; h < dataGridView1.Columns.Count + 1; h++)
        {
            xlWorkSheet.Cells[1, h] = dataGridView1.Columns[h - 1].HeaderText;
        }

        // Get the Cell Values
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {
                DataGridViewCell cell = dataGridView1[j, i];
                xlWorkSheet.Cells[i + 2, j + 1] = cell.Value;
            }
        }

        //xlWorkBook.SaveCopyAs("\\FORM TEST.xlsx");
        xlWorkBook.SaveAs("\\GB STOCK.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        xlApp = null;
        xlWorkBook = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();      
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:13:29+00:00Added an answer on May 31, 2026 at 3:13 pm

    After having read through various searches and through the answers above i came across this code, it works extremely faster (almost instantaneous) as compared with my orginal where it took just under 2 minutes. I am very grateful for your answers above and i will look into these especially the copy and paste method which was an interesting read. I am a relatively new to this (new hobby) and am only beginning to understand some concepts with regards to exporting datasets etc.
    I know this is by no means the best way of acheiving what i set out to but it does what i want it to at the moment. Once again thanks to all who have helped, i am learning alot.

                int cols;
                //open file 
                StreamWriter wr = new StreamWriter("GB STOCK.csv", false, Encoding.UTF8);
    
                //determine the number of columns and write columns to file 
                cols = dgvStock.Columns.Count;
                for (int i = 0; i < cols; i++)
                    { 
                        wr.Write(dgvStock.Columns[i].Name.ToString().ToUpper() + ",");
                    } 
                wr.WriteLine();
    
                //write rows to excel file
                for (int i = 0; i < (dgvStock.Rows.Count); i++)
                    { 
                        for (int j = 0; j < cols; j++)
                            { 
                                if (dgvStock.Rows[i].Cells[j].Value != null)
                                    {
                                        wr.Write(dgvStock.Rows[i].Cells[j].Value + ",");
                                    }
                                else 
                                    {
                                        wr.Write(",");
                                    }
                            }
    
                        wr.WriteLine();
                    }
                    //close file
                    wr.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do a Schema Compare in Visual Studio 2010 on some
I am attempting to export mysql data into excel. I have tried the phpexcel
I am attempting to export data to a excel spreadsheet and so open that
I am attempting to export a query from MS Access into MS Excel in
My application is using MEF to export some classes from an external assembly. These
As a customer in Plesk, I am attempting to run: php -q /httpdocs/_external/export/test.php From
I am attempting to set up a simple Morse encoder using a hashmap in
I am attempting to write a C# application connecting to a Cognos TM1 datastore
Synopsis I am attempting to export a WAR to my webapps folder but everytiime
In attempting to compile ICU49 using Android NDKv7b, I ran into the following: /home/tim/icu49/icu/source/common/putil.cpp:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.