-1

从 Azure Blob 存储解析 Excel 工作表并使用计时器触发函数应用将数据插入数据库

4

1 回答 1

-1

从 Azure Blob 存储解析 Excel 工作表并使用计时器触发函数应用将数据插入数据库

我正在遵循以下解决方法来满足您的要求

要遵循的步骤:

  1. 从 Blob下载文件
  2. 读取/解析下载的文件
  3. 插入/更新到 Azure SQL 数据库
public static async Task RunAsync([TimerTrigger("0 */3 * * * *")]TimerInfo myTimer, ILogger log)
        {

            //Downloading Excel file from blob
            string localPath = "<your path>";
            string fileName = "test.xlsx";
            string localFilePath = Path.Combine(localPath, fileName);

            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            string containerName = "<container Name>";
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            BlobClient blobClient = containerClient.GetBlobClient(fileName);
            string downloadFilePath = localFilePath.Replace(".xlsx", "DOWNLOADED.xlsx");

            Console.WriteLine("\nDownloading blob to\n\t{0}\n", downloadFilePath);

            // Download the blob's contents and save it to a file
            await blobClient.DownloadToAsync(downloadFilePath);

            //Reading/Parsing the downloaded file 
            
            SpreadsheetDocument spreadsheetDocument =
           SpreadsheetDocument.Open(downloadFilePath, false);
            WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
            SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
            string text;
            foreach (Row r in sheetData.Elements<Row>())
            {
                foreach (Cell c in r.Elements<Cell>())
                {
                    if (c.InnerText != "")
                    {
                      //Reading the values here
                        text = c.InnerText;                                            
                    }
                }
            }
            Console.WriteLine();

        }
        //After reading  the values you can insert/update them  into Azure SQL Database

结果

在此处输入图像描述

于 2022-02-23T05:22:18.833 回答