Js 复制页面内容到Excel中

/**
* 说明
* 导出指定元素内数据到Excel中,同时可以指定导出数据在Excel中的边框,字体,行高
* 示例1
* var ee = new Export2Excel();
* ee.Export("tableId1");
* 示例2
* var ee = new Export2Excel();
* ee.Export(id, function () {
* ee.SetBorder(ee.DefaultBorder);
* ee.SetFont({ Name: "宋体", Size: 14 });
* ee.SetRowHeight(ee.DefaultRowHeight);
* ee.SetMergeCells({ Range: "A10:AF10" });
* ee.SetAlign({ Range: "A10:AF10", Align: ee.Align.Center });
* });
*/
function Export2Excel() { }
Export2Excel.prototype
= {

"None": -4142,

"DefaultFont": { Range: "A5:AF7", Name: "仿宋", FontStyle: "常规", Size: 12 },

"DefaultRowHeight": { Range: "A9:AF9", Height: 50 },

"DefaultBorder": { Range: "A5:AF9", LineStyle: 1, Color: -4947386, TintAndShade: 0, Weight: 2 },

"DefaultAlign": { Align: -4131, VAlign: -4108 },

"Align": { Left: -4131, Center: -4108, Right: -4152 },

Export: function (sElementID, callBackFn) {
try {
//创建Excel 处理对象
this.__Appliction = new ActiveXObject("Excel.Application");
}
catch (e) {
var sErrorMessage
= "使用导出功能\n1、您必须安装Excel电子表格软件.\n2、设置浏览器(IE)选项[安全]将此网站加入可信站点\n" +
"3、并启动该自定义级别中的'对未标记为可安全执行脚本的ActiveX控件初始化并执行脚本'选项\n最后刷新页面"
alert(sErrorMessage);
return;
}
//增加工作簿
this.__WorkBook = this.__Appliction.Workbooks.Add();
//获取当前活动的Sheet
this.__ActiveSheet = this.__WorkBook.ActiveSheet;
//创建文本区域
var TextRange = document.body.createTextRange();

var oEelement
= document.getElementById(sElementID);
//把table中的数据移到textRange中
TextRange.moveToElementText(oEelement);

TextRange.select();
//选中textRange中所有数据

TextRange.execCommand(
"Copy");

//移动焦点,以便清除之前选中的文本区

ClearFocus();
//粘贴数据到Excel中
this.__ActiveSheet.Paste();
try {
if (callBackFn) {
callBackFn.call(document,
this);
}
else {
this.SetBorder(this.DefaultBorder);
this.SetFont(this.DefaultFont);
this.SetRowHeight(this.DefaultRowHeight);
}
this.__Appliction.Visible = true;
}
catch (e) {
this.__WorkBook.Close(savechanges = false);
this.__Appliction.Quit();
this.__Appliction = null;
alert(e.message);

}
finally {
//内存清理
idTmr = window.setInterval(function () {
window.clearInterval(idTmr);
CollectGarbage();
},
1);
}
function ClearFocus() {
var el
= document.getElementById("txtClearFocus");
if (el == null) {
el
= document.createElement("<input id='txtClearFocus' type='text' style='width:0;height:0'>");
document.body.appendChild(el);
}
el.focus();
}
},
/**
* 说明
* 设置选中区的边框样式
* 示例
* SetBorder({LineStyle:'线条样式',Color:'边框颜色',Weight='边框粗细'});
* SetBorder({LineStyle: 1,Color:-4947386,Weight=2});
*/
SetBorder: function (opts) {
try {
opts
= $.extend(this.DefaultBorder, opts);
oSelection
= this.CreateRangeSelection(opts.Range);
oSelection.Borders(
5).LineStyle = this.None;
oSelection.Borders(
6).LineStyle = this.None;
var arrBorders
= "7 8 9 10 11 12".split(" ");
for (var i = 0; i < arrBorders.length; i++) {
var selectionBorder
= oSelection.Borders(arrBorders[i]);
selectionBorder.LineStyle
= opts.LineStyle;
selectionBorder.Color
= opts.Color;
selectionBorder.TintAndShade
= opts.TintAndShade;
selectionBorder.Weight
= opts.Weight;
}
}
catch (e) {
throw new Error("设置选中区边框发生了错误\n" + e.message);
}
},
/**
* 说明
* 设置选中区的行高
* 示例
* SetRowHeight({Height:'行高'});
* SetRowHeight({Height:50});
*/
SetRowHeight: function (opts) {
try {
opts
= $.extend(this.DefaultRowHeight, opts);
var oSelection
= this.CreateRangeSelection(opts.Range);
oSelection.RowHeight
= opts.Height;
}
catch (e) {
throw new Error("设置选中区行高发生了错误\n" + e.message);
}
},
/**
* 说明
* 设置选中区的字体
* 示例
* SetFont({Name='字体名称',Size='字体大小',FontStyle='字体样式[常规,粗体,斜体]'});
* SetFont({Name='宋体',Size=12,FontStyle='常规'});
*/
SetFont: function (opts) {
try {
opts
= $.extend(this.DefaultFont, opts);
var oSelection
= this.CreateRangeSelection(opts.Range);
oSelection.Font.Name
= opts.Name;
oSelection.Font.FontStyle
= opts.FontStyle;
oSelection.Font.Size
= opts.Size;
// oSelection.Font.Bold = false;
// oSelection.Font.WrapText = true;
// oSelection.Font.Italic = false;
} catch (e) {
throw new Error("设置选中区字体发生了错误\n" + e.message);
}
},
/**
* 说明
* 设置选中区合并
* 示例
* SetMergeCells({Range:"A1:B2"});合并第一和第二行前两列
*/
SetMergeCells: function (opts) {
try {
opts
= $.extend(this.DefaultFont, opts);
var oSelection
= this.CreateRangeSelection(opts.Range);
oSelection.MergeCells
= true;
}
catch (e) {
throw new Error("设置选中区合并时发生了错误\n" + e.message);
}
},
/**
* 说明
* 设置选中区内容位置
* 示例
* SetAlign({Range:"A1:B2",Align:obj.Align.Center});第一和第二行中内容水平居中
*/
SetAlign: function (opts) {
try {
opts
= $.extend(this.DefaultAlign, opts);
var oSelection
= this.CreateRangeSelection(opts.Range);
oSelection.HorizontalAlignment
= opts.Align;
oSelection.VerticalAlignment
= opts.VAlign;
}
catch (e) {
throw new Error("设置选中区内容位置发生了错误\n" + e.message);
}
},
/**
* 说明
* 创建Excel选中区
* 示例
* CreateRangeSelection("A1:B2");第一和第二行前两列
*/
CreateRangeSelection: function (sRange) {
try {
this.__ActiveSheet.Range(sRange).Select();
return this.__Appliction.Selection;
}
catch (e) {
throw new Error("无效的选中区(" + sRange + "");
}
}
}

  

posted @ 2011-08-10 12:11  昔日醉离愁  阅读(1326)  评论(1编辑  收藏  举报