Wednesday, November 21, 2018

HTML table to convert to XLSX excel file javascript jquery ASP.net



HTML table to convert to XLSX excel file javascript jquery

file link: https://unpkg.com/xlsx@0.14.1/dist/xlsx.full.min.js
https://github.com/sheetjs/js-xlsx
https://freakyjolly.com/demo/jsonToXLS/
https://stackoverflow.com/questions/39475474/how-to-export-or-convert-json-to-excel-in-angularjs

JS file Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jsontohtml.aspx.cs" Inherits="jsontohtml" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="export/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="export/xlsx.full.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <script>

        function expt() {
  function tabletoexcel() {
            var tbl = document.getElementById('sheetjs');
            /* File Name */
            var filename = "FreakyJSON_To_XLS.xlsx";

            /* Sheet Name */
            var ws_name = "FreakySheet";

            if (typeof console !== 'undefined') console.log(new Date());
            var wb = XLSX.utils.book_new(),
                ws = XLSX.utils.table_to_sheet(tbl);

            /* Add worksheet to workbook */
            XLSX.utils.book_append_sheet(wb, ws, ws_name);

            /* Write workbook and Download */
            if (typeof console !== 'undefined') console.log(new Date());
            XLSX.writeFile(wb, filename);
            if (typeof console !== 'undefined') console.log(new Date());

        }

    </script>

        <button onclick="tabletoexcel();return false;">export to table</button><br />
        <table id="sheetjs">
            <tr><td colspan="2">Ashwini</td><td colspan="4">Sharma</td></tr>
<tr><td>Sai</td><td>Kumar</td><td>ved</td><td>kumar</td><td>t</td><td>J</td><td>S</td></tr>
<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
<tr><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td></tr>

</table>