Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.9k views
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...

sl.SaveAs(filepath);

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 Reply

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?!?");
                sl.SaveAs(filePath);
            }

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...