How to export data to excel using react libraries

后端 未结 2 659
我在风中等你
我在风中等你 2021-01-29 08:20

I am using react-html-to-excel to convert my table to excel but what I want is to not to export the first column to excel i.e the first column should not exported t

2条回答
  •  悲&欢浪女
    2021-01-29 08:29

    You can create a simple hack to manipulate the table HTML code by overriding the ReactHTMLTableToExcel.format function which gets the table HTML to format the XLS document.

    ReactHTMLTableToExcel.format = (s, c) => {
      // If there is a table in the data object
      if (c && c['table']) {
        // Get the table HTML
        const html = c.table;
    
        // Create a DOMParser object
        const parser = new DOMParser();
    
        // Parse the table HTML and create a text/html document
        const doc = parser.parseFromString(html, 'text/html');
    
        // Get all table rows
        const rows = doc.querySelectorAll('tr');
    
        // For each table row remove the first child (th or td)
        for (const row of rows) row.removeChild(row.firstChild);
    
        // Save the manipulated HTML table code
        c.table = doc.querySelector('table').outerHTML;
      }
    
      return s.replace(/{(\w+)}/g, (m, p) => c[p]);
    };
    

    And you've got the table HTML filtered and with the first column removed.

    You can check this working in this Stackblitz project.

提交回复
热议问题