How to get the Excel attached to Email?

How to get the Excel attached to Email?

zbjs4mozbjs4mo Posts: 2Questions: 1Answers: 0

I want to send email with attached Excel created from Datatables buttons.
I created controller to send email.
and created datatables to export excel
I do not know how to get the excel in ajax to send to controller.
this is my code but it is not working

$('#example-table').DataTable({
dom: 'Blfrtip',

        buttons: [
            {
                extend: 'excelHtml5',
                title: 'Data',
                text: 'Export To Excel',
                exportOptions: {
                    format: {
                        body: function (inner, rowidx, colidx, node) {
                            if ($(node).children("input").length > 0) {
                                return $(node).children("input").first().val();
                            } else {
                                return inner;
                            }
                        }
                    }


                },

                action: function (e, dt, node, config) {

                   //var files = $.fn.dataTable.ext.buttons.excelHtml5.action.call(this, e, dt, node, config);

                    var table = $('#example-table').DataTable();

                    var data = table.buttons.exportData();
                    var files = $("#fileUploader").get(0).files[0];
                    var url = "/test/SendEmail";
                    formData = new FormData();
                    formData.append("fileUploader", files);

                    jQuery.ajax({
                        type: 'POST',
                        url: url,
                        data: formData,
                        dataType: 'xlsx',
                        cache: false,
                        contentType: false,
                        processData: false,

                    });


                }
            },              

        ]

Answers

  • rf1234rf1234 Posts: 2,955Questions: 87Answers: 416

    You should create the Excel on the server would be me advice. This would also allow you to customize it a lot easier using tools like PHP Spreadsheet for example.

  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin

    Just to add to that, this is the piece of code that finalizes the Excel export. As you'll be able to see there, there is no option to get the generated file as a blob.

    You could if you wanted add that ability there, and then post the generated file to the server to have it e-mail the attachment out. But as usual I'd totally agree with @rf1234 and say it might be easier to just do this all server-side. Use the buttons.exportData() method to get the data to export and send that to the server.

    Allan

  • zbjs4mozbjs4mo Posts: 2Questions: 1Answers: 0

    I changed my code and now has a function in controller that create excel and attach it to email and send. here is what controller should get
    public IActionResult SendEmail(System.Data.DataTable dt)

    my problem now is just to get the data from datatable buttons and send to this controller here is my code:

        $('#example-table').DataTable({
            dom: 'Blfrtip',
    
            buttons: [
                {
                    extend: 'excelHtml5',
                    title: 'Data',
                    text: 'Export To Excel',
                    exportOptions: {
                        format: {
                            body: function (inner, rowidx, colidx, node) {
                                if ($(node).children("input").length > 0) {
                                    return $(node).children("input").first().val();
                                } else {
                                    return inner;
                                }
                            }
                        }
    
    
                    },
                    action: function (e, dt, node, config) {
                        //var myList = dt.buttons.exportData(config.exportOptions);
                        var myList = $('#example-table').DataTable();
    
                        jQuery.ajax({
                            type: 'POST',
                            url: "/R/SendEmail",
                            data: dt,
                            contenttype: "application/json; charset=utf-8",
                            datatype: "json",
                            cache: false,
                            contentType: false,
                            processData: false
    
                        });
    
                    }
    
                }
    
            ]
    
  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin
    var myList = dt.buttons.exportData(config.exportOptions);
    

    looks correct to me. Then sent that as the data.

    Allan

Sign In or Register to comment.