Javascript 在jquery或jqGrid中导出到excel
我有一个可以从服务器(java)以JSON格式立即获取数据的程序。我希望将jqGrid中的数据导出为Excel格式 到目前为止,我在IEJavascript 在jquery或jqGrid中导出到excel,javascript,jquery,excel,jquery-plugins,jqgrid,Javascript,Jquery,Excel,Jquery Plugins,Jqgrid,我有一个可以从服务器(java)以JSON格式立即获取数据的程序。我希望将jqGrid中的数据导出为Excel格式 到目前为止,我在IE“o.url为null或不是对象”grid.import.js中看到了这个错误 我还看到export按钮的工具提示上写着导出到Excel,但保存的文件是xml格式的 因此,我希望有任何建议可以使用javascript或jquery插件或jqgrid的内置功能将我的JSON字符串转换为excel 我的jqGrid 我的jqGrid代码 grid = jQuery
“o.url为null或不是对象”grid.import.js中看到了这个错误
我还看到export按钮的工具提示上写着导出到Excel
,但保存的文件是xml格式的
因此,我希望有任何建议可以使用javascript或jquery插件或jqgrid的内置功能将我的JSON字符串转换为excel
我的jqGrid
我的jqGrid代码
grid = jQuery("#list2");
grid.jqGrid({
datastr : comparePatchData,
datatype: 'jsonstring',
colNames:['Name',starheader, header1, header2],
colModel:[
{name:'elementName',index:'elementName', width:90},
{name:'isPrasentinXml1',index:'isPrasentinXml1', width:100, align:'center', formatter: patchPresent},
{name:'isPrasentinXml2',index:'isPrasentinXml2', width:100, align:'center', formatter: patchPresent},
{name:'isPrasentinXml3',index:'isPrasentinXml3', width:100, align:'center', formatter: patchPresent}
],
pager : '#gridpager2',
rowNum:12,
scrollOffset:0,
height: 320,
autowidth:true,
viewrecords: true,
gridview: true,
loadonce:true,
jsonReader: {
repeatitems: false,
page: function() { return 1; },
root: "response"
},
subGrid: true,
// define the icons in subgrid
subGridOptions: {
"plusicon" : "ui-icon-triangle-1-e",
"minusicon" : "ui-icon-triangle-1-s",
"openicon" : "ui-icon-arrowreturn-1-e",
//expand all rows on load
"expandOnLoad" : false
},
subGridRowExpanded: function(subgrid_id, row_id) {
//console.info(subgrid_id+", "+row_id);
var subgrid_table_id, pager_id, iData = -1;
subgrid_table_id = subgrid_id+"_t";
//pager_id = "p_"+subgrid_table_id;
$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' style='overflow-y:auto' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
$.each(comparePatchData.response,function(i,item){
if(item.id === row_id) {
iData = i;
return false;
}
});
if (iData == -1) {
return; // no data for the subgrid
}
jQuery("#"+subgrid_table_id).jqGrid({
datastr : comparePatchData.response[iData],
datatype: 'jsonstring',
colNames: ['Name','Value1','Value2','Value3'],
colModel: [
{name:"name",index:"name",width:90},
{name:"firstValue",index:"firstValue",width:100},
{name:"secondValue",index:"secondValue",width:100},
{name:"thirdValue",index:"thirdValue",width:100}
],
rowNum:10,
//pager: pager_id,
sortname: 'name',
sortorder: "asc",
height: 'auto',
autowidth:true,
jsonReader: {
repeatitems: false,
//page: function() { return 1; },
root: "attribute"
}
});
jQuery("#"+subgrid_table_id).jqGrid('navGrid',{edit:false,add:false,del:false});
}
});
grid.jqGrid('navGrid','#gridpager2',{add:false,edit:false,del:false});
grid.jqGrid('navButtonAdd','#gridpager2',{
caption:"Export to Excel",
onClickButton : function () {
jQuery("#list2").excelExport();
}
});
为了将数据导入Excel,不必使用Excel格式导出文件。导出到CSV
通常要容易得多<默认情况下,code>CSV
文件应与Excel相关联,因此其旁边应有Excel图标以及所有内容<我认为,code>XML将以同样的方式工作,但是CSV
格式要轻得多,在这种情况下也能完成同样的工作。将JSON
转换为CSV
非常简单:
var response = JSON.parse(responseJSON).response;
var csv = arrayToCSV(response);
function arrayToCSV(arr) {
var columnNames = [];
var rows = [];
for (var i=0, len=arr.length; i<len; i++) {
// Each obj represents a row in the table
var obj = arr[i];
// row will collect data from obj
var row = [];
for (var key in obj) {
// Don't iterate through prototype stuff
if (!obj.hasOwnProperty(key)) continue;
// Collect the column names only once
if (i === 0) columnNames.push(prepareValueForCSV(key));
// Collect the data
row.push(prepareValueForCSV(obj[key]));
}
// Push each row to the main collection as csv string
rows.push(row.join(','));
}
// Put the columnNames at the beginning of all the rows
rows.unshift(columnNames.join(','));
// Return the csv string
return rows.join('\n');
}
// This function allows us to have commas, line breaks, and double
// quotes in our value without breaking CSV format.
function prepareValueForCSV(val) {
val = '' + val;
// Escape quotes to avoid ending the value prematurely.
val = val.replace(/"/g, '""');
return '"' + val + '"';
}
var response=JSON.parse(responseJSON.response);
var csv=arrayToCSV(响应);
函数arrayToCSV(arr){
var columnNames=[];
var行=[];
对于(var i=0,len=arr.length;i我正在使用MOSS 2007将一些列表(比如5个列表)导出到excel。我的要求是需要将多个列表导出到excel。我在页面中添加了一个带有按钮的CEWP,以便通过单击可以将多个列表数据导出到excel。当我使用jquery时,会出现运行时错误
如果($('#WebPartWPQ3')。是(':visible'))-->对象预期错误。
我找不到任何可追踪的部门id。所以有人知道答案吗,请出来。。
非常感谢与此相关的ans。我的代码如下。
<button type="button"
onclick=exportToExcel();>Click<br>
</button><br>
<script type="text/javascript"><br>
function exportToExcel() <br>
{ <br>
alert('Hi');<br>
//alert($("#WebPartWPQ3").attr("visibility"));<br>
if( $('#WebPartWPQ3').is(':visible') )<br>
{ <br>
contentType = "application/vnd.ms-excel";<br>
var oExcel = new ActiveXObject("Excel.Application");<br>
var oBook = oExcel.Workbooks.Add;<br>
var oSheet = oBook.Worksheets(1);<br>
var VESSApplications =document.getElementById<br>('ctl00_m_g_e3f5d791_5651_40ca_a03a_1c511c7f2b28_ctl00_ctl00_toolBarTbl');<br>
alert(document.getElementById('WebPartWPQ3'));<br>
var OtherApplications =document.getElementById('tblOtherApplications');<br>
// var MFGApplications =document.getElementById('tblMFGApplications');<br>
var row=3;<br>
var col=1;<br>
//Define criteria - start<br>
oSheet.Cells(row, col)="Business Function";<br>
oSheet.Cells(row, col+1)="VESS";<br>
oSheet.Cells(row, col+2)=selectedVESSBusinessFunction;<br>
// oSheet.Cells(row, col+3)="Manufacturing";<br>
// if(selectedMFGBusinessFunction != "-Select-")<br>
// oSheet.Cells(row, col+4)=selectedMFGBusinessFunction;<br>
row +=2;<br>
oSheet.Cells(row, col)="Operating System";<br>
oSheet.Cells(row, col+1)="First Choice";<br>
if(selectedOperatingSystemFirstChoice != "-Select-")<br>
oSheet.Cells(row, col+2)=selectedOperatingSystemFirstChoice;<br>
oSheet.Cells(row, col+3)="Second Choice";<br>
if(selectedOperatingSystemSecondChoice != "-Select-")<br>
oSheet.Cells(row, col+4)=selectedOperatingSystemSecondChoice;<br>
row +=2;<br>
oSheet.Cells(row, col)="Platform";<br>
oSheet.Cells(row, col+1)="First Choice";<br>
oSheet.Cells(row, col+2)=selectedPlatformFirstChoice;<br>
oSheet.Cells(row, col+3)="Second Choice";<br>
if(selectedPlatformSecondChoice != "-Select-")<br>
oSheet.Cells(row, col+4)=selectedPlatformSecondChoice;<br>
row +=2;<br>
oSheet.Cells(row, col)="Delivery Method";<br>
oSheet.Cells(row, col+1)="First Choice";<br>
oSheet.Cells(row, col+2)=selectedDeliveryFirstChoice;<br>
oSheet.Cells(row, col+3)="Second Choice";<br>
if(selectedDeliverySecondChoice != "-Select-")<br>
oSheet.Cells(row, col+4)=selectedDeliverySecondChoice;<br>
row +=2;<br>
//alert(VESSApplications.rows.length);<br>
if(VESSApplications.rows.length>0)<br>
{<br>
for (var y = 0; y < VESSApplications.rows.length; y++) <br>
{<br>
for (var x = 0; x < VESSApplications .rows(y).cells.length; x++) <br>
{<br>
// oSheet.Cells(y + 1, x + 1) = VESSApplications .rows(y).cells(x).innerText;<br>
oSheet.Cells(row, x + 1) = VESSApplications .rows(y).cells(x).innerText;<br>
}<br>
row++;<br>
}<br>
// oExcel.Visible = true;<br>
// oExcel.UserControl = true;<br>
}<br>
else<br>
{<br>
alert("There is no VESS/Other Applications to Export!");<br>
}<br>
row +=2;<br>
//Other Applications<br>
/* for (var y = 0; y < OtherApplications.rows.length; y++) <br>
{<br>
for (var x = 0; x < OtherApplications.rows(y).cells.length; x++) <br>
{<br>
oSheet.Cells(row, x + 1) = OtherApplications.rows(y).cells(x).innerText;<br>
}<br>
row++;<br>
}<br>
row +=2;<br>
//MFG Applications<br>
for (var y = 0; y < MFGApplications.rows.length; y++) <br>
{<br>
for (var x = 0; x < MFGApplications.rows(y).cells.length; x++) <br>
{<br>
// oSheet.Cells(y + 1, x + 1) = VESSApplications .rows(y).cells(x).innerText;<br>
oSheet.Cells(row, x + 1) = MFGApplications.rows(y).cells(x).innerText;<br>
}<br>
row++;<br>
}<br>
*/
oSheet.columns.autofit;<br>
oExcel.Visible = true;<br>
oExcel.UserControl = true;<br>
}<br>
else<br>
{<br>
alert('No VESS/Other applications available to export');<br>
}<br>
}<br>
单击
函数exportToExcel()
{
警报('Hi');
//警报($(“#Web部件WPQ3”).attr(“可见性”);
如果($('#WebPartWPQ3')。是(':visible'))
{
contentType=“应用程序/vnd.ms excel”;
var oExcel=新的ActiveXObject(“Excel.Application”);
var oBook=oExcel.Workbooks.Add;
var oSheet=oBook.工作表(1);
var Vesapplications=document.getElementById
('ctl00_m_g_e3f5d791_5651_40ca_a03a_1c511c7f2b28_ctl00_ctl00_工具栏TBL');
警报(document.getElementById('WebPartWPQ3');
var OtherApplications=document.getElementById('tblottherApplications');
//var mfgaapplications=document.getElementById('tblMFGApplications');
变量行=3;
var col=1;
//定义标准-开始
oSheet.Cells(行、列)=“业务功能”;
oSheet.单元格(行、列+1)=“VESS”;
oSheet.Cells(行、列+2)=SelectedVessBusiness功能;
//oSheet.单元格(行、列+3)=“制造业”;
//如果(selectedMFGBusinessFunction!=“选择-”)
//oSheet.Cells(行、列+4)=selectedMFGBusinessFunction;
行+=2;
oSheet.Cells(行、列)=“操作系统”;
oSheet.单元格(行、列+1)=“首选”;
如果(selectedOperatingSystemFirstChoice!=“选择-”)
oSheet.Cells(行、列+2)=selectedOperatingSystemFirstChoice;
oSheet.Cells(行、列+3)=“第二选择”;
如果(SelectedOperatingSystemsSecondChoice!=“选择-”)
oSheet.Cells(行、列+4)=SelectedOperatingSystemsSecondChoice;
行+=2;
oSheet.Cells(行、列)=“平台”;
oSheet.单元格(行、列+1)=“首选”;
oSheet.Cells(行、列+2)=selectedPlatformFirstChoice;
oSheet.Cells(行、列+3)=“第二选择”;
如果(selectedPlatformSecondChoice!=“选择-”)
oSheet.Cells(行、列+4)=selectedPlatformSecondChoice;
行+=2;
oSheet.单元格(行、列)=“交付方法”;
oSheet.单元格(行、列+1)=“首选”;
oSheet.Cells(行、列+2)=selectedDeliveryFirstChoice;
oSheet.Cells(行、列+3)=“第二选择”;
如果(selectedDeliverySecondChoice!=“选择-”)
oSheet.Cells(行、列+4)=selectedDeliverySecondChoice;
行+=2;
//警报(VESSApplications.rows.length);
如果(VESSApplications.rows.length>0)
{
对于(变量y=0;y
{
对于(var x=0;x
{
//oSheet.Cells(y+1,x+1)=VESSApplications.rows(y).Cells(x).innerText;
oSheet.Cells(row,x+1)=VESSApplications.rows(y).Cells(x).innerText;
}
行++;
}
//oExcel.Visible=true;
//oExcel.UserControl=true;
}
否则
{
警报(“没有要导出的VESS/其他应用程序!”;
}
行+=2;
//其他应用程序
/*对于(变量y=0;y
{
对于(var x=0;x
{
oSheet.Cells(行,x+1)=其他应用程序。行(y)。单元格(x)。innerText;
}
行++;
}
行+=2;
//制造应用程序
对于(变量y=0;y
{
对于(var x=0;x
{
//oSheet.Cells(y+1,x+1)=VESSApplications.rows(y).Cells(x).innerText;
oSheet.Cells(行,x+1)=MFGAapplications.rows(y).Cells(x).innerText;
}
行++;
}
*/
oSheet.columns.autofit;
oExcel.Visible=true;
oExcel.UserControl=true;
}
否则
{
警报(“没有可导出的VES/其他应用程序”);
}
}
我有一个jqGrid,在这里我可以立即从JSON格式的服务器(java)获取数据
格式。我希望将jqGrid中的数据导出到Excel中
格式
这是一篇很好的文章,向您展示了如何从jqGrid导出到Excel
我这样解决了这个问题: