直接在 blob 上创建 Excel 个文件

Creating Excel files directly on blob

我有以下功能,在保存到磁盘时工作正常。我正在从 Azure 函数执行代码。有没有办法在不保存到磁盘的情况下写入 blob 存储?

private void ExportDataSet(DataTable ds, string destination)
{
    using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
    {
        var workbookPart = workbook.AddWorkbookPart();

        workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();

        workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();

        var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
        var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
        sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);

        DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
        string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);

        uint sheetId = 1;
        if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)
        {
            sheetId =
                sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1;
        }

        DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = "Sites" };
        sheets.Append(sheet);

        DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();

        List<String> columns = new List<string>();
        foreach (System.Data.DataColumn column in ds.Columns)
        {
            columns.Add(column.ColumnName);

            DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
            cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
            cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
            headerRow.AppendChild(cell);
        }   

        sheetData.AppendChild(headerRow);

        foreach (System.Data.DataRow dsrow in ds.Rows)
        {
            DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
            foreach (String col in columns)
            {
                DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
                newRow.AppendChild(cell);
            }    
            sheetData.AppendChild(newRow);
        }                
    }
}

我希望您可以保存到流中?

如果您不想将其保存到磁盘(在 azure 函数中,您可以将其保存到 azure function kudu 中的磁盘,如 D:\home 等),则保存到流是解决方案。

如果您选择保存到流,只需对您的代码进行一些更改,如下所示:

    private void ExportDataSet(DataTable ds, MemoryStream memoryStream)
    {
        using (var workbook = SpreadsheetDocument.Create(memoryStream, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
        {
        //your code logic here    

        }

        //here, the code to upload to azure blob storage.
         CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);    
         CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
         CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("test1");
         CloudBlockBlob myblob = cloudBlobContainer.GetBlockBlobReference("myexcel.xlsx");


          //upload to blob storage
          memoryStream.Position = 0;
          myblob.UploadFromStream(memoryStream)

          //or you can use Asnyc mehtod like myblob.UploadFromStreamAsync(memoryStream)

    }

注意:如果您使用的是最新的 azure blob 存储 sdk Microsoft.Azure.Storage.Blob, version 9.4.0 或更高版本,则可以在 azure function v2.[=14 中使用 UploadFromStreamAsyncUploadFromStream 方法=]