ahmed sa

ahmed sa

  • NA
  • 289
  • 129k

Export datagridview to excel 2007 suddenly close after secon

Jul 7 2014 7:30 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




Answers (2)