c# – 无法使用Open XML SDK创建文档
作者:互联网
我正在学习在Visual Studio社区2015中使用Open XML SDK.我尝试按照以下示例创建文档:https://msdn.microsoft.com/en-us/library/dd440953(v=office.12).aspx
我的代码:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.HelloWorld("abc.docx");
}
public void HelloWorld(string docName)
{
// Create a Wordprocessing document.
using (WordprocessingDocument package = WordprocessingDocument.Create(docName, 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();
}
}
}
}
我在WordprocessingDocument.Create上遇到错误:
Error CS0012 The type 'Package' is defined in an assembly that is not referenced.
You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=1234'.
ConsoleApplication1 c:\users\john\documents\visual studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs
我已经用nuget安装了DocumentFormat.OpenXml包
代码有什么问题?
解决方法:
该错误消息告诉您如何解决您的问题:
You must add a reference to assembly ‘WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=1234’.
在Visual Studio的解决方案资源管理器中右键单击项目的“引用”节点,然后选择“添加引用”.然后,在Assemblies下 – >框架您必须选择WindowsBase并将其作为参考.然后重新编译您的项目(请注意,这要求您使用的是.NET Framework 3.5或更高版本).
标签:c,net,visual-studio,openxml,openxml-sdk 来源: https://codeday.me/bug/20190713/1453536.html