在EXCEL中搜索关键字
/*
打开EXCEL文件,按关键字判断是否符合
参数:strFileTable 文件数组列表
*/
function fu_FindExcel(strFileTable){
var excelServer ;
var oBook; //工作簿
var oSheet; //工作表
var RowCount,ColCount;
var nRow,nCol ;
var oCell ;
var len;
var index ;
var strKey; //要查找的关键字
var nXh; //序号
var nFind; //查找字符串的位置
var strText; //单元格内容转换为TEXT
var strColName; //列号对应的列名
excelSever = com.excel(true);
excelSever.Visible = false; //隐藏不可见
excelSever.alerts = false; //关闭所有操作提示
len = table.len(strFileTable);
strKey = string.trim(mainForm.edt_key.text);
nRow = 2;
nCol = 2;
nXh = 1
for(index=1;len;1){
mainForm.prog1.pos = index / len * 1000;
mainForm.stTitle.text = "正在搜索文件:" + strFileTable[index];
oBook = excelSever.Open(strFileTable[index]);
oSheet = oBook.Sheets(1);
RowCount = oSheet.UsedRange.Rows.count;
ColCount = oSheet.UsedRange.Columns.count;
for(nRow=1;RowCount;1){
for(nCol=1;ColCount;1){
oCell = oSheet.Cells(nRow,nCol).getValue2();
if(oCell == null){
strText = "";
}
else {
strText = string.format("%s",oCell );
}
//判断单元格内容是否包含关键字
nFind = string.indexOf(strText,strKey);
if(nFind==null){
//没找到,不处理
}
else {
strColName = fu_ColToName(nCol); //获取列号对应的字母
mainForm.list_Find.addItem({text={string.format("%d", nXh),strFileTable[index],strKey,string.format("%d",nRow ),strColName,strText}});
nXh = nXh + 1;
}
}
}
//oCell = oSheet.Cells(nRow,nCol).getValue2();
//mainForm.list_Find.addItem({text={string.format("%d", index),strFileTable[index],strKey,string.format("%d",nRow ),string.format("%d",nCol ),oCell}});
oBook.close();
}
excelSever.Quit();
mainForm.stTitle.text = "搜索完毕";
}