简体   繁体   English

如何在WP8中创建Excel文件?

[英]How to create Excel file in WP8?

I want to create an Excel file with C# for Windows Phone 8 Application, but I couldn't find a way. 我想用C#为Windows Phone 8应用程序创建一个Excel文件,但我找不到办法。

I tried it with OpenXml. 我用OpenXml试了一下。 However, when I try to execute a code, I get this error: 但是,当我尝试执行代码时,我收到此错误:

The type 'System.IO.Packaging.Package' is defined in an assembly that is not referenced. 类型'System.IO.Packaging.Package'在未引用的程序集中定义。 You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. 您必须添加对程序集'WindowsBase,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'的引用。

This is the Code: 这是代码:

public static void CreateSpreadsheetWorkbook(string filepath)
    {
        // Create a spreadsheet document by supplying the filepath.
        // By default, AutoSave = true, Editable = true, and Type = xlsx.

        SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, DocumentFormat.OpenXml.SpreadsheetDocumentType.MacroEnabledWorkbook);

        // Add a WorkbookPart to the document.
        WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
        workbookpart.Workbook = new Workbook();

        // Add a WorksheetPart to the WorkbookPart.
        WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
        worksheetPart.Worksheet = new Worksheet(new SheetData());

        // Add Sheets to the Workbook.
        Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

        // Append a new worksheet and associate it with the workbook.
        Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
        sheets.Append(sheet);

        workbookpart.Workbook.Save();

        // Close the document.
        spreadsheetDocument.Close();
    }

Reference to "System.IO.Packaging" is missing. 缺少对“System.IO.Packaging”的引用。 Add "WindowsBase" to your "References" and this will solve the problem. 将“WindowsBase”添加到“参考”中,这将解决问题。

Steps: 脚步:

  1. Right Click "References" on your project 右键单击项目中的“参考”
  2. Click "Add Reference..." 点击“添加参考...”
  3. Go to "Assemblies"->"Framework". 转到“装配” - >“框架”。 And select "WindowsBase" 并选择“WindowsBase”
  4. Click "OK" to resolve your issue. 单击“确定”以解决您的问题。

I realize the WinRT framework is a subset of the full .NET framework (like WP8 and Silverlight) but sometimes you get lucky and referencing a 'full' .NET assembly works. 我意识到WinRT框架是完整.NET框架的一个子集(如WP8和Silverlight),但有时你会很幸运并引用一个“完整的”.NET程序集。 If the WinRT .NET framework supported System.IO.Packaging, or if there is a version of System.IO.Packaging that has been ported over to the WinRT .NET framework, I should be able to reference and use the DocumentFormat.OpenXml.dll. 如果WinRT .NET框架支持System.IO.Packaging,或者如果有一个已移植到WinRT .NET框架的System.IO.Packaging版本,我应该能够引用和使用DocumentFormat.OpenXml。 DLL。 Even better would be a version of the Open XML SDK for the WinRT framework (hint, hint...). 更好的是WinRT框架的Open XML SDK版本(提示,提示......)。

This answer exists at: http://social.msdn.microsoft.com/Forums/office/en-US/23c2154c-b6e2-4692-b4be-ca284fb74394/open-xml-sdk-winrt-windows-store?forum=oxmlsdk 这个答案存在于: http//social.msdn.microsoft.com/Forums/office/en-US/23c2154c-b6e2-4692-b4be-ca284fb74394/open-xml-sdk-winrt-windows-store?forum=oxmlsdk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM