Saving Data from Dataset to file
I am fairly new at programming in C#. I am using Visual C# 2008 Express, and I have a dataset that I would like to save/export the data to a file. I would like to have that data be saved to either a Text file, or an Excel file. I have not been able to locate how to do this and I completely lost. Any help would be greatly appreciated. This program is a christmas present to my mother who has become quite the collector.
Bechir BejaouiPosted Dec 16, 2008, 4:59 PM
Bechir BejaouiPosted Dec 20, 2008, 7:38 AM
Brandon CzarnockiPosted Dec 19, 2008, 4:19 PM
I appreciate all the help, I may tackle this a different way. But thanks for everything! Big help.
Brandon CzarnockiPosted Dec 15, 2008, 7:20 PM
Bechir BejaouiPosted Dec 15, 2008, 6:38 PM
Brandon CzarnockiPosted Dec 14, 2008, 5:46 PM
Bechir BejaouiPosted Dec 13, 2008, 6:17 PM
To export you data to an xml file
ds.WriteXml(@"C:\myFile");
To export data to a steam
first include
using System.IO;
FileStream oStream = new FileStream(path, FileMode.OpenOrCreate,FileAccess.Write);
ds.writexml(oStream);
to write your dataset content to a text file
using(TextWriter oWriter = new StreamWriter(@"C:\myfile.txt"))
{
ds.Writexml(oWriter);
oWriter.Flush();
}
to write your data set to Excel file
DataSet ds = new DataSet();
DataView oDataView1 = new DataView();
Excel.Application oExcel = new Excel.Application();
Excel.Workbook oWorkBook = new Excel.Workbook();
Excel.Worksheet oWorkSheet = new Excel.Worksheet();
int i = 0;
ds.ReadXml(@"C:\myfile.xml");
//initiate the Dataview from the dataset
oDataView1 = ds.DefaultViewManager.CreateDataView(ds.Tables(0));
//Lunch the excel
oExcel = new Excel.Application();
oExcel.Visible = false;
oWorkBook = oExcel.Workbooks.Add;
oWorkSheet = oWorkBook.ActiveSheet;
// Export Datas
oWorkSheet.Cells(1, 1) = "table from ds";
i = 2;
foreach (var dr in oDataView1) {
oWorkSheet.Cells(i, 1) = dr.Item("columnname");
i = i + 1;
}
//Save and close
oWorkBook.SaveAS(@"C:\myfile.xlsx");
oWorkBook.Close();
or you have this alternative
http://www.dotnetjohn.com/articles.aspx?articleid=36