从Azure Excel blob文件导入数据到SQL Server
本文关键字:数据 SQL Server 导入 文件 Azure Excel blob | 更新日期: 2023-09-27 18:04:45
我有一个MVC web应用程序,允许用户将Excel文件上传到Azure云存储,然后应用程序使用该Azure存储的Excel blob文件将数据导入SQL Server。
我遵循网站http://www.codeproject.com/Tips/752981/Import-Data-from-Excel-File-to-Database-Table-in-A
和
上传Excel文件并从中提取数据,并使用MVC asp.net将数据放入数据库
来执行我的应用程序。但是,来自网站http://www.codeproject.com/Tips/752981/Import-Data-from-Excel-File-to-Database-Table-in-A的示例允许用户将文件上传到web服务器,其中应用程序部署不在Azure存储,并且"fileLocation"变量的内容(请参阅下面的代码)看起来像(相对于web-服务器托管的应用程序路径C或任何驱动器)"C:'MyWebApplicationFolder'MyApplicatioName'Content'Excel_blob.xlsx"
我的问题:对于Azure存储Excel blob文件,我如何指定"fileLocation"answers"excelConnectionString"变量的值?请参阅下面以短语"//***如何使用Azure存储代码执行此操作?"开头的代码注释。
代码来自http://www.codeproject.com/Tips/752981/Import-Data-from-Excel-File-to-Database-Table-in-A
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
DataSet ds = new DataSet();
if (Request.Files["file"].ContentLength > 0)
{
string fileExtension = System.IO.Path.GetExtension(Request.Files["file"].FileName);
if (fileExtension == ".xls" || fileExtension == ".xlsx")
{
string fileLocation = Server.MapPath("~/Content/") + Request.Files["file"].FileName; // *** How can I can do this with Azure storage codes?
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
Request.Files["file"].SaveAs(fileLocation);
string excelConnectionString = string.Empty;
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties='"Excel 12.0;HDR=Yes;IMEX=2'""; // *** How can I can do this with Azure storage codes?
//connection String for xls file format.
if (fileExtension == ".xls")
{
excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties='"Excel 8.0;HDR=Yes;IMEX=2'""; // *** How can I can do this with Azure storage codes?
}
//connection String for xlsx file format.
else if (fileExtension == ".xlsx")
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties='"Excel 12.0;HDR=Yes;IMEX=2'""; // *** How can I can do this with Azure storage codes?
}
...
也许您可以先从Azure下载blob文件到服务器磁盘,然后将其导入到数据库。你可以在这里下载完整的项目。
下载:container.CreateIfNotExists();
CloudBlockBlob blob = container.GetBlockBlobReference(excelName);
blob.DownloadToFile(filePath, FileMode.Create);
读取文件到数据表:
DataTable dt = new DataTable();
using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(filePath, false))
{
//Get sheet data
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>();
// Set columns
foreach (Cell cell in rows.ElementAt(0))
{
dt.Columns.Add(cell.CellValue.InnerXml);
}
//Write data to datatable
foreach (Row row in rows.Skip(1))
{
DataRow newRow = dt.NewRow();
for (int i = 0; i < row.Descendants<Cell>().Count(); i++)
{
if (row.Descendants<Cell>().ElementAt(i).CellValue != null)
{
newRow[i] = row.Descendants<Cell>().ElementAt(i).CellValue.InnerXml;
}
else
{
newRow[i] = DBNull.Value;
}
}
dt.Rows.Add(newRow);
}
}
使用大容量复制将数据插入到db
//Bulk copy datatable to DB
SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionStr);
try
{
columns.ForEach(col => { bulkCopy.ColumnMappings.Add(col, col); });
bulkCopy.DestinationTableName = tableName;
bulkCopy.WriteToServer(dt);
}
catch (Exception ex)
{
throw ex;
}
finally
{
bulkCopy.Close();
}