Sometime you have a .txt file and you want it to read and display on to your web page after proper formatting. This article provides one way to do this. This article then extends the functionality to provide the user option to save the file in .xls and word format
Reading .txt file into a dataset Assumption:- Records in your .txt file contains delimiter. I have a .txt file sample.txt (delimited by space or tab). I will read the file row by row and make a new dataset.
DataSet Text_To_Datasetst(string filePath) { StreamReader reader=new StreamReader(filePath); reader.BaseStream.Seek(0,SeekOrigin.Begin);
DataSet ds=new DataSet(); DataTable dt=new DataTable(); ds.Tables.Add("SampleTable");
// here the fields in the .txt file are double so i add datatypes // in the dataset columns accordingly. ds.Tables["SampleTable"].Columns.Add("Field1",System.Type.GetType("System.Double")); ds.Tables["SampleTable"].Columns.Add("Field1",System.Type.GetType("System.Double")); ds.Tables["SampleTable"].Columns.Add("Field1",System.Type.GetType("System.Double")); ds.Tables["SampleTable"].Columns.Add("Field1",System.Type.GetType("System.Double"));
dt=ds.Tables["SampleTable"];
string delimStr =" "; //Delimiter is space char [] delimiter = delimStr.ToCharArray(); while(reader.Peek() >-1) { int cnt=0; drow=dt.NewRow(); foreach( string strfields in reader.ReadLine().Split(delimiter)) { drow[cnt]=Convert.ToDouble(strfields.Trim()); cnt++; } dt.Rows.Add(drow); } return ds; }
Saving the dataset in .xls or .doc file
void Save_dataset(DataSet ds,int type) { if(type==1) //for .xls { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=FileName.xls"); Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/vnd.xls"; } else //for word { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=FileName_word.doc"); Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/vnd.word"; } System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); dg.RenderControl(htmlWrite); dg.DataSource=ds; dg.DataBind(); dg.RenderControl(htmlWrite); Response.Write(stringWrite.ToString()); Response.End(); }
This article covered the details of reading text file into a dataset and then saving the dataset in a excel or word file.
|
| Author: Gowtham Sen Potnuru 08 Mar 2006 | Member Level: Silver Points : 0 |
Hi friend,
Your article is good. I can't quit without appreciating you. You had explained regarding uploading data to Excel file. Its a good one. I submitted a article on this topic.
You can view here. http://dotnetspider.com/kb/Article1848.aspx
I hope this continue.
Thanks and Regards Gowtham Sen.
|
| Author: tvkswamy 10 May 2007 | Member Level: Bronze Points : 0 |
There is a an error when the Save_dataset is executed. It throws the following error.
Error 1 The name 'dg' does not exist in the current context
The word dg. is used which has no prior declaration.
I do not know how to fix it.
Regards,
tvks
|
| Author: Ian Beckett 15 Mar 2009 | Member Level: Bronze Points : 1 |
In the first "Text_To_Dataset" snippet, line 24:
drow = dt.NewRow();
should be:
DataRow drow = dt.NewRow();
|
| Author: Ian Beckett 15 Mar 2009 | Member Level: Bronze Points : 1 |
In the first "Text_To_Dataset" snippet, line 24:
drow = dt.NewRow();
should be:
DataRow drow = dt.NewRow();
|