Open XML SDK在Azure功能中正常工作。我在我身边测试了它。这是完整的代码。
#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 ");
}
使用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文件,发现它是excel文件的旧版本(.xls)。
要修复异常,可以将excel转换为最新版本(.xlsx)或选择另一个excel解析库。 ExcelDataReader可以用于任何版本的Excel文件。你可以通过搜索'ExcelDataReader'来使用NuGet来安装这个库。以下是如何解析.xls格式excel文件的示例代码。我在Azure功能上测试它,它确实工作正常。
#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中打开它。我还能够在本地使用Office interop COM正确下载,读取和解析它。在这一点上,问题可能与OpenXML有关。如果您可以使用我的文件进行测试,那将是非常好的:http://www2.nationalgrid.com/WorkArea/DownloadAsset.aspx?id=8589936879或者如果您会推荐OpenXML的替代方案。谢谢vm – donquijote
感谢您的反馈。我根据您的评论更新了我的回复。 – Amor
未安装的字体如何? –