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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Grid & Excel

Status
Not open for further replies.

JamesHanley

Programmer
Jun 16, 2006
57
0
0
US
I am trying to export an entire datagrid as a whole to excel. I have tried so many different things, but they dont seem to work. Below is my only button on my project and it takes recovered information and puts it on the data grid. How can i get this to work. just click and it makes a .xls file with all the data on it. no cell by cell stuff. too much work.

private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Runing Report...This May Take Several Minutes.");
DataSet lstDataSet = new DataSet();
sla.HRDataSet(lstDataSet, cboApps.Text);
dataGrid1.DataSource = lstDataSet.Tables[0];
}
 
You can do that by creating an Excel application object, an workbook and WorkSheet, then iterate through all rows and populate the WorkSheet with the desired columns.
Without using Excel object you can try the following solutions:
Code:
lstDataSet.WriteXml("myfile.xls");
That line will create "myfile.xls" which is xml but it can be open very well with Excel and you see DataSet columns and their data.
When you choose Excel to open the myfile.xls , it detects that this file has no schema definition and select "As an XML list" option.
-obislavu-
 
Awesome lstDataSet.WriteXML("myfile.xls"); worked perfect

Is there a way i can tell it to save to a shared network locaion?

\\fs-is\isfiles\ccc\licensing\Software Licensing Application Reporting Tool\

I wish I could just save it to E:\ or something but everyone is mapped differently.

Is this possible?

Brent Serio
Just Born Designs
Email: brentaserio@cox.net
Aim: CsCheerios
 
Never Mind I just added "@\\location\" infront of the file name.

EG.

lstDataSet.WriteXML("@\location\folder\folder\" + cboApps.Text + ".xls");

worked beautifully.



Brent Serio
Just Born Designs
Email: brentaserio@cox.net
Aim: CsCheerios
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top