C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Reading text file into a dataset and saving the dataset in Excel and word file


Posted Date: 22 Feb 2006    Resource Type: Articles    Category: .NET Framework
Author: anuragMember Level: Bronze    
Rating: 1 out of 5Points: 7



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.






Responses

Author: Gowtham Sen Potnuru    08 Mar 2006Member 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 2007Member 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 2009Member 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 2009Member Level: Bronze   Points : 1
In the first "Text_To_Dataset" snippet, line 24:

drow = dt.NewRow();

should be:

DataRow drow = dt.NewRow();


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Understanding IDisposable pattern
Previous Resource: 13 Years of Microsoft Certifications---Part1
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use