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

Posted by Ahmedsa under C# on 7/7/2014 | Points: 10 | Views : 2556 | Status : [Member] | Replies : 6
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




Responses

Posted by: Vuyiswamb on: 7/7/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Now the First place i will check , is the Clients "Windows Application Log"

It will show you the error. I know this Problem i once had the issue before. One other thing is that you are Catching the error but you are doing nothing with the error. MessageBox the Error so that you can see what the issue is.





Thank you for posting at Dotnetfunda
[Administrator]

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ahmedsa on: 7/8/2014 [Member] Starter | Points: 25

Up
0
Down
the message error attached with message
it found in this link
http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=337336

this message box i put in catch and it show message in some computeres
and these computeres have 2007 office and installed pia in it
when i press button2(export to excel)
it give me message in site above

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 7/8/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Show me a line of code in Line 84 of your code.



Thank you for posting at Dotnetfunda
[Administrator]

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ahmedsa on: 7/8/2014 [Member] Starter | Points: 25

Up
0
Down
code line 84
include
{ practise
and // coment line
or not


Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 7/8/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
send a screenshot i don't believe its there code that is there. send a Screenshot please

Thank you for posting at Dotnetfunda
[Administrator]

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ahmedsa on: 7/8/2014 [Member] Starter | Points: 25

Up
0
Down
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;



namespace FleetManagment
{
public partial class ShowBranches : Form
{
string value1;
string value2;
string value3;
string value4;
DataView dv;

public ShowBranches(string str1, string str2, string str3, string str4)
{
InitializeComponent();
value1 = str1;
value2 = str2;
value3 = str3;
value4 = str4;
}

private void ShowBranches_Load(object sender, EventArgs e)
{
label1.Text = Fleet.Branches;
int a;
FleetManagment.Fleet fleet = new FleetManagment.Fleet();
DataTable dt = fleet.ViewBranchesID("Data Source=" + value1 + ";Initial Catalog=" + value2 + ";User ID=" + value3 + ";Password=" + value4, Convert.ToInt32(label1.Text));
dv = dt.DefaultView;
if (dt.Rows.Count > 0)
{
a = Convert.ToInt32(dt.Rows.Count);
label1.Text = a.ToString();
dataGridView1.DataSource = dv;
dataGridView1.Refresh();
}
else
{
MessageBox.Show("No Records Found");
}
}

private void button1_Click(object sender, EventArgs e)
{
printDocument1.Print();

}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
e.Graphics.DrawImage(bm, 150, 150, 450, 350);
}
private void button2_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within 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);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
//Funny
app.Visible = true;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
try
{
//Fixed:(Microsoft.Office.Interop.Excel.Worksheet)
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from Ketoan";
// 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
string fileName = String.Empty;
//SaveFileDialog saveFileDialog1 = new SaveFileDialog();

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


if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = saveFileDialog1.FileName;
//Fixed-old code :11 para->add 1:Type.Missing
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;

// Exit from the application
//app.Quit();
}
catch (System.Exception ex)
{
MessageBox.Show("The error is " + ex);
}
finally
{
app.Quit();
workbook = null;
app = null;
}
}
}
}

This above my code all lines with practise and without comments line
line 84 without comment line is


saveFileDialog1.RestoreDirectory = true;

Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response