javascript EXCEL 操作类代码

      2020-01-11 10:29      编程语言
有空把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();
}
}