导出到Excel中的jQuery或jqGrid

我有一个jqGrid ,我从JSON格式的服务器(Java)一次获取数据。 我想要在jqGrid中的数据导出为Excel格式。

直到现在我看到这个页面给我一个错误在IE中'o.url is null or not an object' grid.import.js

另外,我看到这个演示 ,在导出button的工具提示上说, Export To Excel但保存的文件是XML格式。

所以我想任何build议,可以将我的JSONstring转换成使用JavaScript或jQuery插件的Excel或使用jqgrid的内置function。

我的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(); } }); 

我的Json的一部分

 { "response": [ { "id": "1", "elementName": "libgtop2-devel-2.14.4-3.el5", "subCategory": "patch", "isEqual": false, "isPrasentinXml1": false, "isPrasentinXml2": false, "isPrasentinXml3": true, "attribute": [ { "name": "name", "thirdValue": "libgtop2-devel-2.14.4-3.el5" } ] }, { "id": "2", "elementName": "ifd-egate-0.05-15", "subCategory": "patch", "isEqual": false, "isPrasentinXml1": false, "isPrasentinXml2": false, "isPrasentinXml3": true, "attribute": [ { "name": "name", "thirdValue": "ifd-egate-0.05-15" } ] }, { "id": "3", "elementName": "libXScrnSaver-devel-1.1.0-3.1", "subCategory": "patch", "isEqual": false, "isPrasentinXml1": false, "isPrasentinXml2": false, "isPrasentinXml3": true, "attribute": [ { "name": "name", "thirdValue": "libXScrnSaver-devel-1.1.0-3.1" } ] }, { "id": "4", "elementName": "kde-i18n-Chinese-Big5-3.5.4-1", "subCategory": "patch", "isEqual": false, "isPrasentinXml1": true, "isPrasentinXml2": true, "isPrasentinXml3": true, "attribute": [ { "name": "name", "firstValue": "kde-i18n-Chinese-Big5-3.5.4-1", "secondValue": "kde-i18n-Chinese-Big5-3.5.4-1" } ] }, { "id": "5", "elementName": "cpio-2.6-20", "subCategory": "patch", "isEqual": false, "isPrasentinXml1": true, "isPrasentinXml2": true, "isPrasentinXml3": true, "attribute": [ { "name": "name", "firstValue": "cpio-2.6-20", "secondValue": "cpio-2.6-20", "thirdValue": "cpio-2.6-20" } ] }, { "id": "6", "elementName": "grep-2.5.1-54.2.el5", "subCategory": "patch", "isEqual": false, "isPrasentinXml1": true, "isPrasentinXml2": true, "isPrasentinXml3": true, "attribute": [ { "name": "name", "firstValue": "grep-2.5.1-54.2.el5", "secondValue": "grep-2.5.1-54.2.el5", "thirdValue": "grep-2.5.1-54.2.el5" } ] }, { "id": "7", "elementName": "avahi-compat-libdns_sd-0.6.16-1.el5", "subCategory": "patch", "isEqual": false, "isPrasentinXml1": true, "isPrasentinXml2": true, "isPrasentinXml3": true, "attribute": [ { "name": "name", "firstValue": "avahi-compat-libdns_sd-0.6.16-1.el5", "secondValue": "avahi-compat-libdns_sd-0.6.16-1.el5", "thirdValue": "avahi-compat-libdns_sd-0.6.16-1.el5" } ] }, { "id": "8", "elementName": "gpm-devel-1.20.1-74.1", "subCategory": "patch", "isEqual": false, "isPrasentinXml1": true, "isPrasentinXml2": true, "isPrasentinXml3": true, "attribute": [ { "name": "name", "firstValue": "gpm-devel-1.20.1-74.1", "secondValue": "gpm-devel-1.20.1-74.1", "thirdValue": "gpm-devel-1.20.1-74.1" } ] }, { "id": "9", "elementName": "esc-1.0.0-39.el5", "subCategory": "patch", "isEqual": false, "isPrasentinXml1": false, "isPrasentinXml2": false, "isPrasentinXml3": true, "attribute": [ { "name": "name", "thirdValue": "esc-1.0.0-39.el5" } ] }, { "id": "10", "elementName": "kde-i18n-Spanish-3.5.4-1", "subCategory": "patch", "isEqual": false, "isPrasentinXml1": true, "isPrasentinXml2": true, "isPrasentinXml3": true, "attribute": [ { "name": "name", "firstValue": "kde-i18n-Spanish-3.5.4-1", "secondValue": "kde-i18n-Spanish-3.5.4-1" } ] } ] } 

您不必使用Excel格式导出文件,以便将数据导入到Excel中。 导出为CSV通常要容易得多。 CSV文件应该默认与Excel关联,所以它应该有它的Excel图标和一切。 我认为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 + '"'; } 

我有一个jqGrid,我从JSON格式的服务器(Java)一次获取数据。 我想要在jqGrid中的数据导出为Excel格式。

这里有一篇不错的文章,告诉你如何从jqGrid导出到Excel …

http://www.codeproject.com/Articles/784342/Export-data-from-jqGrid-into-a-real-Excel-file

我正在与MOSS 2007导出一些列表(比如说5列表)以excel.My的要求是我需要多个列表导出到excel.I已经添加了一个CEWP在我的网页上的一个button,只需点击一下我可以导出多个列表数据到excel.nw我得到一个运行时错误,当我使用jquery if($('#WebPartWPQ3')。is(':visible'))—> object expected error。 我没有find任何div id追踪..所以任何人都知道答案,请出来吧..与这个问题有关真的真的真的很感激..我的代码如下。

  <button type =“button”
的onclick = exportToExcel();>点击,点击
 </button>结果
 <script type =“text / javascript”> <br>
 函数exportToExcel()<br>
 {...
警报(“你好”);结果
 //alert($("#WebPartWPQ3").attr("visibility“));点击
 if($('#WebPartWPQ3')。(':visible'))<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('ctl00_m_g_e3f5d791_5651_40ca_a03a_1c511c7f2b28_ctl00_ctl00_toolBarTbl'); <br>
  警报(的document.getElementById( 'WebPartWPQ3'));点击
 var OtherApplications = document.getElementById('tblOtherApplications'); <br>
  // var MFGApplications = document.getElementById('tblMFGApplications'); <br>
 var row = 3; <br>
 var col = 1; <br>

 / /定义条件 - 开始<br>
 oSheet.Cells(row,col)=“业务function”; <br>
 oSheet.Cells(row,col + 1)=“VESS”; <br>
 oSheet.Cells(row,col + 2)= selectedVESSBusinessFunction; <br>
  // oSheet.Cells(row,col + 3)=“制造”; <br>
  // if(selectedMFGBusinessFunction!=“-Select-”)<br>
  // oSheet.Cells(row,col + 4)= selectedMFGBusinessFunction; <br>
行+ = 2; <br>

 oSheet.Cells(row,col)=“操作系统”; <br>
 oSheet.Cells(row,col + 1)=“首选”; <br>
    如果(selectedOperatingSystemFirstChoice!=“ - select”)<br>

  oSheet.Cells(row,col + 2)= selectedOperatingSystemFirstChoice; <br>


 oSheet.Cells(row,col + 3)=“第二select”; <br>

如果(selectedOperatingSystemSecondChoice!=“ - select - ”)<br>

 oSheet.Cells(row,col + 4)= selectedOperatingSystemSecondChoice; <br>

行+ = 2; <br>
 oSheet.Cells(row,col)=“平台”; <br>
 oSheet.Cells(row,col + 1)=“首选”; <br>
 oSheet.Cells(row,col + 2)= selectedPlatformFirstChoice; <br>
 oSheet.Cells(row,col + 3)=“第二select”; <br>

 if(selectedPlatformSecondChoice!=“ - select - ”)<br>

 oSheet.Cells(row,col + 4)= selectedPlatformSecondChoice; <br>
行+ = 2; <br>
 oSheet.Cells(row,col)=“递送方法”; <br>
 oSheet.Cells(row,col + 1)=“首选”; <br>
 oSheet.Cells(row,col + 2)= selectedDeliveryFirstChoice; <br>
 oSheet.Cells(row,col + 3)=“第二select”; <br>

 如果(selectedDeliverySecondChoice!=“ - select - ”)<br>

 oSheet.Cells(row,col + 4)= selectedDeliverySecondChoice; <br>
行+ = 2; <br>
 //alert(VESSApplications.rows.length);点击
如果(VESSApplications.rows.length> 0),点击
 {点击
  for(var y = 0; y <VESSApplications.rows.length; y ++)
  {点击
      for(var x = 0; x <VESSApplications .rows(y).cells.length; x ++)
      {点击
    // 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>

      }结果
             行++;结果

  }结果
  // oExcel.Visible = true; <br>
   // oExcel.UserControl = true; <br>
 }结果

否则结果
 {点击
警报(“没有VESS /其他应用程序导出!”); <br>
 }结果
行+ = 2; <br>

 //其他应用程序<br>

  / * for(var y = 0; y <OtherApplications.rows.length; y ++)
  {点击
      for(var x = 0; x <OtherApplications.rows(y).cells.length; x ++)
      {点击
          oSheet.Cells(row,x + 1)= OtherApplications.rows(y).cells(x).innerText; <br>

      }结果
             行++;结果

  }结果


   行+ = 2; <br>


 // MFG应用程序

  for(var y = 0; y <MFGApplications.rows.length; y ++)
  {点击
      for(var x = 0; x <MFGApplications.rows(y).cells.length; x ++)
      {点击
    // 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>

      }结果
             行++;结果

  }结果

  * /  
 oSheet.columns.autofit;结果


  oExcel.Visible = true; <br>
  oExcel.UserControl = true; <br>
 }结果
否则结果
 {点击
 警报('没有VESS /其他应用程序可用于导出'); <br>
 }结果
 }结果