【问题标题】:How to convert gridview data to Json?如何将gridview数据转换为Json?
【发布时间】:2014-01-05 12:33:05
【问题描述】:

我有一个任务,我必须使用数据库中的数据创建一个网格视图,并将信息从 Gridview 导出到 Pdf 和 Json。

我已经完成了 PDF 部分,但现在我被困在 Json 部分,我不知道如何实现它。

var json = new JavaScriptSerializer().Serialize(GridViewStudents.Rows);
Response.ContentType = "application/json; charset=utf-8";
Response.AppendHeader("content-disposition", "attachment;filename=Students.txt");
Response.Write(json);
Response.End();

这就是我想要做的。我需要先将数据从

  1. Gridview 转 Json

  2. 将数据导出到某个文件,例如 txt 文件,该文件将显示可转换的数据。

我已经编写的代码给出了错误说明

在序列化“System.Web.UI.WebControls.GridViewRow”类型的对象时检测到循环引用

谁能帮我解决这个问题并找到合适的解决方案。

谢谢。

【问题讨论】:

  • 循环网格中的每一行并找到行内的每个控件并将其添加到表中 - 意味着将网格数据转换为表并将该表导出为 json

标签: c# asp.net json gridview


【解决方案1】:

包括这些文件

<script src="~/Scripts/jquery.base64.js"></script>
<script src="~/Scripts/tableExport.js"></script>
<script src="~/Scripts/jspdf/libs/base64.js"></script>
<script src="~/Scripts/jspdf/jspdf.js"></script>
<script src="~/Scripts/jspdf/FileSaver.js"></script>
<script src="~/Scripts/jspdf/jspdf.plugin.cell.js"></script>

然后将这些函数添加到您的脚本中

function ExportTpGridtoPDF(divid) {
    var table1 =
   tableToJson($('#' + divid + ' .grid-table').get(0)),
   cellWidth = 35,
   rowCount = 0,
   cellContents,
   //leftMargin = 2,
   //topMargin = 12,
   //topMarginTable = 55,
   //headerRowHeight = 13,
   //rowHeight = 9,
   leftMargin = 10,
   topMargin = 15,
   topMarginTable = 5,
   headerRowHeight = 13,
   rowHeight = 13,

    l = {
        orientation: 'l',
        unit: 'mm',
        format: 'a3',
        compress: true,
        fontSize: 8,
        lineHeight: 1,
        autoSize: false,
        printHeaders: true
    };

    var doc = new jsPDF(l, '', '', '');

    doc.setProperties({
        title: 'Test PDF Document',
        subject: 'This is the subject',
        author: 'author',
        keywords: 'generated, javascript, web 2.0, ajax',
        creator: 'author'
    });

    doc.cellInitialize();

    $.each(table1, function (i, row) {

        rowCount++;

        $.each(row, function (j, cellContent) {
            if (rowCount == 1) {
                doc.margins = 1;
                doc.setFontSize(12);

                doc.cell(leftMargin, topMargin, cellWidth, headerRowHeight, cellContent, i)
            }
            else if (rowCount == 2) {
                doc.margins = 1;
                doc.setFontSize(12);

                doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i);
            }
            else {

                doc.margins = 1;
                doc.setFontSize(12);

                doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i);
            }
        })
    })

    doc.save('sample Report.pdf');
}

function tableToJson(table) {
    var data = [];

    // first row needs to be headers
    var headers = [];
    for (var i = 0; i < table.rows[0].cells.length; i++) {
        if (table.rows[0].cells[i].innerHTML != "") {
            headers[i] = table.rows[0].cells[i].innerText.toLowerCase().replace(/ /gi, '');
        }
    }
    // go through cells
    for (var i = 1; i < table.rows.length; i++) {
        var tableRow = table.rows[i];
        var rowData = {};
        for (var j = 1; j < tableRow.cells.length; j++) {
            rowData[headers[j]] = tableRow.cells[j].innerText;
        }
        data.push(rowData);
    }
    return data;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 2017-05-14
    • 2011-11-30
    • 2019-08-11
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多