var Excel = function(){
this.initConfig = {
name: '',
sheetName: '',
data : null
}
try{
var args = arguments[0];
for(var i in args) {
if(this.initConfig.hasOwnProperty(i)) {
this.initConfig[i] = args[i];
}
}
if(window.ActiveXObject) {
this.xlApp = new ActiveXObject("Excel.Application");
this.initIE();
} else {
if(!this.uri) {
this.uri = 'data:application/vnd.ms-excel;base64,';
}
if(!this.template){
this.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><table>{table}</table></body></html>';
}
this.initNotIE();
}
} catch(e) {
throw e;
}
}
Excel.prototype.initIE = function() {
this.xlBook = this.xlApp.Workbooks.Add;
this.xlSheet = this.xlBook.Worksheets(1);
this.xlSheet.name = this.initConfig.sheetName;
this.xlSheet = this.xlBook.ActiveSheet;
this.xlSheet.visible = true;
var title = this.initConfig.data.title;
var content = this.initConfig.data.content;
if(title) {
for(var i = 0, len = title.length; i < len; i++) {
this.xlSheet.Cells(1, i).Value = title[i];
}
}
if(content) {
var row = 1;
for(var i = 0, len = content.length; i < len; i++) {
var item = content[i];
row++;
for(var j = 0, size = item.length; j < size; j++){
this.xlSheet.Cells(row, j).Value = item[j];
}
}
}
this.save();
this.destroy();
};
Excel.prototype.initNotIE = function(){
var td = '<td>{value}</td>'
var tr = '<tr>{value}</tr>';
var title = this.initConfig.data.title;
var content = this.initConfig.data.content;
var html = '';
if(title) {
var headers = [];
for(var i = 0, len = title.length; i < len; i++) {
headers.push(this.format(td, {value:!title[i]?'':title[i]}));
}
var header = this.format(tr, {value: headers.join('')});
html += header;
}
if(content) {
for(var i = 0, len = content.length; i < len; i++) {
var item = content[i];
var contents = [];
for(var j = 0, size = item.length; j < size; j++){
contents.push(this.format(td, {value: !item[j]?'':item[j]}));
}
html += this.format(tr, {value: contents.join('')});
}
}
var name = this.initConfig.sheetName;
var ctx = {worksheet: name, table: html};
window.location.href = this.uri + this.encode64(this.format(this.template, ctx));
}
Excel.prototype.encode64 = function(s) {
return window.btoa(unescape(encodeURIComponent(s)));
}
Excel.prototype.format = function(str, array) {
return str.replace(/{(\w+)}/g, function(m, p) { return array[p]; })
}
Excel.prototype.save = function(){
if(this.xlApp) {
this.xlApp.Save();
} else {
}
}
Excel.prototype.destroy = function() {
if(this.xlApp) {
this.xlApp.UserControl = true
this.xlApp.Quit();
this.xlApp = null;
this.initConfig = {
name: '',
sheetName: '',
data : null
}
try{
var args = arguments[0];
for(var i in args) {
if(this.initConfig.hasOwnProperty(i)) {
this.initConfig[i] = args[i];
}
}
if(window.ActiveXObject) {
this.xlApp = new ActiveXObject("Excel.Application");
this.initIE();
} else {
if(!this.uri) {
this.uri = 'data:application/vnd.ms-excel;base64,';
}
if(!this.template){
this.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><table>{table}</table></body></html>';
}
this.initNotIE();
}
} catch(e) {
throw e;
}
}
Excel.prototype.initIE = function() {
this.xlBook = this.xlApp.Workbooks.Add;
this.xlSheet = this.xlBook.Worksheets(1);
this.xlSheet.name = this.initConfig.sheetName;
this.xlSheet = this.xlBook.ActiveSheet;
this.xlSheet.visible = true;
var title = this.initConfig.data.title;
var content = this.initConfig.data.content;
if(title) {
for(var i = 0, len = title.length; i < len; i++) {
this.xlSheet.Cells(1, i).Value = title[i];
}
}
if(content) {
var row = 1;
for(var i = 0, len = content.length; i < len; i++) {
var item = content[i];
row++;
for(var j = 0, size = item.length; j < size; j++){
this.xlSheet.Cells(row, j).Value = item[j];
}
}
}
this.save();
this.destroy();
};
Excel.prototype.initNotIE = function(){
var td = '<td>{value}</td>'
var tr = '<tr>{value}</tr>';
var title = this.initConfig.data.title;
var content = this.initConfig.data.content;
var html = '';
if(title) {
var headers = [];
for(var i = 0, len = title.length; i < len; i++) {
headers.push(this.format(td, {value:!title[i]?'':title[i]}));
}
var header = this.format(tr, {value: headers.join('')});
html += header;
}
if(content) {
for(var i = 0, len = content.length; i < len; i++) {
var item = content[i];
var contents = [];
for(var j = 0, size = item.length; j < size; j++){
contents.push(this.format(td, {value: !item[j]?'':item[j]}));
}
html += this.format(tr, {value: contents.join('')});
}
}
var name = this.initConfig.sheetName;
var ctx = {worksheet: name, table: html};
window.location.href = this.uri + this.encode64(this.format(this.template, ctx));
}
Excel.prototype.encode64 = function(s) {
return window.btoa(unescape(encodeURIComponent(s)));
}
Excel.prototype.format = function(str, array) {
return str.replace(/{(\w+)}/g, function(m, p) { return array[p]; })
}
Excel.prototype.save = function(){
if(this.xlApp) {
this.xlApp.Save();
} else {
}
}
Excel.prototype.destroy = function() {
if(this.xlApp) {
this.xlApp.UserControl = true
this.xlApp.Quit();
this.xlApp = null;
}
使用简介:
new Excel({
name: 'Demo for yourself',
sheetName: 'Demo for yourself',
data : {
title: ['Column1','Column2','Column3'],
content: ['Value1','Value2','Value3']
}
});