简体   繁体   English

使用JSON数组创建RDLC报告

[英]Creating RDLC report with JSON array

I am trying to create RDLC report with JSON array for my project. 我正在尝试为我的项目使用JSON数组创建RDLC报告。 This JSON array is coming from an API. JSON数组来自API。 I have made RDLC report using database as Dataset directly accesses the SQL database , but how to do with API from which a JSON array is coming. 我已经使用数据库制作了RDLC报告,因为Dataset直接访问SQL database ,但是如何处理来自JSON数组的API。

I am trying to get JSON array on a untyped datatable and then merge it to a typed datatable. 我试图在无类型数据表上获取JSON数组,然后将其合并到有类型数据表中。 Which then should show on dataset.xsd , but it is not. 然后应该显示在dataset.xsd ,但事实并非如此。

public partial class WebForm2 : System.Web.UI.Page
{
    [WebMethod]
    [System.Web.Script.Services.ScriptMethod()]
    protected void Page_Load(object sender, EventArgs e)
    {
        test();
    }

    public void test()
    {
        WebClient client = new WebClient();
        string strJSON = client.DownloadString("Json.txt");
        string chk = strJSON.Replace("\\", "");
        chk = chk.Substring(1, chk.Length - 2);
        DataTable dtUsingMethod = GetJSONToDataTableUsingNewtonSoftDll(chk);
        DataSet1.test_tableDataTable tbl = new DataSet1.test_tableDataTable();
        tbl.Merge(dtUsingMethod);
    }

    public static DataTable GetJSONToDataTableUsingNewtonSoftDll(string JSONData)
    {
        DataTable dt = (DataTable)JsonConvert.DeserializeObject(JSONData, (typeof(DataTable)));
        return dt;
    }
}

JSON format is given below: JSON格式如下:

"[{\"plant_sl_no\":\"3451\",\"batch_no\":\"3515\",\"batch_no_sl\":\"0\",\"batch_index\":\"6\",\"batch_date\":\"5\/29\/2019 3:00:00 AM\",\"recp_id\":\"M-30(COLUME)\",\"recp_name\":\"M30\",\"pdt_qty\":\"0.50\",\"truck_id\":\"KA 01 C 9771\",\"cust_id\":\"1\",\"load_sent_qty\":\"15.00\",\"site\":\"PHRE HEATER\",\"Prod1_Agg_Name\":\"10MM\",\"Prod1_Agg_Stwt\":\"260.00\",\"Prod1_Agg_Atwt\":\"270.00\",\"Prod1_Agg_Moi\":\"0.00\",\"Prod2_Agg_Name\":\"R SAND\",\"Prod2_Agg_Stwt\":\"395.00\",\"Prod2_Agg_Atwt\":\"410.00\",\"Prod2_Agg_Moi\":\"0.00\",\"Prod3_Agg_Name\":\"20 MM\",\"Prod3_Agg_Stwt\":\"300.00\",\"Prod3_Agg_Atwt\":\"300.00\",\"Prod3_Agg_Moi\":\"0.00\",\"Prod4_Agg_Name\":\"M SAND\",\"Prod4_Agg_Stwt\":\"0.00\",\"Prod4_Agg_Atwt\":\"0.00\",\"Prod4_Agg_Moi\":\"0.00\",\"Prod5_Agg_Name\":\"0\",\"Prod5_Agg_Stwt\":\"0.00\",\"Prod5_Agg_Atwt\":\"0.00\",\"Prod5_Agg_Moi\":\"0.00\",\"Prod6_Agg_Name\":\"-\",\"Prod6_Agg_Stwt\":\"0.00\",\"Prod6_Agg_Atwt\":\"0.00\",\"Prod6_Agg_Moi\":\"0.00\"},{\"plant_sl_no\":\"3451\",\"batch_no\":\"3515\",\"batch_no_sl\":\"0\",\"batch_index\":\"1\",\"batch_date\":\"5\/29\/2019 3:00:00 AM\",\"recp_id\":\"M-30(COLUME)\",\"recp_name\":\"M30\",\"pdt_qty\":\"0.50\",\"truck_id\":\"KA 01 C 9771\",\"cust_id\":\"1\",\"load_sent_qty\":\"15.00\",\"site\":\"PHRE HEATER\",\"Prod1_Agg_Name\":\"10MM\",\"Prod1_Agg_Stwt\":\"260.00\",\"Prod1_Agg_Atwt\":\"260.00\",\"Prod1_Agg_Moi\":\"0.00\",\"Prod2_Agg_Name\":\"R SAND\",\"Prod2_Agg_Stwt\":\"395.00\",\"Prod2_Agg_Atwt\":\"395.00\",\"Prod2_Agg_Moi\":\"0.00\",\"Prod3_Agg_Name\":\"20 MM\",\"Prod3_Agg_Stwt\":\"300.00\",\"Prod3_Agg_Atwt\":\"300.00\",\"Prod3_Agg_Moi\":\"0.00\",\"Prod4_Agg_Name\":\"M SAND\",\"Prod4_Agg_Stwt\":\"0.00\",\"Prod4_Agg_Atwt\":\"0.00\",\"Prod4_Agg_Moi\":\"0.00\",\"Prod5_Agg_Name\":\"0\",\"Prod5_Agg_Stwt\":\"0.00\",\"Prod5_Agg_Atwt\":\"0.00\",\"Prod5_Agg_Moi\":\"0.00\",\"Prod6_Agg_Name\":\"-\",\"Prod6_Agg_Stwt\":\"0.00\",\"Prod6_Agg_Atwt\":\"0.00\",\"Prod6_Agg_Moi\":\"0.00\"},{\"plant_sl_no\":\"3451\",\"batch_no\":\"3515\",\"batch_no_sl\":\"0\",\"batch_index\":\"2\",\"batch_date\":\"5\/29\/2019 3:00:00 AM\",\"recp_id\":\"M-30(COLUME)\",\"recp_name\":\"M30\",\"pdt_qty\":\"0.50\",\"truck_id\":\"KA 01 C 9771\",\"cust_id\":\"1\",\"load_sent_qty\":\"15.00\",\"site\":\"PHRE HEATER\",\"Prod1_Agg_Name\":\"10MM\",\"Prod1_Agg_Stwt\":\"260.00\",\"Prod1_Agg_Atwt\":\"260.00\",\"Prod1_Agg_Moi\":\"0.00\",\"Prod2_Agg_Name\":\"R SAND\",\"Prod2_Agg_Stwt\":\"395.00\",\"Prod2_Agg_Atwt\":\"395.00\",\"Prod2_Agg_Moi\":\"0.00\",\"Prod3_Agg_Name\":\"20 MM\",\"Prod3_Agg_Stwt\":\"300.00\",\"Prod3_Agg_Atwt\":\"300.00\",\"Prod3_Agg_Moi\":\"0.00\",\"Prod4_Agg_Name\":\"M SAND\",\"Prod4_Agg_Stwt\":\"0.00\",\"Prod4_Agg_Atwt\":\"0.00\",\"Prod4_Agg_Moi\":\"0.00\",\"Prod5_Agg_Name\":\"0\",\"Prod5_Agg_Stwt\":\"0.00\",\"Prod5_Agg_Atwt\":\"0.00\",\"Prod5_Agg_Moi\":\"0.00\",\"Prod6_Agg_Name\":\"-\",\"Prod6_Agg_Stwt\":\"0.00\",\"Prod6_Agg_Atwt\":\"0.00\",\"Prod6_Agg_Moi\":\"0.00\"}]"

Expected result is I want to generate report using JSON array. 预期结果是我想使用JSON数组生成报告。

Ok I found an answer which is working for me. 好的,我找到了一个对我有用的答案。 I am posting it here hopefully it would be helpful for others also. 我希望将其发布在这里,这对其他人也有帮助。 The problem here is we need datasource for the RDLC report. 这里的问题是我们需要RDLC报告的数据源。 Since we cannot use Sql database we have to make our own custom datasource. 由于我们无法使用Sql数据库,因此必须创建自己的自定义数据源。

Step 1. add a class file. 步骤1.添加一个类文件。

Step 2. Code within class file: 步骤2.在类文件中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using Newtonsoft.Json.Serialization;
using System.Web.Services;
using System.Net;

namespace Batch_report
{
    public class batch_data 
    {
        public string plant_sl_no { get; set; }
        public string batch_no { get; set; }
        public string batch_no_sl { get; set; }
        public string batch_index { get; set; }
        public string batch_date { get; set; }
        public string recp_id { get; set; }
        public string recp_name { get; set; }
        public string pdt_qty { get; set; }
        public string truck_id { get; set; }
        public string cust_id { get; set; }
        public string load_sent_qty { get; set; }
        public string site { get; set; }
    }
    public class Custom_batch
    {
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod()]
        public static List<batch_data> GetEmployeeName()
        {
            //List<string> abc = new List<string>();
            //string[] arr;
            WebClient client = new WebClient();
            string info = client.DownloadString("http://json.txt");
            string chk = info.Replace("\\", "");
            chk = chk.Substring(1, chk.Length - 2);

            List<batch_data> obj = new List<batch_data>();
            obj = JsonConvert.DeserializeObject<List<batch_data>>(chk);

            return obj;
        }
    }
}

Step 3. Build your class file, and you will get a batch_report.dll file. 步骤3.构建您的类文件,您将获得batch_report.dll文件。 Add this .dll to the reference folder of your project. 将此.dll添加到项目的参考文件夹中。

Step 4. Add report or report wizard from the toolbox. 步骤4.从工具箱添加报告或报告向导。 When you add a window will open asking for dataset. 添加后,将打开一个窗口,询问数据集。 name your dataset, then in datasource select batch_report from the dropdown list.In "Available Datasets"select the method from which you are getting data which in my case is from Custom_batch class and method is - GetEmployeeName(). 命名您的数据集,然后在数据源中从下拉列表中选择batch_report。在“可用数据集”中,选择要从中获取数据的方法,在我的情况下,该方法来自Custom_batch类,方法是-GetEmployeeName()。

Step 5. click Next until it is done and after that yo can design your report as well. 步骤5.单击“下一步”直到完成,然后您也可以设计报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM