[英]Best way to export data from an Excel file to SQL server database
[英]Recommended way to read data from an Excel file using an Azure App Service?
背景
我有一個遺留站點,允許授權用戶上傳產品數據的Excel電子表格等。然后,該站點讀取Excel工作表並將數據解壓縮到SQL服務器。 這是一個舊網站,它使用OLE。 舊但它的工作原理。
問題
我最近將該網站發布到Azure 應用服務,但我從Excel中讀取的代碼的現有部分不起作用(因為Azure沒有正確的驅動程序)。
這個問題
我很高興重寫這部分代碼但是使用Azure App服務從Excel讀取的CORRECT或RECOMMENDED方法是什么? 我不是在問可能的工作方式我只對這種方式感興趣。
通過“推薦”我的意思是:
我已經研究過這個問題,但未能找到最佳方法的明確聲明。 如果您有不同方法的經驗或知識,我將不勝感激,如果您能分享關於最佳方法的結論。
應該有很多方法可以實現這一點,在這里我列出如下2:
1.使用由MS發布的DocumentFormat.OpenXml ,但它有點復雜。 演示代碼在這里 。
2.使用ExcelDataReader ,它非常簡單,同時支持.xls and .xlsx
。 您可以參考這篇文章來做(請注意, IsFirstRowAsColumnNames
屬性已被放棄,您可以在下面看到我的代碼以進行此更改)。
我用ExcelDataReader
方法ExcelDataReader
編寫了一個演示。為了測試目的,我將excel上傳到azure web app目錄,如下所示:
以下是excel內容:
步驟1:創建一個asp.net MVC項目,然后通過nuget包管理器安裝最新版本的ExcelDataReader
和ExcelDataReader.DataSet
。
第2步:在項目中創建一個用於讀取excel文件的ExcelData.cs文件:
第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;
}
}
}
步驟4:在控制器中,調用read 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中的所有數據:
所以,我使用https://github.com/dotnetcore/NPOI進行Excel導入,並在Azure App Service上進行了測試,這非常好。 我通過成功導入50,000條記錄進行了測試。 但要注意,如果要導入大約10萬條記錄,那么對於長時間運行的任務,您可能會收到請求超時錯誤,應該創建Web作業/功能。 請記住,Azure App Service的requestTimeout限制為230秒。 在選擇實施之前考慮以下鏈接會很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.