gpt4 book ai didi

jquery - 如何从struts2网页导出jqgrid到excel

转载 作者:行者123 更新时间:2023-12-01 05:01:41 25 4
gpt4 key购买 nike

抱歉,我是堆栈溢出的新手,所以我不知道如何发布 struts2 jsp 网页,但我的问题是我使用 j 查询创建网格,我想将该网格导出到 Excel 工作表中。我将如何使用 struts、j query、jqgrid 来实现。

这是我的 JGrid 代码

<script>


function exportExcel()
{
var mya=new Array();
mya=$("#sample_grid").jqGrid('getDataIDs', {}); // Get All IDs
var data=$("#sample_grid").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(i=0;i<mya.length;i++)
{
data=$("#sample_grid").getRowData(mya[i]); // get each row
for(j=0;j<colNames.length;j++)
{
html=html+data[colNames[j]]+"\t"; // output each column as tab delimited
}
html=html+"\n"; // output each row with end of line

}
html=html+"\n"; // end of line at the end
alert(html);
document.forms[0].csvBuffer.value=html;
document.forms[0].method='POST';
document.forms[0].action='csvExport.jsp'; // send it to server which will open this contents in excel file
document.forms[0].target='_blank';
document.forms[0].submit();
}

<s:url id="loadGrid" action="loadGrid" namespace="/"/>

<sjg:grid  href="%{loadGrid}" 
gridModel="gridModel"
caption="Grid Sample"
navigator="true"
navigatorAdd="false"
navigatorDelete="false"
navigatorEdit="false"
navigatorRefresh="false"
navigatorSearch="false"
navigatorView="false"
pager="true"
navigatorExtraButtons="{
add : {
caption : 'Export to Excel',
title : 'Export to Excel',
icon : 'ui-icon-save',
onclick: function() {
exportExcel();
}
}
}">
<sjg:gridColumn title="Name" name="name" align="left"/>
<sjg:gridColumn title="City" name="city" />
<sjg:gridColumn title="Country" name="country"/>
<sjg:gridColumn title="Credit Limit" name="creditLimit"/>

</sjg:grid>

我没有在 function() 内的警报中获取 html 值

最佳答案

$("#list").jqGrid('navGrid', '#pager',{view:true, del:false, add:false,
edit:false, excel:true})
.navButtonAdd('#pager',{
caption:"Export to Excel",
buttonicon:"ui-icon-save",
onClickButton: function(){
exportExcel();
},
position:"last"
});

function exportExcel()
{
var mya=new Array();
mya=$("#list").getDataIDs(); // Get All IDs
var data=$("#list").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(i=0;i<mya.length;i++)
{
data=$("#list").getRowData(mya[i]); // get each row
for(j=0;j<colNames.length;j++)
{
html=html+data[colNames[j]]+"\t"; // output each column as tab delimited
}
html=html+"\n"; // output each row with end of line

}
html=html+"\n"; // end of line at the end
document.forms[0].csvBuffer.value=html;
document.forms[0].method='POST';
document.forms[0].action='csvExport.jsp'; // send it to server which will open this contents in excel file
document.forms[0].target='_blank';
document.forms[0].submit();
}

JSP:-

引用此链接:- http://www.vaannila.com/struts/struts-example/struts-export-jsp-to-excel-example-1.html

关于jquery - 如何从struts2网页导出jqgrid到excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9510642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com