Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to export richtexteditor textbox values(images) to Ms word.. ?
kindly help......im trying to the below the sample code...im geting only text,,and images are not came...

aspx
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" ValidateRequest = "false"  EnableEventValidation="false" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>TinyMCE ASP.Net Example</title>

<script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
   tinyMCE.init({
        mode : "textareas",
        theme : "advanced",
        plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",
        theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor, insertimage",
        theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : false,
        template_external_list_url : "js/template_list.js",
        external_link_list_url : "js/link_list.js",
        external_image_list_url : "js/image_list.js",
        media_external_list_url : "js/media_list.js",
        height:300
    });
</script>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <asp:Panel ID = "pnlEditor" runat = "server" >
        <asp:TextBox ID="RichTextBox" runat="server" TextMode = "MultiLine" ><br />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
        
        <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" Visible = "false" OnClick="btnCancel_Click" />            
        <asp:Label ID="lblDisplay" runat="server" Text="" Visible = "false" >

        <asp:Button ID="btn" runat="server" OnClick="GenerateMsWordDoc" Text="Generate Ms Word Document" />

     
    </div>
</form>
</body>
</html>


aspx.cs
C#
public partial class _Default : System.Web.UI.Page
{
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        lblDisplay.Visible = true;
        pnlEditor.Visible = false;
        lblDisplay.Text = RichTextBox.Text;
        btnSave.Visible = false;
        btnCancel.Visible = true;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        lblDisplay.Visible = false;
        pnlEditor.Visible = true;
        lblDisplay.Text = "";
        RichTextBox.Text = "";
        btnSave.Visible = true;
        btnCancel.Visible = false;
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
            string FilePath = "images/" + FileName;
            FileUpload1.SaveAs(Server.MapPath(FilePath));
            RichTextBox.Text += string.Format("<img src = '{0}' alt = '{1}' />", FilePath, FileName);
        }
    }
    
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }
    protected void GenerateMsWordDoc(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=Test.doc");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/msword";

        StringWriter sw = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(sw);
        lblDisplay.RenderControl(htmlWrite);

        Response.Write(sw.ToString());
        Response.End();
    }
  
}
Posted
Updated 15-Jun-15 1:30am
v5
Comments
Andy Lanng 15-Jun-15 4:25am    
Please use the yellow 'code' button above the textarea to wrap your code sections. It automatically format the code indents and colours keywords. It makes it a lot easier to read so you will be more likely to get an answer.
I have added the 'code' tags for you. You can click "Improve Question" to see where the tags have been added to your original post
aarif moh shaikh 15-Jun-15 4:58am    
what you got error massage..
venkatesh (chennai) 15-Jun-15 5:07am    
not to be error....when i export the textbox values for example.,text,images...
they came the Ms word only text....images are not came....that is my question why images are not came in ms word to export....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900