用C#中的图像动态创建word文档? [英] Dynamically create word document with images in C# ?

查看:135
本文介绍了用C#中的图像动态创建word文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从服务器文件夹我有图像,我需要绑定word文档中的图像。在该文件路径中,我直接给予图像控件,它完全是一个例外不支持URI文件格式。我试着给它的预览图像的文件路径...



我尝试过:



我正在尝试创建.doc文件并声明sting构建器并附加图像控件,并在源代码中提供文件路径url,在本地工作正常,一旦部署在服务器中它返回不支持URI文件格式。

from server folder i have images , i need to bind images in word document .and in that file path i am directly given to the image control its thorough an exception "URI file format not supported ." and i am try to given that file path to chrome its preview image ...

What I have tried:

am trying to create .doc file and declare sting builder and append image control and with in source giving an file path url , with in the local its working fine once deployed in server its return "URI file format not supported ".

推荐答案

从服务器创建Office文件时,使用OpenXML库是MicroSoft官方支持的方案(不建议使用InterOp)在那个场景中)。



OpenXML是一个相当迟钝的库,当你开始使用它时并不是非常容易使用。但是,网上有大量样本。一个好的起点是官方文档:



如何:打开文本并将其添加到文字处理文档(Open XML SDK) [ ^ ]



如何:将图片插入文字处理文档(Open XML SDK) [ ^ ]
When creating Office files from a server, using the OpenXML library is the officially supported scenario by MicroSoft (InterOp is not recommended by them in that scenario).

OpenXML is a fairly obtuse library and not terribly easy to use when you're just getting started with it. There are, however, a large number of samples available on the web. A good place to start is from the official documentation:

How to: Open and add text to a word processing document (Open XML SDK)[^]

How to: Insert a picture into a word processing document (Open XML SDK)[^]


添加到解决方案1:



我已经构建了一个类来执行或多或少的描述:带有图像的自定义文档。我是数据库驱动的,因此DBA可以在不学习如何的情况下创建文档。我碰巧使用php / javascript,但这并不重要。



这就是 - 有助于解决你的问题。



使用Word创建一个非常简单的文档,例如hello world,并将其另存为XML(其中一个选项)。现在,在文本编辑器中打开它,看看发生了什么。



你用一个非常简单的小图形做同样的事情,看看它们是如何存储的。我建议一个新文档,因为编辑标记(用于撤消等)被保存到一定程度并且会混淆分析。只需将你好世界改为你好世界,你就会发现不仅仅是改变了这封信。



所有的标签等等,还有一个其中很多都是以可控的方式显示的。为了作弊,我将页眉/页脚信息复制到文件中,并在构建文档时适当插入它们。请注意,生成的XML会有变体,具体取决于您拥有的Word版本。



您会发现这些页面生成速度非常快,并且通常比Word用于保存文件的常规二进制格式。
Adding to solution 1:

I have built a class to do more or less what you describe: custom documents with images. Mine is database driven so DBA's can create documents without learning how to. I happened to use php/javascript, but that's not important.

Here's what is - in terms of being helpful to solving your problem.

Create a very simple document with Word, like "hello world", and save it as XML (one of the options). Now, open this in a text editor and see what's going on.

You do the same with a very simple and small graphic and see how those are stored. I suggest a new document as the editing markup (for undo, etc.) is saved to an extent and will confuse the analysis. Simply change "hello world" to "Hello world" and you'll observe more than just the letter was changed.

All the tags and such, and there are a lot of them, are displayed in a controllable manner. To cheat, I copied the header/footer info to files and insert them appropriately when building the document. Note that the XML generated comes in variants, depending upon the version of Word you have.

You will find these pages are generated very quickly and are usually much smaller than the normal binary format that Word uses for saving files.


这篇关于用C#中的图像动态创建word文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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