我正在学习使用Open Studio SDK和Visual Studio Community 2015.我尝试按照以下示例创建文档: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
我已经DocumentFormat.OpenXml
用nuget 安装了这个包
代码有什么问题?
该错误消息告诉您如何解决您的问题:
您必须添加对程序集'WindowsBase,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 1234'的引用.
在Visual Studio的解决方案资源管理器中右键单击项目的" 引用"节点,然后选择" 添加引用".然后,在Assemblies - > Framework下,您必须选择WindowsBase并将其作为参考.然后重新编译您的项目(请注意,这要求您使用的是.NET Framework 3.5或更高版本).