c# - 如何在WP8中创建Excel文件?

标签 c# excel windows-phone-8

我想用 C# 为 Windows Phone 8 应用程序创建一个 Excel 文件,但找不到方法。

我用 OpenXml 试过了。但是,当我尝试执行代码时,出现此错误:

The type 'System.IO.Packaging.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=31bf3856ad364e35'.

这是代码:

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();
    }

最佳答案

缺少对“System.IO.Packaging”的引用。将“WindowsBase”添加到您的“引用”,这将解决问题。

步骤:

  1. 在您的项目上右键单击“引用”
  2. 点击“添加引用...”
  3. 转到“程序集”->“框架”。并选择“WindowsBase”
  4. 单击“确定”以解决您的问题。

关于c# - 如何在WP8中创建Excel文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20818190/

相关文章:

c# - ICorProfiler : Why do I get the wrong type token for a jitted function?

c# - 领域驱动设计适合我的项目吗?

c# - 为选择器中的项目添加键值

python - 列 (0,1,3) 具有混合类型。在导入时指定 dtype 选项或设置 low_memory=False。导入 csv 文件时

vba - Excel VBA - 从一张纸复制到另一张纸> 仅复制一行

c# - 有没有办法从 azure blob 的容器文件夹中获取所有子文件夹名称?

vba - Excel 2002 Web 查询搞砸了 en-GB 日期

c# - 在 Windows Phone 中使用 SVG 文件,例如 PNG 文件

deployment - 部署到 Windows Phone 8 时出现 "File not found"错误

windows-phone-8 - Windows Phone 8.1 后台任务