我想用 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”添加到您的“引用”,这将解决问题。
步骤:
- 在您的项目上右键单击“引用”
- 点击“添加引用...”
- 转到“程序集”->“框架”。并选择“WindowsBase”
- 单击“确定”以解决您的问题。
关于c# - 如何在WP8中创建Excel文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20818190/