如何将图像嵌入 ASP.NET 生成的 Word 文件中

发布于 2024-07-10 00:40:52 字数 697 浏览 5 评论 0原文

我有一个很常见的问题,正如我在各个用户组中看到的那样,但找不到合适的答案。

我想要做的是在我的网站中生成一个 ASP.NET 页面,该页面可以选择导出为 Microsoft Word .doc 格式。

我使用的方法是这样的:

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);
Page.RenderControl(htmlWrite);
Response.Write(sw.ToString());
Response.End();

然而,尽管它生成了一个word文档,但图像是嵌入在文档中的,而不是作为链接放置的。 我一直在寻找一种方法来做到这一点,但还没有找到真正有效的方法。

我将不胜感激我能得到的任何帮助,因为这是“最后一刻”的要求 (谈谈典型)

谢谢

I have a quite common problem, as I saw in the various user groups but could not find a suitable answer.

What I want to do is generate an ASP.NET page in my website which will have the option of being exported into Microsoft Word .doc format.

The method I have used is this:

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);
Page.RenderControl(htmlWrite);
Response.Write(sw.ToString());
Response.End();

However this eventhough it generates a word doc, the images are note embedded in the document, rather they are placed as links. I have looked for a way to do this, but have not found something that actually worked.

I would appreciate any help I can get, since this as "last minute" requirement
(talk about typical)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燃情 2024-07-17 00:40:52

简短回答:您需要提供页面中图像来源的绝对 URL。

更长的答案:

如果您使用 *.doc 扩展名重命名 HTML 文档,Microsoft Word 将打开该文档。 这就是您提供的代码正在执行的操作。 在这种情况下,图像不会像您以实际 Word 格式创建文档那样嵌入到文档中。 如果您的图像使用相对 URL,则 Word 将不知道在哪里查找它们,因此需要绝对 URL。

注意:这意味着任何在没有互联网连接的情况下查看文档的人都不会看到图像,因为每次打开文档时都会向服务器请求图像。

更优雅的解决方案是创建文档以真正的Word格式。 一个很棒的库是 Aspose.Words。 使用此库,您可以将图像直接嵌入到文档中,这样它们就不会依赖服务器。

Short Answer: You need to provide absolute URLs for the source of the images in your page.

Longer Answer:

Microsoft Word will open an HTML document if you rename it with a *.doc extension. This is what the code that you provided is doing. In this case, the images are not embedded in the document like they would be if you created a document in actual Word format. If your images are using relative URLs then Word will not know where to look for them, hence the need for absolute URLs.

NOTE: This means that anyone viewing the document without an internet connection will not see the images, as they are requested from the server every time the document is opened.

A more elegant solution would be to create the document in the real Word format. A great library for this is Aspose.Words. Using this library you would be able to embed the images directly into the document so that they do not rely on the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文