jQuery 如何启用 jQgrid 将数据导出到 PDF/Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21494761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:36:17  来源:igfitidea点击:

How to enable jQgrid to Export data into PDF/Excel

jqueryjqgridexport-to-excelexport-to-pdf

提问by Rahul

I am new in jQuery/jQgrid coding. I am using jQgrid version is 4.4.4 & jQuery 1.8.3. I want to enable export to PDF/EXCEL functionality in my jQgrid. For that I referred following links - Click Hereand Click Here. On the basis of this links, I developed few lines of code in jquery which is as follows:

我是 jQuery/jQgrid 编码的新手。我使用的 jQgrid 版本是 4.4.4 & jQuery 1.8.3。我想在我的 jQgrid 中启用导出到 PDF/EXCEL 功能。为此,我提到了以下链接 -单击此处单击此处。在此链接的基础上,我在 jquery 中开发了几行代码,如下所示:

   .jqGrid('navGrid', topPagerSelector, { edit: false, add: false, del: false, search: false, pdf: true}, {}, {}, {}, {}
   }).jqGrid('navButtonAdd',topPagerSelector,{
    id:'ExportToPDF',
    caption:'',
    title:'Export To Pdf',
    onClickButton : function(e)
    {
        try {
            $("#tbPOIL").jqGrid('excelExport', { tag: 'pdf', url: sRelativePath + '/rpt/poil.aspx' });
        } catch (e) {
            window.location = sRelativePath + '/rpt/poil.aspx&oper=pdf';
        }
    },
    buttonicon: 'ui-icon-print'
});

But this code is not working properly. I searched on internet google a lot but I am not getting useful & relevant info to achieve my task. Is anyone know how to do this???

但是这段代码不能正常工作。我在互联网谷歌上搜索了很多,但我没有得到有用和相关的信息来完成我的任务。有人知道怎么做吗???

UPDATE:I a am not using paid version of jqgrid.

更新:我没有使用 jqgrid 的付费版本。

采纳答案by Vinoth Krishnan

function to be called inside of your onclick event.

在 onclick 事件中调用的函数。

function exportGrid(){
  mya = $("#" + table).getDataIDs(); // Get All IDs
var data = $("#" + table).getRowData(mya[0]); // Get First row to get the
// labels
var colNames = new Array();
var ii = 0;
for ( var i in data) {
    colNames[ii++] = i;
} // capture col names

var html = "<html><head>"
        + "<style script=&quot;css/text&quot;>"
        + "table.tableList_1 th {border:1px solid black; text-align:center; "
        + "vertical-align: middle; padding:5px;}"
        + "table.tableList_1 td {border:1px solid black; text-align: left; vertical-align: top; padding:5px;}"
        + "</style>"
        + "</head>"
        + "<body style=&quot;page:land;&quot;>";


for ( var k = 0; k < colNames.length; k++) {
    html = html + "<th>" + colNames[k] + "</th>";
}
html = html + "</tr>"; // Output header with end of line
for (i = 0; i < mya.length; i++) {
    html = html + "<tr>";
    data = $("#" + table).getRowData(mya[i]); // get each row
    for ( var j = 0; j < colNames.length; j++) {
     html = html + "<td>" + data[colNames[j]] + "</td>"; // output each Row as
                // tab delimited
    }
    html = html + "</tr>"; // output each row with end of line
}
html = html + "</table></body></html>"; // end of line at the end
alert(html);
html = html.replace(/'/g, '&apos;');
//  var form = "<form name='pdfexportform' action='generategrid' method='post'>";
//  form = form + "<input type='hidden' name='pdfBuffer' value='" + html + "'>";
//  form = form + "</form><script>document.pdfexportform.submit();</sc"
//      + "ript>";
//  OpenWindow = window.open('', '');
//  OpenWindow.document.write(form);
//  OpenWindow.document.close();
}

回答by Suhail Gupta

Here is a clever solution to save the jqGriddata as excel sheet: (You just need to call this function with GridIDand an optional Filename)

这是将jqGrid数据另存为 Excel 工作表的巧妙解决方案:(您只需要使用GridID和可选的调用此函数Filename

var createExcelFromGrid = function(gridID,filename) {
    var grid = $('#' + gridID);
    var rowIDList = grid.getDataIDs();
    var row = grid.getRowData(rowIDList[0]); 
    var colNames = [];
    var i = 0;
    for(var cName in row) {
        colNames[i++] = cName; // Capture Column Names
    }
    var html = "";
    for(var j=0;j<rowIDList.length;j++) {
        row = grid.getRowData(rowIDList[j]); // Get Each Row
        for(var i = 0 ; i<colNames.length ; i++ ) {
            html += row[colNames[i]] + ';'; // Create a CSV delimited with ;
        }
        html += '\n';
    }
    html += '\n';

    var a         = document.createElement('a');
    a.id = 'ExcelDL';
    a.href        = 'data:application/vnd.ms-excel,' + html;
    a.download    = filename ? filename + ".xls" : 'DataList.xls';
    document.body.appendChild(a);
    a.click(); // Downloads the excel document
    document.getElementById('ExcelDL').remove();
}

We first create a CSVstring delimited with ;. Then an anchortag is created with certain attributes. Finally clickis called on ato download the file.

我们首先创建一个以CSV分隔的字符串;。然后anchor创建具有某些属性的标签。最后click被调用a来下载文件。

回答by devXen

If you are on PHP, try phpGrid. Then you just need to call

如果您使用 PHP,请尝试phpGrid。然后你只需要打电话

$dg->enable_export('PDF'); // for excel: $dg->enable_export('EXCEL'); 

check out http://phpgrid.com/example/export-datagrid-to-excel-or-html. It generates jqGrid javascript required for rendering grid.

查看http://phpgrid.com/example/export-datagrid-to-excel-or-html。它生成渲染网格所需的 jqGrid javascript。

回答by JosephK

    if (exportexcel.Equals(excel) )
   {
        GridView view = new GridView();
        string conn = @"Server=localhost;port=3306;Database=jtext;Uid=root;Password=techsoft";
        IFormatProvider culture = new System.Globalization.CultureInfo("fr-Fr", true);
        MySqlConnection con = new MySqlConnection(conn);
        con.Open();
        MySqlCommand cmd = new MySqlCommand(query, con);
        MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        view.DataSource = ds;
        view.DataBind();
        con.Close();
        HttpContext Context = HttpContext.Current;
        context.Response.Write(Environment.NewLine);
        context.Response.Write(Environment.NewLine);
        context.Response.Write(Environment.NewLine);
        DateTime ss = DateTime.Now;
        string custom = ss.ToString("dd-MM-yyyy");
        string sss = DateTime.Now.ToString("HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
        string aaa = "Generated Date and Time : " + custom + "  " + sss;
        context.Response.Write(aaa);
        context.Response.Write(Environment.NewLine);
        foreach (DataColumn column in ds.Tables[0].Columns)
        {
            context.Response.Write(column.ColumnName + " ,");
        }
        context.Response.Write(Environment.NewLine);
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
            {
                context.Response.Write(row[i].ToString().Replace(" ", string.Empty).Replace(",", " ") + " ,");
             }
            context.Response.Write(Environment.NewLine);
        }
        string attachment = "attachment; filename= " + rolefullname + ".xls";
        context.Response.ContentType = "application/csv";
        context.Response.AppendHeader("Content-Disposition", attachment);
         }
       }

strong text

强文本