js实现导出excel包含多个sheet(实现导出多个表格)

实现效果
例如页面中存在两个表格,需要将它们都导出到excel中
js实现导出excel包含多个sheet(实现导出多个表格)_第1张图片
js实现导出excel包含多个sheet(实现导出多个表格)_第2张图片
js实现导出excel包含多个sheet(实现导出多个表格)_第3张图片

<table id='table1'>
	<tr>
		<td>员工编号td>
		<td>实发工资td>
		<td>员工姓名td>
		<td>应发工资td>
		<td>应扣工资td>
		<td>单位td>
		<td>民族td>
		<td>固定工资td>
		<td>政治面貌td>
		<td>绩效工资td>
	<tr>
	<tr>
		<td>JGH20031095td>
		<td>1020.00td>
		<td>11td>
		<td>1200.00td>
		<td>200td>
		<td>JJJtd>
		<td>汉族td>
		<td>10000.00td>
		<td>中国共产党党员td>
		<td>0.00td>
	<tr>
table>

<table id='table2'>
	<tr>
		<td>绩效工资td>
		<td>身份证件号td>
		<td>籍贯td>
		<td>职务td>
		<td>岗位性质td>
		<td>婚宴状况td>
		<td>岗位td>
		<td>岗位类别td>
		<td>性别td>
		<td>出生日期td>
	<tr>
	<tr>
		<td>0td>
		<td>0td>
		<td>湖南浏阳td>
		<td>副院长td>
		<td>在编td>
		<td>未婚td>
		<td>专任教师td>
		<td>专业技术岗td>
		<td>td>
		<td>1963-05-13td>
	<tr>
table>

js部分

function export(){
	//示例  有多少个table就生成多少个sheet 数组相同
	var tables = ['table1','table2'];
	var sheets = ['sheet0','sheet1'];
	var fileName = '测试导出文件.xls';//文件名
	tablesToExcel(tables,sheets,fileName,'Excel');
}

//导出excel包含多个sheet
//tables:tableId的数组;wsbames:sheet的名字数组;wbname:工作簿名字;appname:Excel
function tablesToExcel(tables, wsnames, wbname, appname) {

    var uri = 'data:application/vnd.ms-excel;base64,'
        ,
        tmplWorkbookXML = ''
            + 'Axel Richter{created}'
            + ''
            + ''
            + ''
            + ''
            + '{worksheets}'
        , tmplWorksheetXML = '{rows}
'
, tmplCellXML = '{data}' , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) } , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) } var ctx = ""; var workbookXML = ""; var worksheetsXML = ""; var rowsXML = ""; for (var i = 0; i < tables.length; i++) { if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]); //控制要导出的行数 for (var j = 0; j < tables[i].rows.length; j++) { rowsXML += ''; for (var k = 0; k < tables[i].rows[j].cells.length; k++) { var dataType = tables[i].rows[j].cells[k].getAttribute("data-type"); var dataStyle = tables[i].rows[j].cells[k].getAttribute("data-style"); var dataValue = tables[i].rows[j].cells[k].getAttribute("data-value"); dataValue = (dataValue) ? dataValue : tables[i].rows[j].cells[k].innerHTML; var dataFormula = tables[i].rows[j].cells[k].getAttribute("data-formula"); dataFormula = (dataFormula) ? dataFormula : (appname == 'Calc' && dataType == 'DateTime') ? dataValue : null; ctx = { attributeStyleID: (dataStyle == 'Currency' || dataStyle == 'Date') ? ' ss:StyleID="' + dataStyle + '"' : '' , nameType: (dataType == 'Number' || dataType == 'DateTime' || dataType == 'Boolean' || dataType == 'Error') ? dataType : 'String' , data: (dataFormula) ? '' : dataValue , attributeFormula: (dataFormula) ? ' ss:Formula="' + dataFormula + '"' : '' }; rowsXML += format(tmplCellXML, ctx); } rowsXML += '' } ctx = {rows: rowsXML, nameWS: wsnames[i] || 'Sheet' + i}; worksheetsXML += format(tmplWorksheetXML, ctx); rowsXML = ""; } ctx = {created: (new Date()).getTime(), worksheets: worksheetsXML}; workbookXML = format(tmplWorkbookXML, ctx); // 查看后台的打印输出 // console.log(workbookXML); var link = document.createElement("A"); link.href = uri + base64(workbookXML); link.download = wbname || 'Workbook.xls'; link.target = '_blank'; document.body.appendChild(link); link.click(); document.body.removeChild(link); }

你可能感兴趣的:(前端技术,js,javascript,前端)