我正在尝试将使用NPOI库创建的Excel文档上传使用Microsoft Graph API创建的SharePoint Online网站。
当我将Excel文件写入本地计算机用户目录时,我可以打开生成的 testexcelupload.xslx
没有问题的文件,然后查看'colone','coltwo','colthree '条目。
但是,当我将Excel文件上传到SharePoint Online网站时,我可以看到文件创建,但是当我尝试打开它时,我会收到错误消息对不起,我们找不到'https:// company 。是否有可能被移动,重命名或删除?
。如果下载文件,我可以看到它的大小为0字节。因此,我认为问题与我的上传方式有关 - 我尝试关注Microsoft文档中的示例 - 。
下面的代码是我所处的位置:
- 使用NPOI库创建一个简单的Excel工作簿,
- 请使用文件流将Excel Workbook写入我的本地目录(我最初只是用它来排除
- 使用 NPOI创建工作簿的问题)图形API要获取我要上传的文档路径,将Excel Workbook读取到存储器流中,然后再次使用Graph API将内存流将其作为内容上传到路径。
有人可以告诉我我可能会出错的地方吗?
public async Task ExcelUploadToSharePointTest()
{
// 1. Create basic excel .xlsx workbook using NPOI v2.5.6
var testColumnData = new List<string> { "ColOne", "ColTwo", "ColThree" };
var workbook = new XSSFWorkbook();
var sheet1 = workbook.CreateSheet();
var row1 = sheet1.CreateRow(0);
for (var i = 0; testColumnData.Count > i; i++)
{
row1.CreateCell(i).SetCellValue(testColumnData[i]);
}
// 2. Write workbook to local user directory - works
await using (var fileSteam = new FileStream($"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/TestExcelUpload.xlsx", FileMode.Create))
{
workbook.Write(fileSteam, true);
}
// 3. Write workbook to SharePoint online, by saving to memory stream and uploading using Graph API - not working
var siteBase = await _graphServiceClient.Sites
.GetByPath("/sites/Technology", "company.sharepoint.com")
.Request()
.GetAsync();
var rootFolder = _graphServiceClient.Sites[siteBase.Id].Drive.Root;
await using (var memoryStream = new MemoryStream())
{
workbook.Write(memoryStream, true);
await rootFolder.ItemWithPath("Tool/Data/TestExcelUpload.xlsx").Content.Request().PutAsync<DriveItem>(memoryStream);
}
}
I am attempting to upload an Excel document created using the NPOI library to a SharePoint Online site using the Microsoft Graph API.
When I write the Excel file to my local machines user directory, I'm able to open the generated TestExcelUpload.xslx
file without issue and see the 'ColOne', 'ColTwo', 'ColThree' entries.
However, when I upload the Excel file to the SharePoint Online site, I can see the file gets created, but when I attempt to open it I get the error message Sorry, we couldn't find 'https://company.sharepoint.com/sites/Technology/Shared Documents/Tool/Data/TestExcelUpload.xlsx'. Is it possible it was moved, renamed or deleted?
. If I download the file, I can see it has a Size of 0 bytes. So I think the issue is to do with how I am doing the upload - I attempted to follow the sample from the Microsoft docs - https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?msclkid=5dab86cad12711ec91c842a089f27412&view=graph-rest-1.0&tabs=csharp.
The code below is where I am with it:
- Create a simple excel workbook using the NPOI library
- Use a file stream to write the excel workbook to my local directory (I initially just used this to rule out a problem creating the workbook with NPOI)
- Use the Graph API to get the document path I want to upload to, read the excel workbook into a memory stream and then use the Graph API again to upload the memory stream to the path as the content.
Would anyone be able to tell me where I might be going wrong?
public async Task ExcelUploadToSharePointTest()
{
// 1. Create basic excel .xlsx workbook using NPOI v2.5.6
var testColumnData = new List<string> { "ColOne", "ColTwo", "ColThree" };
var workbook = new XSSFWorkbook();
var sheet1 = workbook.CreateSheet();
var row1 = sheet1.CreateRow(0);
for (var i = 0; testColumnData.Count > i; i++)
{
row1.CreateCell(i).SetCellValue(testColumnData[i]);
}
// 2. Write workbook to local user directory - works
await using (var fileSteam = new FileStream(quot;{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/TestExcelUpload.xlsx", FileMode.Create))
{
workbook.Write(fileSteam, true);
}
// 3. Write workbook to SharePoint online, by saving to memory stream and uploading using Graph API - not working
var siteBase = await _graphServiceClient.Sites
.GetByPath("/sites/Technology", "company.sharepoint.com")
.Request()
.GetAsync();
var rootFolder = _graphServiceClient.Sites[siteBase.Id].Drive.Root;
await using (var memoryStream = new MemoryStream())
{
workbook.Write(memoryStream, true);
await rootFolder.ItemWithPath("Tool/Data/TestExcelUpload.xlsx").Content.Request().PutAsync<DriveItem>(memoryStream);
}
}
发布评论
评论(1)
看来您需要将
memoryStream.position
设置为0。当您调用
workbook.write(MemoryStream,true)
时,流中的当前位置与流的长度和零字节被上传。It looks like that you need to set
memoryStream.Position
to 0.When you call
workbook.Write(memoryStream, true)
then the current position within the stream is same as the length of the stream and therefore zero bytes are uploaded.