如何搜尋Excel的內容? |
答題得分者是:Rain
|
Bryan
一般會員 發表:6 回覆:8 積分:2 註冊:2002-03-13 發送簡訊給我 |
|
Rain
資深會員 發表:31 回覆:236 積分:268 註冊:2003-02-17 發送簡訊給我 |
您好:
1,試試ExcelWorksheet1.Cells.Find();
2,以前在大富翁論壇上看到的一個操作Excel的單元代碼可以實現,帖子很長,但很有參考價值,我把其中實現Excel中查找的部分貼出來,不妨打開您的Excel檔然後試試看。這段代碼來自Experts-Exchange,現在那邊有許多限制,所以無法去找一下來源,如果有哪位朋友知道最初的出處,還煩請貼個連接位址,謝謝。大富翁論壇上的帖子位置:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=737517 下麵是轉貼的實現代碼:
{Returns The Last Column} Function ExcelLastCol(Excel : Variant): Integer; Var CurRow : Integer; CurCol : Integer; Begin Result := 1; Try CurRow := Excel.ActiveCell.Row; CurCol := Excel.ActiveCell.Column; Result := CurCol; Excel.Selection.End[xlToRight].Select; Result := Excel.ActiveCell.Column; Excel.ActiveSheet.Cells[CurRow, CurCol].Select; Except End; End; {Returns The Last Row} Function ExcelLastRow(Excel : Variant): Integer; Var CurRow : Integer; CurCol : Integer; Begin Result := 1; Try CurRow := Excel.ActiveCell.Row; CurCol := Excel.ActiveCell.Column; Result := CurRow; Excel.Selection.End[xlDown].Select; Result := Excel.ActiveCell.Row; Excel.ActiveSheet.Cells[CurRow, CurCol].Select; Except End; End; {Returns The First Row} Function ExcelFirstRow(Excel : Variant): Integer; Var CurRow : Integer; CurCol : Integer; Begin Result := 1; Try CurRow := Excel.ActiveCell.Row; CurCol := Excel.ActiveCell.Column; Result := CurRow; Excel.Selection.End[xlUp].Select; Result := Excel.ActiveCell.Row; Excel.ActiveSheet.Cells[CurRow, CurCol].Select; Except End; End; {Returns The First Col} Function ExcelFirstCol(Excel : Variant): Integer; Var CurRow : Integer; CurCol : Integer; Begin Result := 1; Try CurRow := Excel.ActiveCell.Row; CurCol := Excel.ActiveCell.Column; Result := CurRow; Excel.Selection.End[xlToLeft].Select; Result := Excel.ActiveCell.Column; Excel.ActiveSheet.Cells[CurRow, CurCol].Select; Except End; End; {Returns the contents of a cell as a shortstring} Function ExcelGetCellValue(Excel : Variant; RowNum, ColNum: Integer): ShortString; Begin Result := ''; Try Result := Excel.Cells[RowNum, ColNum].Value; Except Result := ''; End; End; Function ExcelFindValue( Excel : Variant; FindString : ShortString; TopRow : Integer; LeftCol : Integer; LastRow : Integer; LastCol : Integer; SearchRight : Boolean; SearchDown : Boolean; RowsFirst : Boolean ): Boolean; Var CurRow : Integer; CurCol : Integer; TopRowN : Integer; LeftColN : Integer; LastRowN : Integer; LastColN : Integer; ColLoop : Integer; RowLoop : Integer; CellValue : ShortString; FoundRow : Integer; FoundCol : Integer; Found : Boolean; Begin Result := False; Try Found := False; FindString := UpperCase(FindString); CurRow := Excel.ActiveCell.Row; CurCol := Excel.ActiveCell.Column; FoundRow := CurRow; FoundCol := CurCol; If SearchRight Then Begin LeftColN := LeftCol; LastColN := LastCol; End Else Begin LeftColN := LastCol; LastColN := LeftCol; End; If SearchDown Then Begin TopRowN := TopRow; LastRowN := LastRow; End Else Begin TopRowN := LastRow; LastRowN := TopRow; End; If RowsFirst Then Begin For ColLoop := LeftColN To LastColN Do Begin For RowLoop := TopRowN To LastRowN Do Begin CellValue := ExcelGetCellValue(Excel,RowLoop, ColLoop); If UpperCase(CellValue) = FindString Then Begin FoundRow := RowLoop; FoundCol := ColLoop; Found := True; Break; End; End; If Found Then Break; End; End Else Begin For RowLoop := TopRowN To LastRowN Do Begin For ColLoop := LeftColN To LastColN Do Begin CellValue := ExcelGetCellValue(Excel,RowLoop, ColLoop); If UpperCase(CellValue) = FindString Then Begin FoundRow := RowLoop; FoundCol := ColLoop; Found := True; Break; End; End; If Found Then Break; End; End; Excel.Cells[FoundRow, FoundCol].Activate; Result := Found; Except Result := False; End; End; {Finds A value in a range and moves the cursor there. If the value is not found then the cursor does not move. If nothing is found then false is returned, True otherwise.} Function ExcelFindInRange( Excel : Variant; FindString : ShortString; TopRow : Integer; LeftCol : Integer; LastRow : Integer; LastCol : Integer): Boolean; Begin Result := ExcelFindValue( Excel, FindString, TopRow, LeftCol, LastRow, LastCol, True, True, True); End; {Finds A value and moves the cursor there. If the value is not found then the cursor does not move. If nothing is found then false is returned, True otherwise.} Function ExcelFind( Excel : Variant; FindString : ShortString): Boolean; Begin Result := ExcelFindInRange( Excel, FindString, ExcelFirstRow(Excel), ExcelFirstCol(Excel), ExcelLastRow(Excel), ExcelLastCol(Excel)); End; 調用: uses Comobj; procedure TForm1.Button1Click(Sender: TObject); var AExcel: Variant; begin try AExcel := GetActiveOleObject('Excel.Application'); AExcel.Application.Visible := True; ExcelFind(AExcel, 'Delphi'); except raise; end; end; |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |