如何使用C#使用JSON数据创建Excel文件? [英] How to create excel file using json data using c#?

查看:196
本文介绍了如何使用C#使用JSON数据创建Excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有示例json数据,并希望使用该数据创建一个xls文件.

I have sample json data and want to create a xls file using that.

"{\"Id\":\"123\",\"Name\":\"DEMO\",\"Address\":\"US\",\"Team\":\"JK\"}"

要创建一个excel文件流,我将使用以下代码将其上传到azure存储上-

Want to create a excel file stream which I will upload on azure storage using below code -

CloudFile cloudFile = cloudFileDirectory.GetFileReference("filename.xls");
cloudFile.UploadFromStream(fileStream);

预期输出-

我可以通过以下代码创建csv-

I'm able to create csv by below code -

var result = new StringBuilder();
    for (int i = 0; i < table.Columns.Count; i++)
    {
        result.Append(table.Columns[i].ColumnName);
        result.Append(i == table.Columns.Count - 1 ? "\n" : delimator);
    }
    foreach (DataRow row in table.Rows)
    {
        for (int i = 0; i < table.Columns.Count; i++)
        {
            result.Append(row[i].ToString());
            result.Append(i == table.Columns.Count - 1 ? "\n" : delimator);
        }
    }
    return result.ToString().TrimEnd(new char[] { '\r', '\n' });

推荐答案

要从json文件生成xls文件,您应该执行以下步骤

to generate xls file from json files you should done those steps

  1. 读取文件
  2. 将JSON文件( Json.NET 最好)解析为c#类对象.
  3. li>
  4. 选择xls框架(使用Open XML SDK或您发现的任何人)
  5. 使用第2步中的结构,并使用第3步中的框架API填充xls文件中的列和行.
  1. Read file
  2. Parse JSON file (Json.NET is the best) to your c# Class object.
  3. Choose xls framework(using Open XML SDK or anyone which do you find)
  4. Use structure from step 2 to fill columns and rows in xls file using framework API from step 3.

这篇关于如何使用C#使用JSON数据创建Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆