在Azure Blob存储上读取Excel文件的文件内容 [英] Read file content of Excel file on Azure Blob Storage
问题描述
大家好,
我正在使用Azure功能来读取放置在Azure blob存储上的excel文件的文件内容。
$
http://www.dotnetfunda.com/articles/show/3489/read-a-excel-blob-file-using-excel-data -reader-in-azure
$
由于Framework Change,同一条代码适用于Console App,而不适用于Azure功能。
Azure功能不支持Microsoft.WindowsAzure.ConfigurationManager。
我也试过OpenXMl阅读数据。 https://stackoverflow.com/questions/43171845/download-an-excel-file-and-read-content-with-azure-functions
但下载文件失败
请提供您如何阅读通过Azure功能放置在Azure blob存储上的Excel文件的输入。
感谢任何输入!
委员会,
Rameshwari
Hi All,
I am using Azure function to read file contents of an excel file which is place on Azure blob storage.
http://www.dotnetfunda.com/articles/show/3489/read-a-excel-blob-file-using-excel-data-reader-in-azure
The same piece of code works for Console App and not for Azure functions, due to Framework Change.
Microsoft.WindowsAzure.ConfigurationManager is not supported in Azure function.
I also tried OpenXMl to read data. https://stackoverflow.com/questions/43171845/download-an-excel-file-and-read-content-with-azure-functions
But the download file fails
Please provide your inputs on how do I read excel files that are placed on Azure blob storage through Azure functions.
Appreciate any inputs!
Regards,
Rameshwari
推荐答案
我可以使用下面的代码使用OpenXML读取excel文件,我创建了
< a href ="https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-storage-blob-triggered-function">
Blob触发Azure功能当检测到新的或更新的blob时,trigger会启动一个函数。 blob内容作为函数的输入提供。
I am able to read the excel file using OpenXML with the below code, I have created a Blob trigger Azure Function which trigger starts a function when a new or updated blob is detected. The blob contents are provided as input to the function.
using System.Data;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Linq;
namespace ExcelReaderfromBlob
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([BlobTrigger("funccontainer/{name}", Connection = "conn")]Stream myBlob, string name, ILogger log)
{
log.LogInformation(
" C#Blob触发器函数已处理blob \名称:{name} \ n大小:{myBlob.Length} Bytes");
using(SpreadsheetDocument doc = SpreadsheetDocument.Open(myBlob,false))
{
WorkbookPart workbookPart = doc.WorkbookPart;
SharedStringTablePart sstpart = workbookPart.GetPartsOfType< SharedStringTablePart>()。First();
SharedStringTable sst = sstpart.SharedStringTable;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
工作表sheet = worksheetPart.Worksheet;
var cells = sheet.Descendants< Cell>();
var rows = sheet.Descendants< Row>();
log.LogInformation(string.Format(" Row count = {0}",rows.LongCount()));
log.LogInformation(string.Format(" Cell count = {0}",cells.LongCount()));
//一种方式:遍历工作表中的每个单元格
foreach(单元格中的单元格单元格)
{
if((cell.DataType!= null) &&(cell.DataType == CellValues.SharedString))
{
int ssid = int.Parse(cell.CellValue.Text);
string str = sst.ChildElements [ssid] .InnerText;
log.LogInformation(string.Format(" Shared string {0}:{1}",ssid,str));
}
else if(cell.CellValue!= null)
{
log.LogInformation(string.Format(" Cell contents:{0}",cell.CellValue 。文本));
}
}
}
}
}
}
"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes"); using (SpreadsheetDocument doc = SpreadsheetDocument.Open(myBlob, false)) { WorkbookPart workbookPart = doc.WorkbookPart; SharedStringTablePart sstpart = workbookPart.GetPartsOfType<SharedStringTablePart>().First(); SharedStringTable sst = sstpart.SharedStringTable; WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); Worksheet sheet = worksheetPart.Worksheet; var cells = sheet.Descendants<Cell>(); var rows = sheet.Descendants<Row>(); log.LogInformation(string.Format("Row count = {0}", rows.LongCount())); log.LogInformation(string.Format("Cell count = {0}", cells.LongCount())); // One way: go through each cell in the sheet foreach (Cell cell in cells) { if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString)) { int ssid = int.Parse(cell.CellValue.Text); string str = sst.ChildElements[ssid].InnerText; log.LogInformation(string.Format("Shared string {0}: {1}", ssid, str)); } else if (cell.CellValue != null) { log.LogInformation(string.Format("Cell contents: {0}", cell.CellValue.Text)); } } } } } }
这篇关于在Azure Blob存储上读取Excel文件的文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!