Excel 至 SQL Table(s)
Excel to SQL Table(s)
我正在尝试获取位于 Azure Blob 存储中的 Excel 工作簿中的数据(另外,Excel 文件每天手动更新,然后通过 MS 导出到 Blob 存储Flow),然后将该数据导出到 Azure SQL 数据库中的一些表中。我不能使用 SSIS 包,因为那样太贵了。任何人都知道在没有 SSIS 的情况下完成这项工作的方法吗?我查看了 OPENROWSET 和链接服务器,但两者都是 "not supported in my version of SQL server." 我还考虑过将 Excel 文件转换为 CSV,然后使用 ADF,但我不知道如何将其转换为blob 中的 CSV...(无需手动上传)
I've also considered converting the Excel file to CSV and then using ADF, but I can't figure out how to convert it to CSV in the blob... (without uploading manually)
根据你的描述,我建议你可以尝试使用azure webjob或azure function来达到你的要求。
通过使用这两个服务,您可以启用 blob 触发器(当新文件添加到 blob 时)或时间触发器(每天触发该功能)来执行将数据从 blob 存储导出到 azure 的功能sql 直接数据库。
更多详细信息,您可以参考以下代码(Web作业代码)和文章:
Webjob blobtrigger, webjob timer trigger.
//记得从 Nuget 包中安装 DocumentFormat.OpenXml。
public class 函数
{
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");
}
}
我正在尝试获取位于 Azure Blob 存储中的 Excel 工作簿中的数据(另外,Excel 文件每天手动更新,然后通过 MS 导出到 Blob 存储Flow),然后将该数据导出到 Azure SQL 数据库中的一些表中。我不能使用 SSIS 包,因为那样太贵了。任何人都知道在没有 SSIS 的情况下完成这项工作的方法吗?我查看了 OPENROWSET 和链接服务器,但两者都是 "not supported in my version of SQL server." 我还考虑过将 Excel 文件转换为 CSV,然后使用 ADF,但我不知道如何将其转换为blob 中的 CSV...(无需手动上传)
I've also considered converting the Excel file to CSV and then using ADF, but I can't figure out how to convert it to CSV in the blob... (without uploading manually)
根据你的描述,我建议你可以尝试使用azure webjob或azure function来达到你的要求。
通过使用这两个服务,您可以启用 blob 触发器(当新文件添加到 blob 时)或时间触发器(每天触发该功能)来执行将数据从 blob 存储导出到 azure 的功能sql 直接数据库。
更多详细信息,您可以参考以下代码(Web作业代码)和文章:
Webjob blobtrigger, webjob timer trigger.
//记得从 Nuget 包中安装 DocumentFormat.OpenXml。 public class 函数 {
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");
}
}