【问题标题】:Recommended way to read data from an Excel file using an Azure App Service?推荐使用 Azure 应用服务从 Excel 文件中读取数据的方法?
【发布时间】:2018-11-07 12:38:13
【问题描述】:

背景
我有一个旧站点,允许授权用户上传产品数据的 Excel 电子表格等。然后该站点读取 Excel 工作表并将数据解压缩到 SQL 服务器中。 这是一个旧网站,它使用 OLE。旧的,但它的工作原理。

问题
我最近将该站点发布到 Azure App Service,但我从 Excel 读取的现有代码部分不起作用(因为 Azure 没有正确的驱动程序)。

问题
我很高兴重写这部分代码,但是使用 Azure 应用服务从 Excel 读取的正确或推荐方法是什么? 我不是在问可能的工作方式,我只对正确的方式感兴趣。

“推荐”是指:

  • 没有不必要的复杂。保持简单。
  • 将来可能会保留 Microsoft 的支持

我已经研究过这个问题,但没有找到一个明确的说明来说明最好的方法。如果您有不同的执行此操作方法的经验或知识,如果您能分享您对执行此操作的最佳方法的结论,我将不胜感激。

【问题讨论】:

  • 好吧,我不知道它是否符合您对推荐的定义,但我们在生产中使用this package,尽管它是一个天蓝色的功能。因此,在网络应用程序中使用它应该不会有任何问题。
  • xlsx 是一个包含 XML 文件的 zip 包。您可以在没有任何特殊支持的情况下阅读它们,甚至不使用 OpenXML SDK。您可以使用 OpenXML SDK 读取 真正 大文件,如How to: Parse and read a large spreadsheet document (Open XML SDK) 所示。或者你可以使用像 EPPlus 这样的库,它可能是最流行的用于创建和读取 xlsx 文件的库
  • SQL Server 托管在哪里?它是 Azure SQL 还是 VM?如果是 VM,您可以使用 SSIS 将 Excel 文件从文件夹导入 SQL Server。
  • 最后,关于“保留来自 Microsoft 的支持”。 xls 在 12 年前就被废弃了,甚至像 Google Docs 这样的服务也需要付费订阅才能支持它。 xlsx 是专门为让任何人在不需要 OLEDB 驱动程序的情况下阅读它而构建的。
  • 刚刚也发现了这个 - Import data from Excel to SQL Server or Azure SQL Database。它显示了可用于从服务器端加载 Excel 数据的各种方法。 Azure 数据工厂也可以运行 SSIS 包

标签: sql-server excel azure azure-web-app-service


【解决方案1】:

应该有很多方法可以实现,这里我列出了2个如下:

1.使用DocumentFormat.OpenXml,是MS发布的,但是有点复杂。演示代码是here

2.使用ExcelDataReader,很简单,同时支持.xls and .xlsx。你可以参考这个article来做(注意IsFirstRowAsColumnNames属性被废弃了,你可以在下面看到我的代码来做这个更改)。

我用第二种方法ExcelDataReader编写了一个演示。出于测试目的,我将excel上传到了azure web app目录,如下所示:

以下是excel内容:

第一步:创建一个asp.net MVC项目,然后通过nuget包管理器安装最新版本的ExcelDataReaderExcelDataReader.DataSet

第二步:在你的项目中创建一个ExcelData.cs文件,用来读取excel文件:

步骤 3:在 ExcelData.cs 中编写以下代码:

using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;

namespace WebApplication42
{
    public class ExcelData
    {
        string _path;
        public ExcelData(string path)
        {
            _path = path;
        }

        public IExcelDataReader GetExcelReader()
        {
            FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read);
            IExcelDataReader reader = null;
            try
            {
                if (_path.EndsWith(".xls"))
                {
                    reader = ExcelReaderFactory.CreateBinaryReader(stream);
                }
                if (_path.EndsWith(".xlsx"))
                {
                    reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                }

                return reader;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //read the sheets name if you need
        public IEnumerable<string> GetWorksheetNames()
        {
            var reader = this.GetExcelReader();
            var workbook = reader.AsDataSet();
            var sheets = from DataTable sheet in workbook.Tables select sheet.TableName;
            return sheets;
        }

        //read data in a specified sheet
        public IEnumerable<DataRow> GetData(string sheet)
        {

            var reader = this.GetExcelReader();
            var workSheet = reader.AsDataSet(new ExcelDataSetConfiguration()
            {
                ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
                {
                    //indicates if use the header values
                    UseHeaderRow = true
                }

            }).Tables[sheet];

            var rows = from DataRow a in workSheet.Rows select a;
            return rows;
        }    

    }
}

第四步:在控制器中,调用读取excel方法:

        public ActionResult Excels()
        {
            ViewBag.Message = "the data from excel:";
            string data = "";

            //your excel path after uploaded, here I hardcoded it for test only
            string path = @"D:\home\site\wwwroot\Files\ddd.xls";
            var excelData = new ExcelData(path);
            var people = excelData.GetData("sheet1");

            foreach (var p in people)
            {
                for (int i=0;i<=p.ItemArray.GetUpperBound(0);i++)
                {
                    data += p[i].ToString()+",";
                }

                data += ";";
            }

            ViewBag.Message += data;

            return View();
        }

第5步:发布到azure后,启动站点并查看结果->读取excel中的所有数据:

【讨论】:

  • 最佳选择..!!干得好..!
【解决方案2】:

所以,我使用https://github.com/dotnetcore/NPOI 进行 Excel 导入,并在 Azure 应用服务上进行了测试,它真的很棒。我已经通过成功导入 50,000 条记录进行了测试。但请注意,如果您要导入大约 10 万条记录,您可能会收到请求超时错误,因为对于长时间运行的任务,应该创建 Web 作业/功能。 请记住,Azure 应用服务的 requestTimeout 限制为 230 秒。在选择实施之前考虑以下链接会很好。

https://feedback.azure.com/forums/169385-web-apps/suggestions/19309957-allow-a-request-timeout-of-more-then-3-8-minutes

Azure ASP .net WebApp - 500 Error - The request timed out

【讨论】:

    猜你喜欢
    • 2015-05-25
    • 2012-08-04
    • 2017-10-08
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多