Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export datagridview to excel 2007 not open by c#(open suddenly for second then close suddenly)

Status
Not open for further replies.

ahm1985

Programmer
Dec 6, 2012
138
EG

0I 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.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
 
Personally I use EPPlus for reading/writing Excel 2007 and above, it avoids all the Interop and Type.Missing stuff.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top