CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2002
    Location
    India (Delhi)
    Posts
    199

    Thumbs down create a word doc file with formatting from asp.net and C#

    Hi

    In my application i need to format some accroding to the user requirement like some text must be in bold italics etc and then save that text into a word document.

    is there any way to do this.
    currently i m it like this
    on my page i create a iframe and target to a html page so my text is in the html and to format the text im using javascript functions and then writing the text in a html file through streamwriter.
    Code:
    protected System.Web.UI.HtmlControls.HtmlInputHidden hdnmsg;
    	
    		private void Page_Load(object sender, System.EventArgs e)
    		{
    			// Put user code to initialize the page here
    			Button3.Attributes.Add("onClick", "javascript:fillTxt();");
    		}
    
    		#region Web Form Designer generated code
    		override protected void OnInit(EventArgs e)
    		{
    			//
    			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
    			//
    			InitializeComponent();
    			base.OnInit(e);
    		}
    		
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{    
    			//this.Button1.Click += new System.EventHandler(this.Button1_Click);
    			this.Button3.Click += new System.EventHandler(this.Button3_Click);
    			this.Load += new System.EventHandler(this.Page_Load);
    
    		}
    		#endregion
    
    		private void Button3_Click(object sender, System.EventArgs e)
    		{
    			FileStream fs=new FileStream(Server.MapPath(".")+"\\new.html",FileMode.OpenOrCreate);
    			StreamWriter sw=new StreamWriter(fs);
    			sw.Write(hdnmsg.Value);
    			sw.Flush();
    			sw.Close();
    			
    		
    		}
    if i save the file as .doc then the formatting ive set on the text is not available in the doc file.
    Keep Posting in this Forum

  2. #2
    Join Date
    Aug 2015
    Posts
    3

    Re: create a word doc file with formatting from asp.net and C#

    Hi, do you mind to use a web component for your working? If it works for you, you can use aceoffix. It can call Ms office to web page and save Word document as html. The content in the Word document can be edit just as what you can do in Word.

  3. #3
    Join Date
    Apr 2014
    Posts
    23

    Re: create a word doc file with formatting from asp.net and C#

    Try following sample code to create Word file and format the font in C#.


    Code:
    using System.Drawing;
    using Spire.Doc;
    using Spire.Doc.Documents;
    
    namespace CreateWordWithFormatting
    {
        class Progarm
        {
            static void Main(string[] args)
            {
                //Create a word document
                Document doc = new Document();
                Section section = doc.AddSection();
    
                //Add a paragraph
                Paragraph Para = section.AddParagraph();
                //Append some text
                Para.AppendText("you text")
    
                //Set Font Style and Size
                ParagraphStyle style = new ParagraphStyle(doc);
                style.Name = "FontStyle";
                style.CharacterFormat.FontName = "Century Gothic";
                style.CharacterFormat.FontSize = 20;
                style.CharacterFormat.Bold = true;
                style.CharacterFormat.Italic = true;
                doc.Styles.Add(style);
                Para.ApplyStyle(style.Name);
    
                //Save the file
                document.SaveToFile("font.docx", FileFormat.Docx2010);
            }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured