function PageToExcel(TableID,FirstRow,LastRowColor,SaveAsName){
this.lastRowColor=LastRowColor==""?0:LastRowColor;
var today=new Date();
this.saveAsName=(SaveAsName==""?today.getYear()+""+(today.getMonth()+1) + today.getDate() + today.getHours() + today.getMinutes() + today.getSeconds() + ".xls":SaveAsName);
this.tableId=TableID;
this.table=document.getElementById(this.tableId);//导出的table 对象
this.rows=this.table.rows.length;//导出的table总行数
this.colSumCols=this.table.rows(0).cells.length;//第一行总列数
this.fromrow=FirstRow;
this.beginCol=0; //起始列数
this.cols=this.colSumCols;
this.oXL=null;
this.oWB=null;
this.oSheet=null;
this.rowSpans=1; //行合并
this.colSpans=1; //列合并
this.colsName={0:"A",1:"B", 2:"C", 3:"D", 4:"E", 5:"F", 6:"G", 7:"H", 8:"I",9:"J", 10:"K", 11:"L", 12:"M", 13:"N", 14:"O", 15:"P", 16:"Q", 16:"R" ,18:"S", 19:"T", 20:"U", 21:"V", 22:"W", 23:"X", 24:"Y", 25:"Z"};
}
PageToExcel.prototype.DeleteExcelCols=function(NotShowColList){//数组NotShowColList
//this.notShowColList=NotShowColList;//不显示列集合,1,2,3,1
//删除excel中的列
var m=0;
for(var i=0;i<NotShowColList.length;i++){
if(i>0){
m++;
}
var temp=NotShowColList[i]- m;
var index=this.colsName[temp];
this.oSheet.Columns(index).Delete;//删除
}
m=0;
}
PageToExcel.prototype.CreateExcel=function(ExcelVisible){
try{
this.oXL = new ActiveXObject("Excel.Application"); //创建应该对象
this.oXL.Visible = ExcelVisible;
this.oWB = this.oXL.Workbooks.Add();//新建一个Excel工作簿
this.oSheet = this.oWB.ActiveSheet;//指定要写入内容的工作表为活动工作表
//不显示网格线
//this.oXL.ActiveWindow.DisplayGridlines=false;
}catch(e){
alert("请确认安装了非绿色版本的excel!"+e.description);
CloseExcel();
}
}
//关闭excel
PageToExcel.prototype.CloseExcel=function(){
this.oXL.DisplayAlerts = false;
this.oXL.Quit();
this.oXL = null;
this.oWB=null;
this.oSheet=null;
}
PageToExcel.prototype.ChangeElementToLabel=function (ElementObj){
var GetText="";
try{
var childres=ElementObj.childNodes;
}catch(e){ return GetText}
if(childres.length<=0) return GetText;
for(var i=0;i<childres.length;i++){
try{if(childres[i].style.display=="none"||childres[i].type.toLowerCase()=="hidden"){continue;}}
catch(e){}
try{
switch (childres[i].nodeName.toLowerCase()){
case "#text" :
GetText +=childres[i].nodeValue ;
break;
case "br" :
GetText +="\n";
break;
case "img" :
GetText +="";
break;
case "select" :
GetText +=childres[i].options[childres[i].selectedIndex].innerText ;
break;
case "input" :
if(childres[i].type.toLowerCase()=="submit"||childres[i].type.toLowerCase()=="button"){
GetText +="";
}else if(childres[i].type.toLowerCase()=="textarea"){
GetText +=childres[i].innerText;
}else{
GetText +=childres[i].value;
}
break;
default :
GetText += this.ChangeElementToLabel(childres[i]);
break;
}
}catch(e){}
}
return GetText;
}
PageToExcel.prototype.SaveAs=function (){
//保存
try{
this.oXL.Visible =true;
var fname = this.oXL.Application.GetSaveAsFilename(this.saveAsName, "Excel Spreadsheets (*.xls), *.xls");
if(fname){
this.oWB.SaveAs(fname);
this.oXL.Visible =false;
}
}catch(e){};
}
PageToExcel.prototype.Exec=function()
{
//寻找列数,考虑到第一行可能存在
for (var i=0; i<this.colSumCols;i++) {
var tmpcolspan = this.table.rows(0).cells(i).colSpan;
if ( tmpcolspan>1 ) {
this.cols += tmpcolspan-1;
}
}
//定义2维容器数据,1:行;2:列;值(0 可以填充,1 已被填充)
var container=new Array(this.rows);
for (var i=0;i<this.rows;i++) {
container[i]=new Array(this.cols);
for (j=0;j<this.cols;j++) {
container[i][j]=0;
}
}
//将所有单元置为文本,避免非数字列被自动变成科学计数法和丢失前缀的0
this.oSheet.Range(this.oSheet.Cells(this.fromrow+1,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).NumberFormat = "@";
// 循环行
for (i=0;i<this.rows;i++){
//循环列
for (j=0;j<this.cols;j++){
//寻找开始列
for (k=j;k<this.cols;k++){
if (container[i][k]==0) {
this.beginCol=k;
k=this.cols; //退出循环
}
}
//赋值
//此处相应跟改 根据 标签的类型,替换相关参数
this.oSheet.Cells(i+1+this.fromrow,this.beginCol+1).value = this.ChangeElementToLabel(this.table.rows(i).cells(j));
//计算合并列
try{
this.colSpans = this.table.rows(i).cells(j).colSpan;
}catch(e){
this.colSpans=0
}
if (this.colSpans>1) {
//合并
this.oSheet.Range(this.oSheet.Cells(i+1+this.fromrow,this.beginCol+1),this.oSheet.Cells(i+1+this.fromrow,this.beginCol+this.colSpans)).Merge();
}
//将当前table位置填写到对应的容器中
for (k=0; k<this.colSpans;k++) {
container[i][this.beginCol+k]= 1;
}
// 计算合并行
try{
this.rowSpans = this.table.rows(i).cells(j).rowSpan;
}catch(e){
this.rowSpans = 0;
}
if (this.rowSpans>1) { //行合并
this.oSheet.Range(this.oSheet.Cells(i+1+this.fromrow,this.beginCol+1),this.oSheet.Cells(i+this.rowSpans+this.fromrow,this.beginCol+this.colSpans)).Merge();
//将当前table位置填写到对应的容器中
for (k=1; k<this.rowSpans;k++) { //由于第0行已经被colSpans对应的代码填充了,故这里从第1行开始
for (l=0;l<this.colSpans;l++) {
container[i+k][this.beginCol+l]=1;
}
}
}
//如果开始列+合并列已经等于列数了,故不需要再循环html table
if (this.beginCol+this.colSpans>=this.cols) j=this.cols;
}
if(i==0)
{
//标题栏
this.oSheet.Range(this.oSheet.Cells(1,1), this.oSheet.Cells(1,1)).Font.Size=20;
this.oSheet.Range(this.oSheet.Cells(1,1), this.oSheet.Cells(1,1)).Font.Bold = true;
this.oSheet.Range(this.oSheet.Cells(1,1), this.oSheet.Cells(1,1)).HorizontalAlignment = -4108; //居中
this.oSheet.Range(this.oSheet.Cells(1,1), this.oSheet.Cells(1,1)).Rows.RowHeight = 40;
}
//自动调整行高
}
//最后一行是否空色
try{
this.oSheet.Range(this.oSheet.Cells(this.rows,1), this.oSheet.Cells(this.rows,1)).Font.Color=this.lastRowColor;
}catch(e){}
this.oSheet.Range(this.oSheet.Cells(this.fromrow+2,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).Rows.RowHeight=20;
this.oSheet.Range(this.oSheet.Cells(this.fromrow+2,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).Font.Size=10;
//自动换行
this.oSheet.Range(this.oSheet.Cells(this.fromrow+2,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).WrapText = true;
//自动调整列宽
this.oSheet.Range(this.oSheet.Cells(this.fromrow+1,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).Columns.AutoFit();
//点虚线
//this.oSheet.Range(this.oSheet.Cells(this.fromrow+1,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).Borders.LineStyle = -4118;
return this.rows;
}
function test(tabId){
// JavaScript Document
//调用方法
var test=new PageToExcel(tabId,0,255,"测试.xls");//table id , 第几行开始,最后一行颜色 ,保存的文件名
test.CreateExcel(false);
test.Exec();
test.SaveAs();
test.CloseExcel();
//LastRowColor 0黑色 255红色
}
分享到:
相关推荐
js导出execl,自动合并单元格,自动增行,增列
Javascript导出excel为xlsx格式,兼容IE6+和主流浏览器,下载下来直接可以使用。 其他javascript导出excel插件可看作者文章:https://blog.csdn.net/qq_21693027/article/details/80459677
前端导出excel,可以控制样式,单元格合并居中等
一个导出excel的demo,包含导出的逻辑代码和依赖包,下载以后请执行 npm install ,然后起服务访问,不能直接以 file://打开哦
vue 导出excel,支持单元格合并,背景色,列宽,字体大小,一个js 文件就可以搞定
winform使用Microsoft.Office.Interop.Excel读取带有合并单元格的Excel的demo,Excel版本不限,可以是.xls可以是.xlsx版本。本程序采用webbrowser显示读取的数据,使用bootstrap的css样式美化table表格,使用Json...
使用nodejs将mysql数据库中的数据导出成excel格式,并附带mysql连接池及mysql模块的封装,以及三百万的mysql测试数据,内容详细。
在浏览器中将数据导出到excel或WPS的电子表格中是程序开发中经常在实现的功能,提供数据导出,详细的功能注释让你一目了然,如Excel文件的操作,工作表、工作簿操作,单元格操作,表格格式(表格线、行高、列宽、...
SheetJS免费版的不支持格式,比如居中、自动换行都不行。xlsx-style是SheetJS的一个分支,且支持各种格式,可以做到文字居中,自动换行,列宽设置,单元格合并,冻结表头等。
layui数据表格导出Excel插件 博客地址: https://www.cnblogs.com/YunRuiSoftWare/p/13346396.html
Javascript导出Excel,自动合并单元格、自动列宽、自动获取table页面数据进行合并单元格。列
自己写的一个关于页面表格(HtmlTable)导出为Execl的功能,专门用户一些复杂的报表导出,因为有些表头存在一些合并单元格、或合并行问题。
直接可以用的项目。数据量大,导出多个excel然后压缩成zip文件。。。
vue实现多sheet页导出所需文件Export2Excel.js
使用xlsx.full.min.js导出有复杂表头的excel(亲测有效) > 今天项目用到了导出table表格,但表头有点特殊,多个合并的单元格。 > 搜索了半天,发现有博主使用 ``xlsx.full.min.js`` 做过此类功能,使用的是vue+...
树结构 EXCEL 导出 解析树结构并导出 excel 自动计算并合并单元格 树结构保留 API简洁 高度内聚 依赖 FileSaver.js exceljs 数据格式要求 [ { value: "value 1", list: [ { value: "value 2" }, ] } ] 示例 ...
1.纯js+html实现抽签功能,导入excel文件,得到抽签结果,将抽签table导出为PDF(html2canvas+jsPdf)、Excel文件(js-xlsx,实现合并单元格,并设置单元格样式 字体、宽度、居中显示) 2.导入excel文件,生成小组赛...
1、页内数据导出excel 2、后端数组数据导出excel 3、自定义excel样式 4、自定义合并单元格
Export2Excel.js、Blob.js实现将数据导出为表格的形式
由于handsontable官方不支持excel导入,在尝试了sheetjs后,发现sheetjs会丢失单元格样式,于是转而使用exceljs完整了实现了Excel的读取和渲染。 handsontable非常强大,能够完全自定义单元格渲染方式和内容,是开发...