简体   繁体   English

如何将 blob(Excel 文件)上传到 Azure 存储帐户

[英]How to upload a blob (Excel file) to an Azure Storage account

I'm trying to create an Excel report using NPOI.XSSF library.我正在尝试使用 NPOI.XSSF 库创建 Excel 报告。 It works fine when I save into a disk file using a FileStream ;当我使用FileStream保存到磁盘文件时它工作正常; yet, if I use a MemoryStream and try to upload it to an Azure storage account, it uploads a file with the correct size but when I try to open it with excel it is corrupted.但是,如果我使用MemoryStream并尝试将其上传到 Azure 存储帐户,它会上传大小正确的文件,但是当我尝试使用 excel 打开它时,它已损坏。

This is my code:这是我的代码:

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

What am I doing wrong that makes the file to be corrupted?我做错了什么导致文件损坏?

I think the problem is that you using GetBuffer on the first MemoryStream to create the second one.我认为问题在于您在第一个MemoryStream上使用GetBuffer来创建第二个。 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.例如,如果将字符串“test”写入MemoryStream object,则从GetBuffer返回的缓冲区长度为 256,而不是 4,其中 252 个字节未使用。 To obtain only the data in the buffer, use the ToArray method;要仅获取缓冲区中的数据,请使用ToArray方法; however, ToArray creates a copy of the data in memory.但是, ToArray会在 memory 中创建数据的副本。

What is likely happening is that you are getting unwanted extra zero bytes at the end of the stream from the buffer.可能发生的情况是,您在缓冲区的 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 末尾获得了不需要的额外零字节。 Excel doesn't know what to do with the extra bytes, so it declares that the workbook is corrupted. Excel 不知道如何处理多余的字节,因此它声明工作簿已损坏。 Since XLSX is a compressed format, it's entirely possible that the file size would be the same with the extra zeros as without them, so you might not be able to tell just by looking at that.由于 XLSX 是一种压缩格式,因此文件大小完全有可能与没有它们的额外零相同,因此您可能无法仅通过查看来判断。

In short, you should use ToArray instead to prevent the corruption.简而言之,您应该改用ToArray来防止损坏。

To fix, change this line:要修复,请更改此行:

var buffer = ms.GetBuffer();

To this:对此:

var buffer = ms.ToArray();

To upload the file to Azure blob, you can use the default "Azure.Storage.Blobs" library provided by microsoft.要将文件上传到 Azure blob,可以使用 microsoft 提供的默认“Azure.Storage.Blobs”库。 then create a refrence to the container and provide the path to the file in the blob.upload(path).然后创建对容器的引用并在 blob.upload(path) 中提供文件的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM