JS导出 excel
// JavaScript Document
//调用方法
// var test=new PageToExcel("data",0,255,"测试.xls");//table id , 第几行开始,最后一行颜色 ,保存的文件名
// test.CreateExcel(false);
// test.Exec();
// test.SaveAs();
// test.CloseExcel();
//LastRowColor 0黑色 255红色
//
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()+"日.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();
}
}
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; //退出循环
}
}
//try{
//赋值
//此处相应跟改 根据 标签的类型,替换相关参数
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;
}
JS导出 excel相关推荐
- 浏览器端JS导出EXCEL
浏览器端JS导出EXCEL FileSaver.js 实现了在本身不支持 HTML5 W3C saveAs() FileSaver 接口的浏览器支持文件保存.FileSaver.js 在客户端保存文件 ...
- 通过javascript获取sharepoint数据,使用JS导出Excel
通过网页将数据导出到Excel是常见需求,使用服务器端导出,开发周期长,部署麻烦.这里推荐一种客户端导出方法. 客户端导出与Sharepoint无关.这里附带使用场景基于SP,因为SP自带的导出功能比 ...
- chrome浏览器使用js导出Excel出现网络错误
使用js 导出Excel由于数据量太大导致出现网络错误,如图所示: 解决办法: 表格绘制完成后,使用 Blob let blob = new Blob([format(template, ctx]); ...
- 四种利用js导出Excel的方法(兼容IE6+、主流浏览器、支持复杂表头和合并单元格)
因为项目需求变更,最后决定使用做JS导出Excel,网上看了很多的帖子和例子,很多的例子并不能满足需求( 处理复杂表头,兼容主流浏览器,兼容IE等等).所以,自己找了几个比较不错的例子,在其基础上结合 ...
- 关于IE8浏览器JS导出excel,要使导出列宽度按自己控制。
关于IE8浏览器JS导出excel,要使导出列宽度按自己控制. function getExplorer() { var explorer = window.navigator.userAgent ; ...
- 纯js导出Excel文件(无需引入插件)
纯js导出Excel文件(无需引入插件) 提示:代码没有来得及整理,大致代码都在这里 let thlist = [[{text: '序号', rowspan: '2'},{text: '姓名', ro ...
- js导出excel(js-export-excel)
js导出excel表格 这个需求在后台管理系统中很常见, 我们使用js-export-excel进行excel的导出 首先在我们的项目中安装这个包 npm install js-export-exce ...
- js导出EXCEL js导出EXCEL
参考一:http://wenku.baidu.com/view/7b81f3eb6294dd88d0d26b57.html 参考二: js导出EXCEL js导出EXCEL <html> ...
- js导出excel(带边框以及文本样式)
js导出excel 假定有这样一个页面 <div class="content-table"><table lay-filter="queryTable ...
- html table导出excel 插入图片_前端 Table 用 JS 导出EXCEL(支持大量数据,保留报表格式) - pensive2019...
最近项目上,需要用到将网页上的table报表导出Excel.原先一直用,面对简单的数结构时只要简单的套用就能导出了,但是table结构相对复杂时,很难在组成一样结构,要花很多时间调:这时就想到在百度上 ...
最新文章
- AtCoder Regular Contest 067 F - Yakiniku Restaurants
- leetcode 35 Search Insert Position(二分法)
- JQUERY的split
- 中医:看脚十秒钟可知身体疾病
- C语言关键字以及-格式输入输出中“%d,%o,%x,%e,%f”等的含义
- webpack的css样式文件加载依赖
- CVPR 2022NTIRE 2022|首个用于高光谱图像重建的 Transformer
- 台达DOP-B触摸屏通过MODBUS RTU通讯4台台达M变频器.含触摸屏程序,接线图和变频器参数设定
- 实验五 CA的安装和使用
- 杜撰的柏拉图(转自李止介的个人空间)
- 安卓投屏软件_免费领取15天懒人听书会员+安卓美食菜谱整合app+安卓乐播投屏+办公软件幕布405天免费领取...
- 新疆计算机一级考试excel公式,2020年XX专业技术人员继续教育公需课《Excel快速统计》试题及答案...
- 计算机笔记--【并发编程①】
- android 相册view,Android直接把当前View保存到相册
- mc服务器对硬盘有要求吗,我的世界电脑配置要求是什么_我的世界电脑配置要求高吗_玩游戏网...
- 用python制作二维码_用python做一个可视化生成二维码的工具
- CAD图纸中,提取标注的方法是什么呢?
- 改变输出的文字的字体格式
- Docker学习笔记7——Docker-Compose(幕布笔记)
- 全国计算机优秀教师奖励大会,优秀教师表彰奖励决定