[英]How to enable jQgrid to Export data into PDF/Excel
我是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'
});
但是這段代碼不能正常工作。 我在互聯網谷歌搜索了很多,但我沒有得到有用的和相關的信息來完成我的任務。 有誰知道怎么做?
更新:我沒有使用jqgrid的付費版本。
這是一個聰明的解決方案,將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();
}
我們首先創建一個用以下分隔的CSV
字符串;
。 然后使用某些屬性創建anchor
標記。 最后在a
上調用click
來下載文件。
在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="css/text">"
+ "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="page:land;">";
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, ''');
// 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();
}
如果您使用的是PHP,請嘗試使用phpGrid 。 然后你只需要打電話
$dg->enable_export('PDF'); // for excel: $dg->enable_export('EXCEL');
查看http://phpgrid.com/example/export-datagrid-to-excel-or-html 。 它生成渲染網格所需的jqGrid javascript。
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);
}
}
強文
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.