Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  javascript  >  正文 javascript excel操作类实现代码

javascript excel操作类实现代码

发布时间:2015-08-10   编辑:www.jquerycn.cn
分享一个javascript操作excel文件的类,javascript excel操作类代码,感兴趣的朋友参考下。

把JAVASCRIPT对EXCEL的操作进行了封装,可在此基础上加以扩展。

例子,javascript操作excel文件。
 

复制代码 代码示例:
ExcelOperation = function(){
this.oXL = null;
this.oWB = null;
this.oSheet = null;
this.xlsRowCount = 0; //总记录数
this.excelFileName = null;
this.currentRow = 2; //当前行
/**
* 得到EXCEL表格中的总记录数
*/
this.getRowCount = function(){
//oSheet.Range("C1").Sort(oSheet.Columns("C"),xlAscending);
var rowsCount = this.oSheet.UsedRange.Cells.Rows.Count;
return rowsCount;
}
/**
* 按指定的列进行排序
* @param column 列名,如"C"
*/
this.sort = function(column){
var xlAscending = 1;
var xlYes = 1;
var xlSortRows=1;
var xlPinYin= 1;
var xlSortNormal =1;
this.oSheet.UsedRange.Sort(this.oSheet.Columns(column),
xlAscending,null,null,null,null,null,xlYes,null,null,
xlSortRows,xlPinYin,xlSortNormal,null,null);
}
/**
* 打开一个EXCEL
*/
this.openExcel = function(fileName){
this.fileName = fileName;
if(this.fileName){
try{
this.oXL = new ActiveXObject("Excel.application");
this.oWB = this.oXL.Workbooks.open(fileName);
//"e:\\join.xls"
this.oWB.worksheets(1).select();
this.oSheet = this.oWB.ActiveSheet;
this.xlsRowCount = this.getRowCount();
}catch(e){
if(this.oXL)
this.closeExcel();
Ext.Msg.show({
title : '错误提示',
msg : '请检查您的系统以下几方面的设置:1,'+
'是否正确安装了OFFICE中的EXCEL;2,正确设
置您的IE浏览器('+
'工具->internet选项->安全->internet->自定
义级别->'+
'启用“对没有标记为安全的ActiveX控件...”
);3,数据文件是否被删除',
buttons : Ext.Msg.OK,
icon : Ext.Msg.ERROR
});
return false;
}
}else{
Ext.Msg.show({
title : '错误提示',
msg : '请选择要导入的源数据文件!',
buttons : Ext.Msg.OK,
icon : Ext.Msg.ERROR
});
return false;
}
return this.oSheet;
}
/**
* 读取指定单元格的数据,
*/
this.readData = function(row,col){
var data = this.oSheet.Cells(row,col).Value;
if(typeof data == 'undefined')
return '';
else
return data;
}
/**
* 向指定单元格写入数据
*/
this.writeData = function(row,col,data){
this.oSheet.Cells(row,col) = data
}
/**
* 关闭EXCEL
*/
this.closeExcel = function(){
this.oXL.DisplayAlerts = false;
this.oXL.Quit();
this.oXL = null;
this.oWB=null;
this.oSheet=null;
CollectGarbage();
}
}

您可能感兴趣的文章:
javascript excel操作类实现代码
javascript操作excel文件方法详解
php读取excel的实例代码
解决php下载excel无法打开的问题
.net中清除EXCEL进程最有效的方法
javascript 操作excel代码示例
Python控制Excel实现自动化办公
php导出word文档与excel表格文件
js实现保存指定网页为Excel文件的代码
[C#]结束Excel的进程的方法

[关闭]