0

我想獲取位於Azure Blob存儲中的Excel工作簿中的數據(此外,Excel文件每天手動更新,然後通過MS Flow導出到Blob存儲)以及然後將該數據導出到Azure SQL數據庫中的某些表中。我不能使用SSIS包,因爲這太昂貴了。任何人都知道如何在沒有SSIS的情況下完成這項任務?我研究過OPENROWSET和連接的服務器,但都是「我的SQL服務器版本不支持」。我也考慮將Excel文件轉換爲CSV然後使用ADF,但我無法弄清楚如何將其轉換爲CSV中的blob ...(無需手動上傳)Excel到SQL表格

+0

你爲什麼說SSIS是昂貴的? – FLICKER

回答

1

我也考慮將Excel文件轉換爲CSV然後使用ADF,但我無法弄清楚如何將其轉換爲CSV中的blob ...(無需手動上傳)

根據您的描述,我建議您可以嘗試使用天藍色的webjob或azure功能來實現您的要求。

通過使用這兩個服務,您可以啓用blob觸發器(當新文件被添加到blob中)或時間觸發器(每天觸發該函數)來執行函數以將數據從blob存儲導出到azure sql數據庫直接。

更多細節,你可以參考下面的代碼(Web作業規範)和文章:

Webjob blobtriggerwebjob timer trigger

//記得從Nuget Package安裝DocumentFormat.OpenXml。 public class Functions {

 public static string GetCellValue(SpreadsheetDocument document, Cell cell) 
     { 
      SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart; 
      string value = cell.CellValue.InnerXml; 

      if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString) 
      { 
       return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText; 
      } 
      else 
      { 
       return value; 
      } 
     } 


     public static void ExportExcelToDatabase ([BlobTrigger("excel/testexcel.xlsx")] Stream blobStream, TextWriter log) 
     { 
      log.WriteLine("Start export excel to azure sql database"); 


      string connectionStr = "{sql database connection string}"; 

      //This is the excel table column name 
      List<string> columns = new List<string>() { "Name", "Class", "Score", "Sex" }; 
      string tableName = "StudentScore"; 


      DataTable dt = new DataTable(); 
      using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(blobStream, false)) 
      { 
       WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart; 
       IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>(); 
       string relationshipId = sheets.First().Id.Value; 
       WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId); 
       Worksheet workSheet = worksheetPart.Worksheet; 
       SheetData sheetData = workSheet.GetFirstChild<SheetData>(); 
       IEnumerable<Row> rows = sheetData.Descendants<Row>(); 

       foreach (Cell cell in rows.ElementAt(0)) 
       { 
        dt.Columns.Add(GetCellValue(spreadSheetDocument, cell)); 
       } 

       foreach (Row row in rows.Skip(1)) 
       { 
        DataRow tempRow = dt.NewRow(); 

        for (int i = 0; i < row.Descendants<Cell>().Count(); i++) 
        { 
         tempRow[i] = GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i)); 
        } 

        dt.Rows.Add(tempRow); 
       } 
      } 



      //Bulk copy datatable to DB 
      SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionStr); 
      try 
      { 
       bulkCopy.DestinationTableName = tableName; 
       columns.ForEach(col => { bulkCopy.ColumnMappings.Add(col, col); }); 
       bulkCopy.WriteToServer(dt); 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
      finally 
      { 
       bulkCopy.Close(); 
      } 

      log.WriteLine("End export excel to azure sql database"); 


     } 
    }