.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Creating Word Document

Posted By:Athira Sreejith       Posted Date: February 22, 2009    Points: 10    Category: ADO.NET    URL: http://www.dotnetspark.com  
 

Creating Word Document  in asp.net. Below is the sample code to create MS  Word file.

Creating Word Document :

lblView.Visible=true;

hlinkWord.Visible=true;(To click and view the file which was converted)

lblView.Text ="Pls Wait......";

String Header1="", Header2="", Footer1="", Footer2="", HyperText="", DownLoadFormat="";

DateTime datnow = DateTime.Now;(Getting Today's Date{1/10/2007})

string strnow = datnow.ToString("ddMyy_hhmmss"); ("10107_053540")

string sConn =ConfigurationSettings.AppSettings["ConnStr"];

SqlConnection objConn = new SqlConnection(sConn);

objConn.Open();

SqlDataReader drChapterHeader;

String strsql;

strsql="select * from ANC_Quest_Main where QID=" + QID.ToString() ;

(Retrieving the values depending upon the QID)

SqlCommand cmd=new SqlCommand(strsql,objConn);

drChapterHeader=cmd.ExecuteReader();

if (drChapterHeader.Read())

{

Header1=Convert.ToString(drChapterHeader["Question"]);

Header2=Convert.ToString(drChapterHeader["Answer"]);

HyperText = Convert.ToString(drChapterHeader["HyperText"]);

DownLoadFormat = Convert.ToString(drChapterHeader["DownLoadFormat"]);

Footer1="";

Footer2="";

}

drChapterHeader.Close();

public WordDoc.SaveWord wd;( Assign before the Page_Load Event)

wd=new WordDoc.SaveWordClass();

string strFileName = logid;(Getting the logid for the corresponding Q?)

string strHtmlFileName = strFileName;(Assigning Logid as FileName)

strFileName = Server.MapPath ("DocumentFiles\\whtml\\" + strFileName + ".html");(making a path where the files to be stored in which folder)

FileStream fsWriteHtml = new FileStream(strFileName,FileMode.OpenOrCreate,FileAccess.Write);

StreamWriter sw = new StreamWriter(fsWriteHtml);

sw.Write("");

sw.Write("

");

sw.Write("Dear User");

sw.Write("

Please find below answer to the query posted by you from the knowledgebase.

");

sw.Write("

");

sw.Write("

Question:");

sw.Write(Header1 +"

 

--------------------------------------------------------------------------------

");

sw.Write("Solution :");

sw.Write(Header2 +"

");

sw.Write("Reference File :");

sw.Write(DownLoadFormat +"

Reference URL :");

sw.Write(HyperText +"

");

sw.Write("

");

sw.Write("

");

sw.Write("

Thank you");

sw.Write("

Knowledgebase Manager");

sw.Write("

");

sw.Write("

");

sw.Close();

try

{

string s1,s2;

s1=Server.MapPath("DocumentFiles\\whtml\\"+ strHtmlFileName + ".html");

Sample path given below were the files are stored and with what filename (FileName is created by using corressponding question Logid)

(@"c:\inetpub\wwwroot\Ancilla\DocumentFiles\whtml\IAIAC-MC22-CP24-21.html")

s2=Server.MapPath("DocumentFiles\\wdoc\\" + strHtmlFileName + ".doc");

(@"c:\inetpub\wwwroot\Ancilla\DocumentFiles\wdoc\IAIAC-MC22-CP24-21.doc")

File.Copy (s1,s2,true);

wd=null;

lblView.Text ="Click here to View the Document";

hlinkWord.Text = strHtmlFileName + ".doc";

hlinkWord.NavigateUrl = s2;

(Here the S2 value is c:\inetpub\wwwroot\Ancilla\DocumentFiles\wdoc\IAIAC-MC22-CP24-21.doc)

string s3=Server.MapPath("DocumentFiles\\wdoc");

(Before Replacing the value of s3 is: @"c:\inetpub\wwwroot\Ancilla\DocumentFiles\wdoc")

s3=s3.Replace("\\","/");

(After Replacing the value of s3 is:

c:/inetpub/wwwroot/Ancilla/DocumentFiles/wdoc)

Response.Write("");

}

catch(Exception ex)

{

ex=ex;

wd=null;

}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend