我想从二维数组创建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);
});
});
我从others fiddle获得了此代码。出于任何原因,代码似乎无法正常工作。
任何帮助将不胜感激。感谢。
答案 0 :(得分:0)
您无法手动创建Excel文件。但是您可以创建可以由Excel读取的.csv文件。看看this帖子。
答案 1 :(得分:0)
您可以使用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>
关于如何编写/创建Excel文件的示例:
// --- 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");