export to excel in jquery or jqGrid

后端 未结 4 1437
别跟我提以往
别跟我提以往 2020-12-16 06:15

I have a jqGrid where I get data at once from server (java) in JSON format. I want the data in the jqGrid to be exported into Excel format.

Till now I saw this page

相关标签:
4条回答
  • 2020-12-16 06:28

    I'm working with MOSS 2007 to export some lists(say 5 lists) to excel.My requirement is i need more than one lists to be exported to excel.I have added a CEWP in my page with a button so that by one click i can export more than one list datas to excel.nw i get a run time error when i use jquery if( $('#WebPartWPQ3').is(':visible') )--->object expected error. I dont find any div id to trace..so is anybody know the answer,pls be come out of it.. ans related to this issue is really really really appreciated..my code is as follows.

    <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>
    
    0 讨论(0)
  • 2020-12-16 06:35

    You don't have to export a file using the Excel format in order to get the data into Excel. It is generally much easier to export to CSV. CSV files should be associated with Excel by default, so it should have the Excel icon by it and everything. XML would work the same way, I think, but the CSV format is much lighter, and does the same job in this case. Converting JSON to CSV is simple:

    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 + '"';
    }
    
    0 讨论(0)
  • 2020-12-16 06:37

    I have a jqGrid where I get data at once from server (java) in JSON format. I want the data in the jqGrid to be exported into Excel format.

    Here's a nice article, showing you how to export from jqGrid to Excel...

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

    0 讨论(0)
  • 2020-12-16 06:41

    I solved this like this:

    1. Read

    https://w3lessons.info/2015/07/13/export-html-table-to-excel-csv-json-pdf-png-using-jquery/#Installation

    1. Here is github

    https://github.com/kayalshri/tableExport.jquery.plugin

    1. Here is a demo

    http://demos.w3lessons.info/jquery-table-export#

    It works perfectly i tried the Export Excel. In my case i just used the id selector for the jqgrid table.

    BTW This will export only visible part so if you 100 pages it need server side because info is just not there !.

    0 讨论(0)
提交回复
热议问题