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

时间:2022-09-20 09:48:55

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.

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

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.

然而,尽管它生成了单词doc,但是这些图像是在文档中嵌入的,而是将它们作为链接放置。我已经找到了一种方法来做到这一点,但还没有找到真正有用的东西。

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

我很感激我能得到的任何帮助,因为这是“最后一分钟”的要求(谈论典型的)

Thanks

1 个解决方案

#1


6  

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

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

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.

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

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.

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

#1


6  

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

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

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.

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

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.

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