Click here to Skip to main content
15,917,321 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have exported the gridview value to an excel file.But when i tried it to save using a save dialog box,it's not working,but no error is showing.Kindly rectify my mistake.The code i am using is given below.

C#
private void button2_Click(object sender, EventArgs e)
       {

           string sd;
           saveFileDialog1.ShowDialog();
           saveFileDialog1.InitialDirectory = "c:";
           saveFileDialog1.FileName = "";
           saveFileDialog1.Filter = "Excel File|*.xls|All Files|*.*";







           Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
           ExcelApp.Application.Workbooks.Add(Type.Missing);


           ExcelApp.Columns.ColumnWidth = 20;


           for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
           {
               ExcelApp.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++)
               {
                   ExcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
               }
           }


           sd = saveFileDialog1.FileName;
           ExcelApp.ActiveWorkbook.SaveCopyAs(sd);
           ExcelApp.ActiveWorkbook.Saved = true;
           ExcelApp.Quit();
           MessageBox.Show("Excel file created");
       }
Posted
Updated 15-Mar-18 23:39pm
Comments
Christian Graus 4-Feb-12 8:02am    
This looks logical to me. What does 'not working' mean, do you get any file saved at all ? Do you need to set which workbook is 'active' ? Does SaveCopyAs return anything ?
sreenathpktr 4-Feb-12 8:06am    
not working means,if i selected the c: drive and saved,and the file is not there.But no error is showing.
Christian Graus 4-Feb-12 8:07am    
OK - then can you check in debugger or code what the ActiveWorkbook is ? Does SaveCopyAs return any value for success, or is there a property you can check to find out what went wrong ? Is SaveCopyAs the only save method you can call ?
sreenathpktr 4-Feb-12 8:10am    
I have tried some other save methods,but shows some errors.When i tried without the savedialogbox,the file is created.

Why not use SaveAs[^] instead.

Also using debugger check that you have given a valid path and file name to sd variable.

You can also wire BeforeSave[^] to see what happens before the save operation.
 
Share this answer
 
v2
C#
private void button2_Click(object sender, EventArgs e)
       {

           string sd;
           saveFileDialog1.ShowDialog();
           saveFileDialog1.InitialDirectory = "c:";

           saveFileDialog1.Filter = "Excel File|*.xls|All Files|*.*";







           Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
           ExcelApp.Application.Workbooks.Add(Type.Missing);


           ExcelApp.Columns.ColumnWidth = 20;


           for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
           {
               ExcelApp.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++)
               {
                   ExcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
               }
           }


           sd = saveFileDialog1.FileName;

           ExcelApp.ActiveWorkbook.SaveCopyAs(sd+".xls");
           ExcelApp.ActiveWorkbook.Saved = true;
           ExcelApp.Quit();
           MessageBox.Show("Excel file created");
       }
 
Share this answer
 
Comments
Richard MacCutchan 4-Feb-12 8:31am    
Does this mean it works now that you are not clearing the filename, or is it still failing?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900