C# .NET - Export datagridview to excel 2007 not open by c#(open suddenly for second then close sudde

Asked By ahmed elbarbary on 07-Jul-14 08:44 AM

I make datagridview to show data then export to excel 2007

in my computer it work ok no proplem

when i open exe file in another computer and press button export datagridview to excel

excel sheet open for one second  and close suddenly and another computeres have office 2007

and my computer also have 2007 and it open and in another computeres not open and have 2007

this is my code

using   

System;

using

  System.Collections.Generic;

using

  System.ComponentModel;

using

System.Data;

using

System.Drawing;

using

System.Linq;

using

System.Text;

using

System.Windows.Forms;

using

System.Data.SqlClient;

using

Excel = Microsoft.Office.Interop.Excel;

 Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
 System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
 System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
 Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
 Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
 app.Visible = true;
      
try
{
         
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
worksheet.Name = "Exported from Ketoan";
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();
 }
 }

         
string fileName = String.Empty;
saveFileDialog1.Filter = "Excel files |*.xlsx|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
 {
fileName = saveFileDialog1.FileName;
           
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, Type.Missing);

}
else
return;

         
}
catch (System.Exception ex)
{

}
finally
{
app.Quit();
workbook = null;
app = null;
}

 the code working for me in my computer

but my question

Why it open and close suddenly in another computerES and not show data

And how to solve this proplem

My proplem happen in more computeres not one computers all have office 2007



Robbe Morris replied to ahmed elbarbary on 07-Jul-14 09:45 AM
How about putting in a message box to show the error in your catch statement instead of just swallowing the exception?
ahmed elbarbary replied to Robbe Morris on 08-Jul-14 05:37 AM
http://www.dotnetspider.com/forum/337336-Export-datagridview-to-excel-2007-not-open-by-c-open-suddenly-for-second.aspx
this link above contain messagebox after i press button2(export to excel)
message in site above show when i put message box in catch and then press export datagridview to excel
why this message box show and how to solve