我正在尝试获取位于Azure Blob存储中的Excel工作簿中的数据(此外,Excel文件每天手动更新,然后通过MS Flow导出到Blob存储),然后将该数据导出到某些表中在Azure SQL数据库中。我不能使用SSIS包,因为这太昂贵了。有没有人知道如何在没有SSIS的情况下完成这项工作?我查看了OPENROWSET和链接服务器,但两者都“在我的SQL服务器版本中不受支持”。我还考虑过将Excel文件转换为CSV然后使用ADF,但我无法弄清楚如何将其转换为blob中的CSV ...(无需手动上传)
答案 0 :(得分:1)
我还考虑过将Excel文件转换为CSV然后使用ADF,但我无法弄清楚如何将其转换为blob中的CSV ...(无需手动上传)
根据您的描述,我建议您尝试使用azure webjob或azure功能来满足您的要求。
通过使用这两个服务,您可以启用blob触发器(当新文件添加到blob中时)或时间触发器(每天触发函数)执行函数以将数据从blob存储器导出到azure sql数据库直接
更多细节,您可以参考以下代码(网络工作代码)和文章:
Webjob blobtrigger,webjob timer trigger。
//记得从Nuget包安装DocumentFormat.OpenXml。 公共类功能 {
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");
}
}