Azure函数读取excel(xlsx)文件

问题描述 投票:0回答:1

我必须将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);
    }
}

约束

excel azure-sql-database azure-functions
1个回答
0
投票

如果可以在存储帐户中放置/上传该Excel文件,则可以在Azure SQL数据库上使用BULK INSERT。

下面的示例使用一个外部数据源指向一个Azure存储帐户中的容器(名为week3)。

CREATE EXTERNAL DATA SOURCE MyAzureInvoicesContainer
    WITH (
        TYPE = BLOB_STORAGE,
        LOCATION = 'https://newinvoices.blob.core.windows.net/week3',
        CREDENTIAL = UploadInvoices
    );

使用大容量插入,请不要在文件描述中使用容器名称:

BULK INSERT Colors2
FROM 'inv-2017-01-19.csv'
WITH (DATA_SOURCE = 'MyAzureInvoicesContainer',
      FORMAT = 'CSV');

如果您仍要使用Azure函数来执行此任务,请单击here,将找到完整的示例。

© www.soinside.com 2019 - 2024. All rights reserved.