0

доброго времени суток, могу поинтересоваться кто пользовался библиотекой exceljs ??? нужен экспорт нескольких таблиц в эксельный документ формата xlsx, одну таблицу получилось вывести, а вот несколько... исшарил все что можно, но что-то толковой инфы не нашел( Передаю одну таблицу таким способом

var arr_head = [];
var get_head = $('#table thead tr').each(function (index, element) {    
var elements = $(element).find("td");
$(elements).each(function(index_head, element_head) {
arr_head.push({ header: $(element_head).text(), width: 15});
    });

});

worksheet.columns = arr_head;

может есть другие библиотеки которые смогут это сделать ??

1 ответ 1

0

Те кто вдруг случайно столкнется с exceljs вот скрипка которая эскпортирует несколько таблиц в эксель методом blob ну там само определитесь как вам будет удобно)) недеюсь кому-то помог) HTML:

<div class="container">
    <table id="table" class='table_for_export_excel'>
      <thead id="thead">
        <tr>
          <th>number</th>
          <th>head</th>
          <th>value</th>
          <th>test</th>
          <th>ging</th>
        </tr>

      </thead>

      <tbody id="tbody">
        <tr>
          <td>body</td>
          <td>body</td>
          <td>body</td>
          <td>body</td>
          <td>body</td>

        </tr>

      </tbody>

      <tfoot id="tfoot">
        <tr>

            <td>foot</td>
            <td>foot</td>
            <td>foot</td>
            <td>foot</td>
            <td>foot</td>


        </tr>
      </tfoot>
    </table>


    <table id="table" class='table_for_export_excel'>
      <thead id="thead">
        <tr>
          <th>head2</th>
          <th>head2</th>
          <th>head2</th>
          <th>head2</th>
          <th>head2</th>
        </tr>

      </thead>

      <tbody id="tbody">
        <tr>

          <td>body2</td>
          <td>body2</td>
          <td>body2</td>
          <td>body2</td>
          <td>body2</td>

        </tr>


      </tbody>

      <tfoot id="tfoot">
        <tr>

            <td>foot2</td>
            <td>foot2</td>
            <td>foot2</td>
            <td>foot2</td>
            <td>foot2</td>
        </tr>
      </tfoot>
    </table>
</div>

JS:

var workbook = new ExcelJS.Workbook();
var worksheet = workbook.addWorksheet();    
var getting_table = function(){
    var post_class = $('.table_for_export_excel');
    post_class.each(function(index, element){

        var thead = [];
        //loop for current table 
        $(element).each(function (index_head, element_head){
            var elements_td = $(element_head).find("th");

            $(elements_td).each(function(index_th, element_th){
                // debugger;
                thead.push($(element_th).text());
                // console.log(element_th);

            });
            $(thead).each(function(index_val, el_body){

                    thead.push($(el_body).text());
                    console.log(el_body);

            });
            worksheet.addRow(thead);
            worksheet.addRow("  ");
        });

        var body = [];

        //loop for current table body
        $(element).each(function (index_head, element_head){

            var elements_td = $(element_head).find("#tbody tr td");

            $(elements_td).each(function(index_td, element_td){
                // debugger;
                body.push($(element_td).text());

            });
            $(body).each(function(index_val, el_body){


                    thead.push($(el_body).text());
                    console.log(el_body);

            });
            worksheet.addRow(body);
            worksheet.addRow("  ");
        });

        //loop for current table footer
        $(element).each(function (index_head, element_head){
        var footer = [];

            var elements_td = $(element_head).find("#tfoot tr td");

            $(elements_td).each(function(index_th, element_th){
                // debugger;
                footer.push($(element_th).text());

            });
            $(footer).each(function(index_val, el_footer){

                    footer.push($(el_footer).text());

            });
            worksheet.addRow(footer);
            worksheet.addRow(" ");

        });
    // debugger;

    });
};

getting_table();


workbook.xlsx.writeBuffer().then(function (data) {
    var blob = new Blob([data], {
        type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    });

    saveAs(blob, "export_excel.xlsx");

});

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.