saveFileDialog and export datagridviw to excel

ErikVest
ErikVest used Ask the Experts™
on
Hi.

I've found some code on "tha web" that exports the datagridview to excel. But the author has hard coded in the path to where he want to save the file to. I've tried to use saveFileDialog to get the path and filename where to save the excel file. But i cant get it to work. Can anyone help me?

I've attavhed the original code for exporting datagridview to excel. Could anyone give me an exampel with this code on how to use saveFileDialog with this code?

Thanx.


private void button1_Click_1(object sender, EventArgs e)
        {
 
            // creating Excel Application
            Microsoft.Office.Interop.Excel._Application app  = new Microsoft.Office.Interop.Excel.Application();
 
 
            // creating new WorkBook within Excel application
            Microsoft.Office.Interop.Excel._Workbook workbook =  app.Workbooks.Add(Type.Missing);
           
 
            // creating new Excelsheet in workbook
             Microsoft.Office.Interop.Excel._Worksheet worksheet = null;                   
           
           // see the excel sheet behind the program
            app.Visible = true;
          
           // get the reference of first sheet. By default its name is Sheet1.
           // store its reference to worksheet
            worksheet = workbook.Sheets["Sheet1"];
            worksheet = workbook.ActiveSheet;
 
            // changing the name of active sheet
            worksheet.Name = "Exported from gridview";
 
           
            // storing header part in Excel
            for(int i=1;i<dataGridView1.Columns.Count+1;i++)
            {
    worksheet.Cells[1, i] = dataGridView1.Columns[i-1].HeaderText;
            }
 
 
 
            // storing Each row and column value to excel sheet
            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();
                }
            }
 
 
            // save the application
            workbook.SaveAs("c:\\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);
           
            // Exit from the application
          app.Quit();
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
btw, here is the link to the author: http://www.c-sharpcorner.com/UploadFile/hrojasara/2904/
Senior Software Engineer
Top Expert 2009
Commented:
Ad the following code at the top of your handler:

           string fileName = String.Empty;
           SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "xls files (*.xlst)|*.txt|All files (*.*)|*.*" ;
            saveFileDialog1.FilterIndex = 2 ;
            saveFileDialog1.RestoreDirectory = true ;

            if(saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                  fileName =   saveFileDialog1.FileName ;
            }
            else
               return;

Then change line 46 to:
workbook.SaveAs(fileName ,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive , Type.Missing, Type.Missing, Type.Missing, Type.Missing);
           

Author

Commented:
Hi.

Great. It works. Thnx.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial