gpt4 book ai didi

azure - 下载 Excel 文件并使用 azure 函数读取内容

转载 作者:行者123 更新时间:2023-12-04 14:09:26 26 4
gpt4 key购买 nike

我正在尝试编写一个 C# Azure 函数来使用 OpenXml-SDK 下载并打开 Excel 文件。

Office Interop 在这里不起作用,因为 Office 不可用于 Azure 功能。

我正在尝试使用 OpenXml-SDK 打开并读取该文件,该文件似乎需要已保存文件的路径,而不是 url 或从远程 url 下载的流。

由于我不知道如何在 Azure Functions 中临时存储 excel 文件,因此我使用了 Azure 文件存储。

我已将 excel 文件从 url 上传到 Azure 文件存储,但无法使用 OpenXML-SDK 打开该 excel 文件。

我测试了 Azure 文件存储中的 Excel 文件是否正常工作,但是,当我尝试从 MemoryStream 打开 OpenXML.SpreadsheetDocument 时,出现错误,指示文件已损坏。

如果我尝试打开 SpreadsheetDocument 并传递文件 Uri ( https://learn.microsoft.com/en-us/azure/storage/storage-dotnet-how-to-use-files#develop-with-file-storage ),则该地址将超过 260 个字符的限制。

我愿意使用 OpenXML 以外的库,理想情况下我不希望必须存储 excel 文件。

最佳答案

Open XML SDK 在 Azure Function 中运行良好。我在我这边测试了一下。这是完整的代码。

#r "DocumentFormat.OpenXml.dll"
#r "WindowsBase.dll"

using System.Net;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");

WebClient client = new WebClient();

byte[] buffer = client.DownloadData("http://amor-webapp-test.azurewebsites.net/Content/hello.xlsx");
MemoryStream stream = new MemoryStream();
stream.Write(buffer, 0, buffer.Length);
stream.Position = 0;
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(stream, 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.Info(string.Format("Row count = {0}", rows.LongCount()));
log.Info(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.Info(string.Format("Shared string {0}: {1}", ssid, str));
}
else if (cell.CellValue != null)
{
log.Info(string.Format("Cell contents: {0}", cell.CellValue.Text));
}
}
}

return req.CreateResponse(HttpStatusCode.OK, "Hello ");
}

enter image description here

要使用Open XML,请确保您已在函数文件夹下创建了bin文件夹,并将DocumentFormat.OpenXml.dll和WindowsBase.dll上传到其中。

"File contains corrupted data".

您是否尝试过另一个 Excel 文件来检查问题是否与特定 Excel 文件有关。我建议您创建一个新的简单 Excel 来再次测试您的代码。

"It didn't work on my file with the same "File contains corrupted data" message. "

我下载了您的 Excel 文件,发现它是旧版本(.xls)的 Excel 文件。

要修复此异常,您可以将 Excel 转换为最新版本 (.xlsx) 或选择其他 Excel 解析库。 ExcelDataReader可以适用于任何版本的 Excel 文件。您可以使用 NuGet 通过搜索“ExcelDataReader”来安装此库。以下是如何解析.xls格式excel文件的示例代码。我在Azure Function上测试了它,效果很好。

#r "Excel.dll"
#r "System.Data"

using System.Net;
using System.IO;
using Excel;
using System.Data;

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");

WebClient client = new WebClient();

byte[] buffer = client.DownloadData("http://amor-webapp-test.azurewebsites.net/Content/abcdefg.xls");
MemoryStream stream = new MemoryStream();
stream.Write(buffer, 0, buffer.Length);
stream.Position = 0;

IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);

DataSet result = excelReader.AsDataSet();

for (int i = 0; i < result.Tables.Count; i++)
{
log.Info(result.Tables[i].TableName +" has " + result.Tables[i].Rows.Count + " rows.");
}

return req.CreateResponse(HttpStatusCode.OK, "Hello ");
}

在执行上面的代码之前,请将“Excel.dll”文件添加到您的函数的bin文件夹中。

关于azure - 下载 Excel 文件并使用 azure 函数读取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43171845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com