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

我是jQuery / jQgrid编码的新手。 我使用的是jQgrid版本是4.4.4和jQuery 1.8.3。 我想在我的jQgrid中启用导出到PDF / EXCELfunction。 为此我提到了以下链接 – 点击这里并点击这里 。 在这个链接的基础上,我在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' }); 

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

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

在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 = "" + "" + "" + ""; for ( var k = 0; k < colNames.length; k++) { html = html + "" + colNames[k] + ""; } html = html + ""; // Output header with end of line for (i = 0; i < mya.length; i++) { html = html + ""; data = $("#" + table).getRowData(mya[i]); // get each row for ( var j = 0; j < colNames.length; j++) { html = html + "" + data[colNames[j]] + ""; // output each Row as // tab delimited } html = html + ""; // output each row with end of line } html = html + ""; // end of line at the end alert(html); html = html.replace(/'/g, '''); // var form = "
"; // form = form + ""; // form = form + "

如果您使用的是PHP,请尝试使用phpGrid 。 那你只需要打电话

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

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

这是一个聪明的解决方案,将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 

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

  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); } } 

强文