Skip to content

Export datagridview in a windows application to excel in C#

Fathy Geaiessah

Asked Jun 15, 2013, 5:45 AM EDT1 answer
Share
LinkedInX

 I have a code that export a datagridview content in a windows application to excel but it doesn't enable the user to name and locate the created excel file, i want to modify the code to enable the user to name and locate the created excel file using SaveFileDialog, here is the code
button2 in code is the export to excel button,

Note: the application is a windows application, i have posted this question but there is a problem in reply link, and i couldn't continue replying the question.

Code
private void button2_Click(object sender, EventArgs e)

        {

            Excel.
Application xlApp;

Excel.

Code
Workbook xlWorkBook;

            Excel.
Worksheet xlWorksheet;


object misValue = System.Reflection.Missing.Value;

xlApp =

Code
new Excel.ApplicationClass();

            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorksheet = (Excel.
Worksheet)xlWorkBook.Worksheets.get_Item(1);


int i = 0;


int j = 0;


for (i = 0; i <= dataGridView1.RowCount - 1; i++)

            {


for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)

                {


DataGridViewCell cell = dataGridView1[j, i];

                    xlWorksheet.Cells[i+1, j+1] = cell.Value;

                }

            }

            xlWorkBook.SaveAs(
"Products.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

xlWorkBook.Close(

TypeScript
true, misValue, misValue);

            xlApp.Quit();

            releaseobject(xlWorksheet);

            releaseobject(xlWorkBook);

            releaseobject(xlApp);


MessageBox.Show("Excel file is created successfully");



        }


private void releaseobject(object obj)

        {


try

            {

                System.Runtime.InteropServices.
Marshal.ReleaseComObject(obj);

obj =

Code
null;

            }


catch (Exception ex)

            {

                obj =
null;


MessageBox.Show("Exception Occured while releasing object " + ex.ToString());

            }


finally

            {


GC.Collect();

            }

        }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Sunny  Sharma
    0
    Hi Fathy,

    Just add these codes at the start of Button Click event code as:
    ----------------------------------------------------------------

    private void button2_Click(object sender, EventArgs e)

    {

    string fileSavePath = Environment.CurrentDirectory+"\\DefaultFileName.xls";
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "*.xls|*.xls";
    DialogResult dr = ofd.ShowDialog();
    if (dr == DialogResult.OK)
    {
          fileSavePath = ofd.FileName;
    }
    //your rest of the code goes here
    .........................
    .........................
    .........................
    //Replace "Product.xls" with fileSavePath.


    xlWorkBook.SaveAs(fileSavePath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);


    //Rest Of the code

    }  

    -----------------------------------------------
    If user enters any file name then file name is replaced with that otherwise default file name will be used. If you want to stop/exit if no file name is given then just add a return in else block after if like:

    if(dr==DialogResult.OK)

    {

      // .....

    }

    else

    {

            return;

    }

    Happy Coding :)


    Do mark this as answer if it helps.


    Thanks.

    0