Tuesday, September 8, 2009
.Net - write output text file
public Boolean WriteCSVFile()
{
try
{
SqlDataReader dr = this.GetTable("SELECT MtgName,CONVERT(VARCHAR(10),MtgBeginDate,101) as MtgBeginDate," +
"CONVERT(VARCHAR(10),MtgEndDate,101) as MtgEndDate," +
"MtgCityStZip,MtgLoc1,MtgLoc2,MtgLoc3,MeetingMinutesURL FROM MTGMain WHERE MtgId=@Id");
//if (!Directory.Exists(@"c:\magenda\"))
//{
// //Directory.CreateDirectory(@"c:\magenda\");
// Directory.CreateDirectory(@"c:\magenda\");
//}
//string outfile = @"c:\magenda\meeting.csv";
string outfile = @".\meeting.csv";
using (StreamWriter sw = File.CreateText(outfile))
{
while (dr.Read())
{
sw.WriteLine("Meeting: {0}, Begin Date: {1}, End Date: {2}, City State Zip: {3}," +
"Location 1 {4}, Location 2 {5}, Location 3 {6}, Meeting URL {7}",
dr["MtgName"].ToString(),
dr["MtgBeginDate"].ToString(),
dr["MtgEndDate"].ToString(),
dr["MtgCityStZip"].ToString(),
dr["MtgLoc1"].ToString(),
dr["MtgLoc2"].ToString(),
dr["MtgLoc3"].ToString(),
dr["MeetingMinutesURL"].ToString());
}
dr = this.GetTable("SELECT CONVERT(VARCHAR(10),DayDate,101) as DayDate," +
"StartTime,EndTime,Location FROM MTGDay WHERE MtgId=@Id");
sw.WriteLine("Meeting Day Times:");
while (dr.Read())
{
sw.WriteLine("Day: {0},Start: {1}, End: {2} Location {3}",
dr["DayDate"].ToString(),
dr["StartTime"].ToString(),
dr["EndTime"].ToString(),
dr["Location"].ToString());
}
sw.Close();
System.IO.FileInfo file = new System.IO.FileInfo(outfile);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.End();
}
return true;
}
catch
{
return false;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment