excel - 如何在 Azure 逻辑应用中解析 Excel 电子表格

标签 excel azure parsing

我需要使用 Azure 逻辑应用从 Excel 电子表格中解析和提取列信息

我已经为我的逻辑应用程序设置了从 Outlook 检索最新未读电子邮件的功能。此外,我的逻辑应用程序执行 FOR EACH 来读取所有附件(来自未读电子邮件)并确保它们是 Excel 文件(基于文件扩展名)。

我有一个基本的 Excel 文件,其中包含 3 列“产品、描述、价格”,我需要解析每一行(仅产品和价格)列。

我将添加将解析的数据存储到 Azure 上托管的 SQL 表中的功能。

最佳答案

在这里,您也可以使用 HTTP 请求。

enter image description here

using ExcelDataReader;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Nancy.Json;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ConvertExcelToJSon
{
    public static class Function1
    {
        [FunctionName("ConvertToJson")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");


            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            string blobName = data?.blobName;

            string[] splitBlob = blobName.Split('/');

            Stream blob = await GetBlobStreamAsync(splitBlob[1], splitBlob[2] + "/" + splitBlob[3]);
            DataSet ds = CreateDataSet(blob);
            List<Simple> simpleList = new List<Simple>();
            foreach (DataTable table in ds.Tables)
            {

                return (ActionResult)new OkObjectResult(DataTableToJSON(table));

            }


            return blobName != null
            ? (ActionResult)new OkObjectResult($"Hello, {blobName}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }



        public static string DataTableToJSON(DataTable table)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            foreach (DataRow row in table.Rows)
            {
                Dictionary<string, object> dict = new Dictionary<string, object>();

                foreach (DataColumn col in table.Columns)
                {
                    dict[col.ColumnName] = (Convert.ToString(row[col]));
                }
                list.Add(dict);
            }
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            return serializer.Serialize(list);
        }

        public static string AppSetting(this string Key)
        {
            string ret = string.Empty;
            if (Environment.GetEnvironmentVariable(Key) != null)
            {
                ret = Environment.GetEnvironmentVariable(Key);
            }
            return ret;
        }

        public static async Task<Stream> GetBlobStreamAsync(string containerName, string blobName)
        {
            Stream myBlob = new MemoryStream();
            if (CloudStorageAccount.TryParse("AzureWebJobsStorage".AppSetting(), out CloudStorageAccount storageAccount))
            {
                CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName);
                CloudBlob myBloab = container.GetBlobReference(blobName);
                await myBloab.DownloadToStreamAsync(myBlob);
                myBlob.Seek(0, SeekOrigin.Begin);
            }
            return myBlob;
        }
        public static DataSet CreateDataSet(Stream stream)
        {
            DataSet ds;
            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
            IExcelDataReader reader = null;
            reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            ds = reader.AsDataSet(new ExcelDataSetConfiguration()
            {
                ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
                {
                    UseHeaderRow = true,
                }
            });
            return ds;
        }
    }
}

关于excel - 如何在 Azure 逻辑应用中解析 Excel 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56761196/

相关文章:

Excel VBA : Pass `Target` Variable from Click Event to Userform

azure - 通过 C# 将数据插入到 Azure 数据资源管理器

parsing - 不区分大小写的 Scala 解析器组合器

java - 了解 Java 的 DOM 解析器

excel - 在 vba 中缩进 mpp 文件(Ms 项目文件)中的任务(excel 到 mpp 文件)

jquery - 将 Excel 数据复制到 Web 表单上的输入

vba - 运行时错误1004 excel找不到文本文件来刷新此外部范围

sql-server - 从 Office 365 连接到 Azure SQL 数据库/SQL Server

azure - Microsoft Application Insights 不显示自定义属性

javascript - 将正则表达式转换为掩码输入 - Javascript