问题ASP.NET编写HTML内容的Word文档 [英] Problem writing HTML content to Word document in ASP.NET

查看:96
本文介绍了问题ASP.NET编写HTML内容的Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想HTML页面内容导出到Word中。

我的HTML页面显示是:


  1. 什么是你最喜欢的颜色?

NA


  1. 榜前三位的学校?

一个国家
2离散事件
三个P

和点击事件的按钮。按钮单击事件将打开MS Word和Word中粘贴网页内容。

字页面包含HTML页面设计的表属性。它仅发生在Word 2003中但在2007字word文档包含了表属性的文本。我怎样才能删除这个字表属性2003

我无法添加快照。否则我会让你清楚。

我对ASPX设计网页。我出口由以下code中的网页内容。

 保护无效的button1_Click(对象发件人,EventArgs的发送)
{    Response.ContentEncoding = System.Text.Encoding.UTF7;
    System.Text.StringBuilder SB =新System.Text.StringBuilder();
    System.IO.StringWriter SW =新System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlTW =新System.Web.UI.HtmlTextWriter(SW);
    tbl.RenderControl(htmlTW);
    字符串strBody =< HTML和GT; +
        <身体GT; +< D​​IV>< B>中+ htmlTW.InnerWriter.ToString()+< / B>< / DIV>中+
        < /身体GT; +
        < / HTML>中;    Response.AppendHeader(内容类型,应用程序/ msword);
    Response.AppendHeader(内容处置,附件;文件名=+文件名);
    Response.ContentEncoding = System.Text.Encoding.UTF7;    字符串FILENAME1 =C://温度/ Excel的+ DateTime.Now.Millisecond.ToString();
    的BinaryWriter作家=新的BinaryWriter(File.Open(文件名,FileMode.Create));
    writer.Write(strBody);
    writer.Close();
    的FileStream FS =新的FileStream(文件名,FileMode.Open,FileAccess.Read);
    字节[] renderedBytes;
    //创建文件流长度的字节数组
    renderedBytes =新的字节[fs.Length]
    从流的字节//读取块入字节数组
    fs.Read(renderedBytes,0,System.Convert.ToInt32(fs.Length));
    //关闭文件流
    fs.Close();
    FileInfo的TheFile =新的FileInfo(文件名);
    如果(TheFile.Exists)
    {
        File.Delete(文件名);
    }
    Response.BinaryWrite(renderedBytes);    Response.Flush();
    到Response.End();
}


解决方案

您正在编写HTML,声称它是内容类型应用程序/ msword,那么最好的希望。

有更正确的方式来实现你的目标。

有几个项目围绕转换(X)HTML到WordML中的内容,其中的 docx4j-ImportXHTML.NET 之一。披露:我认为;你可以找到链接到其他人在这里StackOverflow的。

另外,你可以使用Word的altChunk机制,但请注意:


  • 您有超过如何执行进口较少的控制;

  • AltChunk不是由Word 2003(甚至与兼容包)的支持。

I am trying to export the HTML page contents to Word.

My Html display page is:

  1. What is your favourite color?

NA

  1. List the top three school ?

one National two Devs three PS

And a button for click event. The button click event will open MS word and paste the page contents in word.

The word page contains the table property of html design page. It occurs only in Word 2003. But in word 2007 the word document contains the text with out table property. How can I remove this table property in word 2003.

I am not able to add the snapshots. Else i will make you clear.

I am designing the web page by aspx. I am exporting the web page content by the following code.

protected void Button1_Click(object sender, EventArgs e)
{

    Response.ContentEncoding = System.Text.Encoding.UTF7;
    System.Text.StringBuilder SB = new System.Text.StringBuilder();
    System.IO.StringWriter SW = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlTW = new System.Web.UI.HtmlTextWriter(SW);
    tbl.RenderControl(htmlTW);
    string strBody = "<html>" +
        "<body>" + "<div><b>" + htmlTW.InnerWriter.ToString() + "</b></div>" +
        "</body>" +
        "</html>";

    Response.AppendHeader("Content-Type", "application/msword");
    Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
    Response.ContentEncoding = System.Text.Encoding.UTF7;

    string fileName1 = "C://Temp/Excel" + DateTime.Now.Millisecond.ToString();
    BinaryWriter writer = new BinaryWriter(File.Open(fileName1, FileMode.Create));
    writer.Write(strBody);
    writer.Close();
    FileStream fs = new FileStream(fileName1, FileMode.Open, FileAccess.Read);
    byte[] renderedBytes;
    // Create a byte array of file stream length 
    renderedBytes = new byte[fs.Length];
    //Read block of bytes from stream into the byte array 
    fs.Read(renderedBytes, 0, System.Convert.ToInt32(fs.Length));
    //Close the File Stream 
    fs.Close();
    FileInfo TheFile = new FileInfo(fileName1);
    if (TheFile.Exists)
    {
        File.Delete(fileName1);
    }
    Response.BinaryWrite(renderedBytes);

    Response.Flush();
    Response.End();
}

解决方案

You are writing HTML, claiming it is of content type "application/msword", then hoping for the best..

There are more "correct" ways to achieve your objective.

There are a few projects around for converting (X)HTML to WordML content, of which docx4j-ImportXHTML.NET is one. Disclosure: I maintain that; you can find links to others elsewhere here on StackOverflow.

Alternatively, you can use Word's altChunk mechanism, though note:

  • you have less control over how the import is performed;
  • AltChunk isn't supported by Word 2003 (even with the compatibility pack).

这篇关于问题ASP.NET编写HTML内容的Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆