custom button for excel export

custom button for excel export

dascocodascoco Posts: 9Questions: 4Answers: 0

I'm using the solution from @you2525 for excel export with multiple sheets
Is there also away to define a Custom button which exports a specific table to first sheet and other ones in following sheets and ad this button to each table?

This question has an accepted answers - jump to answer

Answers

  • you2525you2525 Posts: 23Questions: 5Answers: 0

    I don't know if it answers to your question but what i did is initialize all the tables that are needed in the excel export in the html file and then hide the ones that i don't need to be seen on the website in the js file with initComplete: function() { $('table.dataTable').hide(); }

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    This example shows how to add a custom button:
    https://datatables.net/extensions/buttons/examples/initialisation/custom.html

    You can add the custom code to export the tables in the order you wish.

    Kevin

  • dascocodascoco Posts: 9Questions: 4Answers: 0

    Thank you for the answer.

    I generally know how to add a custom button.
    My problem is how to start the excel export for a specific table.

    Taking the example from @you2525 the export is initialized for table example and the sheet for table example2 is added.

      var table = $('#example').DataTable({
        dom: 'Bftrip',
        buttons: [
          {
            extend: 'excelHtml5',
            text: 'Excel',
            customize: function( xlsx ) {
              setSheetName(xlsx, 'Calls');
              addSheet(xlsx, '#example2', 'My Sheet2', 'Summary2', '2');
            }      
          }
        ]
      });
    

    If I would know add this code for export button to example2 then the export would start with example2 in first sheet and add example in second sheet.
    Therefore I want to create a custom button which starts export with example and then adds a new sheet with example2.And attach this custom button to both tables.

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    Answer ✓

    The problem is that Datatables automatically creates the first sheet from the table the Excel button is configured for and working with OpenXML is complicated. Probably the easiest way is to overwrite sheet1 (default sheet) with the table you want. I created a new function called replaceSheet1() and use the code that creates the new sheet then it sets the name of the sheet. Here is the updated example:
    http://live.datatables.net/zezodeyo/1/edit

    Kevin

  • dascocodascoco Posts: 9Questions: 4Answers: 0

    Thank you this solved the problem

This discussion has been closed.