Monday, June 1, 2009

Create word document using FileStream

Hello guys

U can create word document using FileStrem in C#.Net.

using System.IO;
using System.Text;

Public void CreateWordDoc()
{
string strFileName;
strFileName = Guid.NewGuid().ToString() + ".doc";
string strFilePath = Server.MapPath("NewFile/") + strFileName;
FileStream f = new FileStream(strFilePath, FileMode.Create);
StreamWriter s = new StreamWriter(f);


HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";

HttpContext.Current.Response.ContentType = "application/msword";


HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);

StringBuilder strHTMLContent = new StringBuilder();

HttpContext.Current.Response.ContentType = "application/msword";


HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);
HttpContext.Current.Response.Charset = "";

System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

tbmMain.RenderControl(hw); //Where dv is div holding img with run at server
HttpContext.Current.Response.Write(tw.ToString());

// create a write stream

// write to the stream
string str=tw.ToString();
s.WriteLine(str);
s.Close();
f.Close();

}
Thanks & Regards
Jignesh Patel

No comments:

Post a Comment