Welcome to ZaiNaLe Developer Community-Open, Learning and Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

save created excel file to azure blob container. path issue

Using an Azure Function I create an excel file and I am trying to save it to my Azure Blob Container. The code to save it is...


The problem is I don't know what to put for the variable filepath. When I save it on my local harddrive in development I use this path which works...

string filepath= @"C:Usersxxxsource eposxxxxxxempx.xlsx";

In Azure, if I goto properties for the blob container I see this. https://abc.blob.core.windows.net/xyz

I know I am missing something simple here. Right now I am running in debug mode locally using VS2019, not sure if that is the issue.

Any help would be greatly appreciated!

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We can't use SaveAs method to save your excel file directly to Storage.

I guess you use the SpreadsheetLight assembly. Please refer to the following code, you can save your Excel file to local first and then read it as stream:

            String filePath = "D:\document\MahNewShoes.xlsx";
            using (SLDocument sl = new SLDocument())
                sl.SetCellValue("B3", "It costs what for a Jimmy Choo?!?");

            string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("test");
            BlobClient blobClient = containerClient.GetBlobClient("MahNewShoes.xlsx");
            using FileStream uploadFileStream = File.OpenRead(filePath);
            await blobClient.UploadAsync(uploadFileStream, true);

Welcome to ZaiNaLe Developer Community-Open, Learning and Share