如何在Javascript中从2D数组创建excel工作表

如何在Javascript中从2D数组创建excel工作表,javascript,Javascript,我想从二维数组创建excel工作表 我很抱歉要求从头开始编写代码 function downloadableCSV(rows) { var content = "data:text/csv;charset=utf-8,"; rows.forEach(function(row, index) { content += row.join(",") + "\n"; }); return encodeURI(content);

我想从二维数组创建excel工作表

我很抱歉要求从头开始编写代码

    function downloadableCSV(rows) {
      var content = "data:text/csv;charset=utf-8,";

      rows.forEach(function(row, index) {
        content += row.join(",") + "\n";
      });

      return encodeURI(content);
    }

    var rows = [
      ["name1", 2, 3],
      ["name2", 4, 5],
      ["name3", 6, 7],
      ["name4", 8, 9],
      ["name5", 10, 11]
    ];

    $(document).ready(function(){
        $("#download").click(function() {
          downloadableCSV(rows);
        });

    });
我从你那里得到这个密码。不管出于什么原因,代码似乎不起作用


任何帮助都将不胜感激。谢谢。

您不能手动创建Excel文件。但是您可以创建.csv文件,这些文件可以被Excel读取。看看帖子。

您可以使用ExcelPlus api。只需添加两行即可调用必要的文件:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/CHOOSE_A_VERSION/xlsx.core.min.js"></script>
<script type="text/javascript" src="excelplus-2.5.min.js"></script>

谢谢你的回答。我已经更新了我的问题,你能在那里纠正我吗。
// --- EXAMPLE 1 ---
// in this example we want to build an Excel file with one sheet and write some stuff
var ep=new ExcelPlus();
// We're going to do several tasks in one line of code:
// 1) create an Excel with one sheet called "Book1"
// 2) write some data from an array to the new-created sheet
// 3) create a new sheet called "Book2"
// 4) write "A1" in cell "A1" of the new-created sheet
// 5) write the today date in D1 of the "Book1" sheet
// 6) save it on the user computer (this last step only works with IE10+ and modern browsers)
ep.createFile("Book1")
  .write({ "content":[ ["A1","B1","C1"] ] })
  .createSheet("Book2")
  .write({ "cell":"A1", "content":"A1" })
  .write({ "sheet":"Book1", "cell":"D1", "content":new Date() })
  .saveAs("demo.xlsx");