C# 在ASP.NET中将HTML内容写入Word文档时出现问题
我正在尝试将HTML页面内容导出到Word 我的Html显示页面是:C# 在ASP.NET中将HTML内容写入Word文档时出现问题,c#,asp.net,ms-word,export,C#,Asp.net,Ms Word,Export,我正在尝试将HTML页面内容导出到Word 我的Html显示页面是: 你最喜欢什么颜色 NA 列出前三名学校 一个国家 两名开发人员 三个p 和单击事件的按钮。按钮单击事件将打开MS word并将页面内容粘贴到word中 word页面包含html设计页面的表属性。它只出现在Word 2003中。但是在Word2007中,word文档包含的文本没有表属性。如何在word 2003中删除此表属性 我无法添加快照。否则我会告诉你的 我正在用aspx设计网页。我通过以下代码导出网页内容 protecte
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();
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
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.渲染控制(htmlTW);
字符串strBody=“”+
“”+“”+htmlTW.InnerWriter.ToString()+“”+
"" +
"";
响应。附录标题(“内容类型”、“应用程序/msword”);
Response.AppendHeader(“内容处置”、“附件;文件名=“+filename”);
Response.ContentEncoding=System.Text.Encoding.UTF7;
字符串fileName1=“C://Temp/Excel”+DateTime.Now.millis秒.ToString();
BinaryWriter=newBinaryWriter(File.Open(fileName1,FileMode.Create));
作家,作家(斯特博迪);
writer.Close();
FileStream fs=newfilestream(fileName1,FileMode.Open,FileAccess.Read);
字节[]渲染字节;
//创建文件流长度的字节数组
renderdbytes=新字节[fs.Length];
//将字节块从流读入字节数组
读取(renderdbytes,0,System.Convert.ToInt32(fs.Length));
//关闭文件流
fs.Close();
FileInfo TheFile=新的FileInfo(fileName1);
如果(文件存在)
{
File.Delete(fileName1);
}
BinaryWrite(renderBytes);
Response.Flush();
Response.End();
}
问题不清楚
这些链接可能有助于您理解MSWord-C自动化:
您还可以尝试创建一个开放的XML文档,该文档现在已被MS office识别。 以下是代码示例的更多信息:
您正在编写HTML,声称它的内容类型为“application/msword”,然后希望是最好的 有更多“正确”的方法来实现你的目标 目前有一些将(X)HTML转换为WordML内容的项目,其中之一就是。披露:我认为;你可以在StackOverflow上找到其他地方的链接 或者,您也可以使用Word的altChunk机制,不过请注意:
- 您对如何执行导入的控制较少李>
- Word 2003不支持AltChunk(即使使用兼容包)
相反,您可能希望使用Office互操作(Microsoft.Office.Interop.Word)或使用nuget安装DocX。在线查看一些示例,搜索“C#创建word文档”。很难理解您正在做什么或您的问题是什么。请尝试编辑问题,以便我们了解您试图实现的目标。谢谢我无法添加快照。很抱歉这么说。你能告诉我你需要更多了解的信息是什么吗?我试着猜你在做什么:你用C程序(Windows应用程序)抓取一个网页。然后将其“导出”到Word文档?或者您正在谈论的是ASP.NET应用程序?不清楚,是的,斯莱特。你是对的。我正在将网页内容导出到word。您在ASP.NET或Windows窗体应用程序中使用C#吗?还是在web浏览器中“手动”执行?