如何将 blob(Excel 文件)上传到 Azure 存储帐户
How to upload a blob (Excel file) to an Azure Storage account
我正在尝试使用 NPOI.XSSF 库创建 Excel 报告。当我使用 FileStream
保存到磁盘文件时它工作正常;但是,如果我使用 MemoryStream
并尝试将其上传到 Azure 存储帐户,它会上传一个大小正确的文件,但是当我尝试使用 excel 打开它时,它已损坏。
这是我的代码:
public static void ExportToStorageAccount(string storageConnection, IEnumerable<ReportEntry> reportEntries)
{
var storageAccount = CloudStorageAccount.Parse(storageConnection);
var myClient = storageAccount.CreateCloudBlobClient();
var container = myClient.GetContainerReference("reportsContainer");
string fileName = GetFileName();
using (var ms = new MemoryStream())
{
IWorkbook workbook = CreateWorkbook(reportEntries);
workbook.Write(ms);
var blockBlob = container.GetBlockBlobReference(fileName);
var buffer = ms.GetBuffer();
blockBlob.UploadFromStream(new MemoryStream(buffer, false));
}
}
我做错了什么导致文件损坏?
要将文件上传到Azure blob,可以使用微软提供的默认"Azure.Storage.Blobs"库。然后创建对容器的引用并在 blob.upload(path).
中提供文件的路径
我认为问题在于您使用 GetBuffer
on the first MemoryStream
to create the second one. From the documentation:
Remarks
Note that the buffer contains allocated bytes which might be unused. For example, if the string "test" is written into the MemoryStream
object, the length of the buffer returned from GetBuffer
is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use the ToArray
method; however, ToArray
creates a copy of the data in memory.
可能发生的情况是您在流的末尾从缓冲区中获取不需要的额外零字节。 Excel 不知道如何处理额外的字节,因此它声明工作簿已损坏。由于 XLSX 是一种压缩格式,因此文件大小完全有可能与没有它们的额外零相同,因此您可能无法仅通过查看来判断。
简而言之,您应该使用 ToArray
来防止损坏。
要修复,请更改此行:
var buffer = ms.GetBuffer();
为此:
var buffer = ms.ToArray();
我正在尝试使用 NPOI.XSSF 库创建 Excel 报告。当我使用 FileStream
保存到磁盘文件时它工作正常;但是,如果我使用 MemoryStream
并尝试将其上传到 Azure 存储帐户,它会上传一个大小正确的文件,但是当我尝试使用 excel 打开它时,它已损坏。
这是我的代码:
public static void ExportToStorageAccount(string storageConnection, IEnumerable<ReportEntry> reportEntries)
{
var storageAccount = CloudStorageAccount.Parse(storageConnection);
var myClient = storageAccount.CreateCloudBlobClient();
var container = myClient.GetContainerReference("reportsContainer");
string fileName = GetFileName();
using (var ms = new MemoryStream())
{
IWorkbook workbook = CreateWorkbook(reportEntries);
workbook.Write(ms);
var blockBlob = container.GetBlockBlobReference(fileName);
var buffer = ms.GetBuffer();
blockBlob.UploadFromStream(new MemoryStream(buffer, false));
}
}
我做错了什么导致文件损坏?
要将文件上传到Azure blob,可以使用微软提供的默认"Azure.Storage.Blobs"库。然后创建对容器的引用并在 blob.upload(path).
中提供文件的路径我认为问题在于您使用 GetBuffer
on the first MemoryStream
to create the second one. From the documentation:
Remarks
Note that the buffer contains allocated bytes which might be unused. For example, if the string "test" is written into the
MemoryStream
object, the length of the buffer returned fromGetBuffer
is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use theToArray
method; however,ToArray
creates a copy of the data in memory.
可能发生的情况是您在流的末尾从缓冲区中获取不需要的额外零字节。 Excel 不知道如何处理额外的字节,因此它声明工作簿已损坏。由于 XLSX 是一种压缩格式,因此文件大小完全有可能与没有它们的额外零相同,因此您可能无法仅通过查看来判断。
简而言之,您应该使用 ToArray
来防止损坏。
要修复,请更改此行:
var buffer = ms.GetBuffer();
为此:
var buffer = ms.ToArray();