jqGrid - 使用PHP服务器导出到Excel [英] jqGrid - Export to Excel with PHP server
问题描述
我已经将jqGrid设置为DataTable的替代品,但是我缺少的一个重要部分是能够导出到Excel。 DataTable使用html5来处理这个问题,并且使用Flash作为备份方法(我根本不想使用flash)。
I have setup jqGrid as a replacement for DataTables, but one important piece I'm missing is the ability to Export to Excel. DataTables handles this with html5 and uses Flash as a backup method (I do not wish to use flash at all).
在搜索jqGrid的方法时看到几个相当老的帖子,但最近没有。我正在使用来自JSON文件的数据的php服务器后端。我看到付费版本的jqGrid有一个php版本。有没有一种与jqGrid免费版本兼容的方法,我可以使用它来导出一个真正的Excel文件(而不是一个csv)?
When searching for a way to do this with jqGrid I see several posts that are quite old, but nothing recent. I am using a php server backend with the data sourced from JSON files. I see the paid version of jqGrid has a php version. Is there a method compatible with the free version of jqGrid that I can use to export a real Excel file (not a csv)?
推荐答案
p> jqGrid的按钮
Button for jqGrid
$grid.jqGrid("navButtonAdd", {
caption: "Export<br />To Excel",
buttonicon: "ui-pg-button-text ui-pg-button-icon-over-text fa-file-excel-o",
title: "Export to Excel",
onClickButton: function () {
exportExcel();
}
});
表单输入的HTML
<form id='_export' method="post" action="jqGrid/export_excel.php">
<input type="hidden" name="csvBuffer" id="csvBuffer" value="" />
</form>
用于导出到Excel功能的JAVASCRIPT
JAVASCRIPT for Export to Excel function
function exportExcel()
{
var keys=[], ii=0, rows="";
var ids=$("#list").getDataIDs();
var row=$("#list").getRowData(ids[0]);
var p = $("#list").jqGrid("getGridParam");
var rows="<table><thead><tr>";
for (var k in row) {
keys[ii++]=k;
rows=rows+'<th>'+p.colNames[ii+1]+'</th>';
}
rows=rows+"</tr></thead><tbody>";
for(i=0;i<ids.length;i++) {
row=$("#list").getRowData(ids[i]);
rows=rows+"<tr>";
for(j=0;j<keys.length;j++){
rows=rows+'<td>'+row[keys[j]]+'</td>';
}
rows=rows+"</tr></tbody>";
}
rows=rows+"</table>";
document.getElementById('csvBuffer').value=rows;
document.getElementById('_export').submit();
}
下载XLS文件的PHP
PHP for Downloading XLS file
$buffer = $_POST['csvBuffer'];
// file name for download
$filename = "baplie_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
try{
echo $buffer;
}catch(Exception $e){
}
这篇关于jqGrid - 使用PHP服务器导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!