使用OpenXml创建Word文档 [英] Creating Word Document using OpenXml
本文介绍了使用OpenXml创建Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码创建示例文档。
I am creating a sample document using the code below.
public static void CreateDocumentFile(string file)
{
// Create a Wordprocessing document.
using (WordprocessingDocument package = WordprocessingDocument.Create(file, WordprocessingDocumentType.Document))
{
// Add a new main document part.
package.AddMainDocumentPart();
// Create the Document DOM.
package.MainDocumentPart.Document =
new Document(
new Body(
new Paragraph(
new Run(
new Text("Hello World!")))));
// Save changes to the main document part.
package.MainDocumentPart.Document.Save();
}
文档是在所需位置创建的,但是当我打开它时,会给我一个错误,
无法打开文件,因为内容中存在一些问题,并指出我的document.xml文件包含错误在线:1 col:198
我的document.xml包含
Document is created at the desired location but when i open it, gives me an error ,
The file cannot be opened because there are some problem in the content and it is pointing that my document.xml file contains error on line:1 col:198
my document.xml contains
<pre lang="xml"><?xml version="1.0" encoding="utf-8"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><a:p xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:r><a:t>Hello World!</a:t></a:r></a:p></w:body></w:document>
推荐答案
您好,
这是错误的使用
声明......
段落
类存在于3个不同的命名空间中,我认为你选错了。
处理Word文档时,你应该使用DocumentFormat.OpenXml.Wordprocessing.Paragraph
我认为您使用DocumentFormat.OpenXml.Drawing.Paragraph
,因为示例中显示的架构是绘图命名空间的架构。
所以解决方案在你的使用部分很简单,删除:
Hello,
That''s a wrongusing
statement...
TheParagraph
class exists in 3 different namespaces, and I think you chose the wrong one.
When dealing with a Word document you should useDocumentFormat.OpenXml.Wordprocessing.Paragraph
I think you usedDocumentFormat.OpenXml.Drawing.Paragraph
as the schema shown in your example is the schema for the drawing namespace.
So the solution is simple in your using section, remove:
using DocumentFormat.OpenXml.Drawing;
并替换为正确的:
and replace by the correct one:
using DocumentFormat.OpenXml.Wordprocessing;
< br $>
Valery。
Valery.
用 Open XML Format SDK 2.0 [ ^ ](v 2.0.5022.0)[NET 3.5]
或 Open XML SDK 2.5 [ ^ ](v 2.5.5631.0)[NET 4.0]我的输出看起来像这样(输出格式化):
By testing your code with Open XML Format SDK 2.0[^] (v 2.0.5022.0) [NET 3.5]
or Open XML SDK 2.5[^] (v 2.5.5631.0) [NET 4.0] my output looked like this (output formatted):
<?xml version="1.0" encoding="utf-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:r>
<w:t>Hello World!</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
我能够在Word 2007和写字板6中打开文档。
and I was able to open the Document in Word 2007 and WordPad 6.
这篇关于使用OpenXml创建Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文