複製代碼 代碼如下: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();
}
}