通过Microsoft Graph API创建Excel文件 [英] Create excel file through Microsoft Graph API
问题描述
有人知道如何通过MS Graph API创建excel和ppt文件吗?我们正在尝试利用MS Graph API在单击按钮时创建word/excel/ppt文件,尽管我们发现了如何创建Word文件,但是即使api成功响应,所创建的excel和powerpoint文件也已损坏.下面的终点适用于Word文件.我们一直在使用图api资源管理器( https://developer.microsoft.com/en-us/graph/graph-explorer#).任何帮助将不胜感激!
Does anyone know how to create excel and ppt files through the MS Graph API? We're trying to leverage the MS Graph API to create word/excel/ppt files on a button click and while we found how to create word files, the excel and powerpoint files created are corrupted even with a success response from the api. The end point below works for the word files. We've been just working with the graph api explorer (https://developer.microsoft.com/en-us/graph/graph-explorer#) for now. Any help would be appreciated!
POST https://graph.microsoft.com/v1.0/drives/{Drive ID}/root/children/
Request Body:
{
"name": "FileTest6.docx",
"file":{
}
}
推荐答案
PowerPoint文件
PowerPoint files could be created via DriveItem upload endpoint, for example:
PUT https://graph.microsoft.com/v1.0/me/drive/root:/sample.pptx:/content
或
POST https://graph.microsoft.com/v1.0/me/drive/root/children
{
"name": "Sample.pptx",
"file":{ }
}
Excel文件
对于excel文件,情况有所不同,因为要上传的excel文件的内容需要明确提供.
With excel files the situation is a bit different since the content of the excel file to be uploaded needs to be provided explicitly.
对于ASP.NET Core应用程序,可以考虑以下解决方案:
For ASP.NET Core application the following solution could be considered:
- 通过打开XML SDK
- 创建空的Excel文档(请参见
CreateWorkbook
下面的示例) - 通过 DriveItem上传终结点
- create empty Excel document via Open XML SDK (see
CreateWorkbook
example below) - upload it via DriveItem upload endpoint
C#示例
using (var stream = new MemoryStream())
{
CreateWorkbook(stream);
stream.Seek(0, SeekOrigin.Begin);
var driveItem = await graphClient.Me
.Drive
.Root
.ItemWithPath("SampleWorkbook1.xlsx")
.Content
.Request()
.PutAsync<DriveItem>(stream);
}
其中
public static void CreateWorkbook(Stream stream)
{
// By default, AutoSave = true, Editable = true, and Type = xlsx.
var spreadsheetDocument =
SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart to the document.
var workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
// Add a WorksheetPart to the WorkbookPart.
var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
var sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
var sheet = new Sheet()
{Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet"};
sheets.Append(sheet);
workbookpart.Workbook.Save();
// Close the document.
spreadsheetDocument.Close();
}
这篇关于通过Microsoft Graph API创建Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!