【发布时间】:2020-03-02 21:42:57
【问题描述】:
我必须将 excel 文件导入 Azure SQL。这是相当频繁的活动,因此不能使用向导或手动过程。该文件包含 8000 行和 95 列。我正在考虑使用 Azure Functions,但使用 openxml 读取 excel 需要花费太多时间(本地晚上)。除了 openxml 和优化下面的代码,我还有其他选择吗?
代码:
using (SpreadsheetDocument workbook =
SpreadsheetDocument.Open(uploadedFile, false))
{
var sheet = ExcelUtilities.GetFirstSheet(workbook);
var workSheet = sheet.Worksheet;
var sheetData = workSheet.GetFirstChild<SheetData>();
IEnumerable<Row> rows = sheetData.Descendants<Row>();
foreach (Cell cell in rows.ElementAt(0))
{
dt.Columns.Add(ExcelUtilities.GetCellValue(workbook, cell));
}
for (int rowIndex = 1; rowIndex < rows.Count(); rowIndex++)
{
var row = rows.ElementAt(rowIndex);
DataRow tempRow = dt.NewRow();
for (int i = 0; i < row.Descendants<Cell>().Count(); i++)
{
tempRow[i] = ExcelUtilities.GetCellValue(workbook, row.Descendants<Cell>().ElementAt(i));
}
dt.Rows.Add(tempRow);
}
}
约束:
- 我认为 interop.excel 或 OLEDB 不能在 Azure Functions 中使用(据此https://stackoverflow.com/a/58155350/3633589Microsoft.Ace 提供程序不可用)
- OPENROWSET 也不是一个选项
【问题讨论】:
标签: excel azure-sql-database azure-functions