我如何使用 reactjs / javascript 在单个 excel sheet 中导出 2 个表
How can i export 2 tables in a single excel sheet using reactjs / javascript
我正在尝试使用 reactjs 在单个 excel sheet 中导出 2 个表,任何人都可以建议我任何库或任何想法如何实现这个
function toExcel() {
if ("ActiveXObject" in window) {
alert("This is Internet Explorer!");
} else {
var cache = {};
this.tmpl = function tmpl(str, data) {
var fn = !/\W/.test(str) ? cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) :
new Function("obj",
"var p=[],print=function(){p.push.apply(p,arguments);};" +
"with(obj){p.push('" +
str.replace(/[\r\t\n]/g, " ")
.split("{{").join("\t")
.replace(/((^|}})[^\t]*)'/g, "\r")
.replace(/\t=(.*?)}}/g, "',,'")
.split("\t").join("');")
.split("}}").join("p.push('")
.split("\r").join("\'") + "');}return p.join('');");
return data ? fn(data) : fn;
};
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{{=worksheet}}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>{{for(var i=0; i<tables.length;i++){ }}<table>{{=tables[i]}}</table>{{ } }}</body></html>',
base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)));
},
format = function (s, c) {
return s.replace(/{(\w+)}/g, function (m, p) {
return c[p];
});
};
return function (tableList, name) {
if (!tableList.length > 0 && !tableList[0].nodeType) table = document.getElementById(table);
var tables = [];
for (var i = 0; i < tableList.length; i++) {
tables.push(tableList[i].innerHTML);
}
var ctx = {
worksheet: name || 'Worksheet',
tables: tables
};
window.location.href = uri + base64(tmpl(template, ctx));
};
})();
tableToExcel(document.getElementsByClassName("exportToExcel"), "one");
}
}
您可以使用 react-html-table-to-excel
使用 id 导出文件,然后在 ReactToExcel 上实现相同的 id
npm i react-html-table-to-excel
或者如果你使用纱线 yarn add react-html-table-to-excel
<table id="table-to-xls" >
.....
</table>
<ReactToExcel
className="btn "
table="table-to-xls"
filename="Name of file"
sheet="sheet 1"
buttonText={t("export_report")}
/>
我正在尝试使用 reactjs 在单个 excel sheet 中导出 2 个表,任何人都可以建议我任何库或任何想法如何实现这个
function toExcel() {
if ("ActiveXObject" in window) {
alert("This is Internet Explorer!");
} else {
var cache = {};
this.tmpl = function tmpl(str, data) {
var fn = !/\W/.test(str) ? cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) :
new Function("obj",
"var p=[],print=function(){p.push.apply(p,arguments);};" +
"with(obj){p.push('" +
str.replace(/[\r\t\n]/g, " ")
.split("{{").join("\t")
.replace(/((^|}})[^\t]*)'/g, "\r")
.replace(/\t=(.*?)}}/g, "',,'")
.split("\t").join("');")
.split("}}").join("p.push('")
.split("\r").join("\'") + "');}return p.join('');");
return data ? fn(data) : fn;
};
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{{=worksheet}}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>{{for(var i=0; i<tables.length;i++){ }}<table>{{=tables[i]}}</table>{{ } }}</body></html>',
base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)));
},
format = function (s, c) {
return s.replace(/{(\w+)}/g, function (m, p) {
return c[p];
});
};
return function (tableList, name) {
if (!tableList.length > 0 && !tableList[0].nodeType) table = document.getElementById(table);
var tables = [];
for (var i = 0; i < tableList.length; i++) {
tables.push(tableList[i].innerHTML);
}
var ctx = {
worksheet: name || 'Worksheet',
tables: tables
};
window.location.href = uri + base64(tmpl(template, ctx));
};
})();
tableToExcel(document.getElementsByClassName("exportToExcel"), "one");
}
}
您可以使用 react-html-table-to-excel
使用 id 导出文件,然后在 ReactToExcel 上实现相同的 id
npm i react-html-table-to-excel
或者如果你使用纱线 yarn add react-html-table-to-excel
<table id="table-to-xls" >
.....
</table>
<ReactToExcel
className="btn "
table="table-to-xls"
filename="Name of file"
sheet="sheet 1"
buttonText={t("export_report")}
/>