<big>在Delphi中对Excel 2000进行操作的若干问题!!!</big>(200分)

  • 主题发起人 主题发起人 honestman
  • 开始时间 开始时间
H

honestman

Unregistered / Unconfirmed
GUEST, unregistred user!
E文差,要在Vbaxl9.chm中找资料实在太恐怖了。

无奈之下只好用分买答案!
  我想把TStringGrid控件中的内容导出到一个Excel文件中,方便提交和打印。
1、在Excel 2000中有条虚线作为页与页之间的界线,但是在程序中怎样知道这条界线在
哪个Cells下面呢???
2、如何对某个范围的Cells设置它们的边框呢???
3、如何插入一行???
4、如何把某个范围的Cells复制到另一个地方。
 
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&amp;qid=11954158
太长了,我就不贴过来了。
 
to unreal:
  我去那里看过了。里面的函数虽然多,但是符合要求的好象比较少。而里面的连接:
&lt;center&gt;&lt;a href="http://www.djpate.freeserve.co.uk/AutoExcl.htm"&gt;http://www.djpate.freeserve.co.uk/AutoExcl.htm&lt;/a&gt;&lt;/center&gt;
我是觉得不错的!我去看看。
 
大虾:
  两个地方我都看过了,好象只是解决了4,其他的都没有的。
 
1、 worksheet.HPageBreaks[1].Location 水平分页符下面的格子(range对象)
worksheet.VPageBreaks[1].Location 垂直分页符右面的格子(range对象)

2、worksheet.Range["A1:D10"].Borders[xlEdgeLeft].LineStyle=xlDashDot
worksheet.Range["A1:D10"].Borders[xlEdgeLeft].Color=RGB(255,0,0)

3、sheet.Rows[4].Insert 第4行前插入一行

4、sheet1.Range("A1:D4").Copy(sheet2.Range("E5"))
表一的a1-d4 copy 到表二的 e5开始的地方
 
上不去。:(
上去的朋友把里面的东西贴出来好吗?
 
to Pipi.:
  我试试,可以的话,此贴就可以结束了。

to 老吴:
  那两份资料很长的,单是第一份的其中一部分如果打印都要三十多页。
  你有需要的话,另外开个帖子我帖上去。不用给太多分--0分就可以了。
 
who say 贴不下来?look me!


Welcome
chengjian

Question Points: 2602
Questions Asked: 36
Expert Points: 3012
Questions Answered: 22
&amp;#8226; Earn Points
&amp;#8226; Member Profile
&amp;#8226; Logout


EE Updates

&amp;#8226; What's New

EE Community

&amp;#8226; Winner's Circle
&amp;#8226; Experts Only
&amp;#8226; Help Desk
&amp;#8226; Community Support
&amp;#8226; Expert Input
&amp;#8226; Get a Free T-Shirt
&amp;#8226; Expert Hall of Fame

EE Info

&amp;#8226; About Us
&amp;#8226; The EE Network
&amp;#8226; Advertise
&amp;#8226; All Topics
&amp;#8226; Home

Most Popular Topics

1. Visual Basic
2. Windows 98
3. Microsoft Access
4. Delphi Programming
5. Active Server Pages
6. Java Programming
7. C++ Programming
8. JavaScript
9. Hardware
10. MFC Programming
Delphi Programming Question Ask A Question Other Questions Reload Question
Question: how to access MS EXCEL worksheet content Date: 11/23/2000 02:51AM PST
From: xiemingsuk
Status: Waiting for Answer Points: 50
Email A Friend
Please tell me how to access MS EXCEL worksheet content and put them into my delphi's database (PARADOX).
Thank you

Question History
Rejected Answer
From: Tal_avidor Date: 11/23/2000 05:13AM PST
you can use Automation Objects
ActiveX

var
APP_Excel: OleVariant;
WorkBook: OleVariant;
Sheet: OleVariant;
....
...
APP_Excel := CreateOleObject('Excel.Application');
WorkBook := APP_Excel.Workbooks.Add;
Sheet := WorkBook.Worksheets('Sheet1')
....
Data:=Sheets.Cell[1,1];
....
APP_Excel.Close;


you can look in MSDN for the Objects Methods list.



if you need more help i can give u a full source for reading excel sheets
Comment
From: xiemingsuk Date: 11/23/2000 04:25PM PST
Tal Avidor,
Thank you for your answer, but I need the full source code.
As I don't know how to contact you, I have to reject your answer, hoping that you will send me the full
source code.
I believe that your help will be graded 'B'.

Thanks,

Bronson
Comment
From: Tal_avidor Date: 11/24/2000 01:16AM PST
hi xieminqsuk

this is a unit that works good luke !!!






unit ads_excel;


interface

uses
Dialogs, Messages, SysUtils, Grids, Cmp_Sec, ComObj, Ads_Misc;

{!~Add a blank WorkSheet}
Function ExcelAddWorkSheet(Excel : Variant): Boolean;

{!~Close Excel}
Function ExcelClose(Excel : Variant; SaveAll: Boolean): Boolean;

{!~Returns the Column String Value from its integer equilavent.}
Function ExcelColIntToStr(ColNum: Integer): ShortString;

{!~Returns the Column Integer Value from its Alpha equilavent.}
Function ExcelColStrToInt(ColStr: ShortString): Integer;

{!~Close All Workbooks. All workbooks can be saved or not.}
Function ExcelCloseWorkBooks(Excel : Variant; SaveAll: Boolean): Boolean;

{!~Copies a range of Excel Cells to a Delphi StringGrid. If successful
True is returned, False otherwise. If SizeStringGridToFit is True
then the StringGrid is resized to be exactly the correct dimensions to
receive the input Excel cells, otherwise the StringGrid is not resized.
If ClearStringGridFirst is true then any cells outside the input range
are cleared, otherwise existing values are retained. Please not that the
Excel cell coordinates are "1" based and the Delphi StringGrid coordinates
are zero based.}
Function ExcelCopyToStringGrid(
Excel : Variant;
ExcelFirstRow : Integer;
ExcelFirstCol : Integer;
ExcelLastRow : Integer;
ExcelLastCol : Integer;
StringGrid : TStringGrid;
StringGridFirstRow : Integer;
StringGridFirstCol : Integer;
{Make the StringGrid the same size as the input range}
SizeStringGridToFit : Boolean;
{cells outside input range in StringGrid are cleared}
ClearStringGridFirst : Boolean
): Boolean;

{!~Delete a WorkSheet by Name}
Function ExcelDeleteWorkSheet(
Excel : Variant;
SheetName : ShortString): Boolean;

{!~Moves the cursor to the last row and column}
Function ExcelEnd(Excel : Variant): Boolean;

{!~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;

{!~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;

{!~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. The search directions can be defined.
If you want row searches to go from left to right then SearchRight should
be set to true, False otherwise. If you want column searches to go from
top to bottom then SearchDown should be set to true, false otherwise.
If RowsFirst is set to true then all the columns in a complete row will be
searched.}
Function ExcelFindValue(
Excel : Variant;
FindString : ShortString;
TopRow : Integer;
LeftCol : Integer;
LastRow : Integer;
LastCol : Integer;
SearchRight : Boolean;
SearchDown : Boolean;
RowsFirst : Boolean
): Boolean;

{!~Returns The First Col}
Function ExcelFirstCol(Excel : Variant): Integer;

{!~Returns The First Row}
Function ExcelFirstRow(Excel : Variant): Integer;

{!~Returns the name of the currently active worksheet
as a shortstring}
Function ExcelGetActiveSheetName(Excel : Variant): ShortString;

{!~Gets the formula in a cell.}
Function ExcelGetCellFormula(
Excel : Variant;
RowNum, ColNum: Integer): ShortString;

{!~Returns the contents of a cell as a shortstring}
Function ExcelGetCellValue(Excel : Variant; RowNum, ColNum: Integer): ShortString;

{!~Returns the the current column}
Function ExcelGetCol(Excel : Variant): Integer;

{!~Returns the the current row}
Function ExcelGetRow(Excel : Variant): Integer;

{!~Moves the cursor to the last column}
Function ExcelGoToLastCol(Excel : Variant): Boolean;

{!~Moves the cursor to the last row}
Function ExcelGoToLastRow(Excel : Variant): Boolean;

{!~Moves the cursor to the Leftmost Column}
Function ExcelGoToLeftmostCol(Excel : Variant): Boolean;

{!~Moves the cursor to the Top row}
Function ExcelGoToTopRow(Excel : Variant): Boolean;

{!~Moves the cursor to Home position, i.e., A1}
Function ExcelHome(Excel : Variant): Boolean;

{!~Returns The Last Column}
Function ExcelLastCol(Excel : Variant): Integer;

{!~Returns The Last Row}
Function ExcelLastRow(Excel : Variant): Integer;

{!~Open the file you want to work within Excel. If you want to
take advantage of optional parameters then you should use
ExcelOpenFileComplex}
Function ExcelOpenFile(Excel : Variant; FileName : String): Boolean;

{!~Open the file you want to work within Excel. If you want to
take advantage of optional parameters then you should use
ExcelOpenFileComplex}
Function ExcelOpenFileComplex(
Excel : Variant;
FileName : String;
UpdateLinks : Integer;
ReadOnly : Boolean;
Format : Integer;
Password : ShortString): Boolean;

{!~Saves the range on the currently active sheet
to to values only.}
Function ExcelPasteValuesOnly(
Excel : Variant;
ExcelFirstRow : Integer;
ExcelFirstCol : Integer;
ExcelLastRow : Integer;
ExcelLastCol : Integer): Boolean;

{!~Renames a worksheet.}
Function ExcelRenameSheet(
Excel : Variant;
OldName : ShortString;
NewName : ShortString): Boolean;

{!~Saves the range on the currently active sheet
to a DBase 4 table.}
Function ExcelSaveAsDBase4(
Excel : Variant;
ExcelFirstRow : Integer;
ExcelFirstCol : Integer;
ExcelLastRow : Integer;
ExcelLastCol : Integer;
OutFilePath : ShortString;
OutFileName : ShortString): Boolean;

{!~Saves the range on the currently active sheet
to a text file.}
Function ExcelSaveAsText(
Excel : Variant;
ExcelFirstRow : Integer;
ExcelFirstCol : Integer;
ExcelLastRow : Integer;
ExcelLastCol : Integer;
OutFilePath : ShortString;
OutFileName : ShortString): Boolean;

{!~Selects a range on the currently active sheet. From the
current cursor position a block is selected down and to the right.
The block proceeds down until an empty row is encountered. The
block proceeds right until an empty column is encountered.}
Function ExcelSelectBlock(
Excel : Variant;
FirstRow : Integer;
FirstCol : Integer): Boolean;

{!~Selects a range on the currently active sheet. From the
current cursor position a block is selected that contains
the currently active cell. The block proceeds in each
direction until an empty row or column is encountered.}
Function ExcelSelectBlockWhole(Excel: Variant): Boolean;

{!~Selects a cell on the currently active sheet}
Function ExcelSelectCell(Excel : Variant; RowNum, ColNum: Integer): Boolean;

{!~Selects a range on the currently active sheet}
Function ExcelSelectRange(
Excel : Variant;
FirstRow : Integer;
FirstCol : Integer;
LastRow : Integer;
LastCol : Integer): Boolean;

{!~Selects an Excel Sheet By Name}
Function ExcelSelectSheetByName(Excel : Variant; SheetName: String): Boolean;

{!~Sets the formula in a cell. Remember to include the equals sign "=".
If the function fails False is returned, True otherwise.}
Function ExcelSetCellFormula(
Excel : Variant;
FormulaString : ShortString;
RowNum, ColNum: Integer): Boolean;

{!~Sets the contents of a cell as a shortstring}
Function ExcelSetCellValue(
Excel : Variant;
RowNum, ColNum: Integer;
Value : ShortString): Boolean;

{!~Sets a Column Width on the currently active sheet}
Function ExcelSetColumnWidth(
Excel : Variant;
ColNum : Integer;
ColumnWidth: Integer): Boolean;

{!~Set Excel Visibility}
Function ExcelSetVisible(
Excel : Variant;
IsVisible: Boolean): Boolean;

{!~Saves the range on the currently active sheet
to values only.}
Function ExcelValuesOnly(
Excel : Variant;
ExcelFirstRow : Integer;
ExcelFirstCol : Integer;
ExcelLastRow : Integer;
ExcelLastCol : Integer): Boolean;

{!~Returns the Excel Version as a ShortString.}
Function ExcelVersion(Excel: Variant): ShortString;

Function IsBlockColSide(
Excel : Variant;
RowNum: Integer;
ColNum: Integer): Boolean; Forward;

Function IsBlockRowSide(
Excel : Variant;
RowNum: Integer;
ColNum: Integer): Boolean; Forward;


implementation


type
//Declare the constants used by Excel
SourceType = (xlConsolidation, xlDatabase, xlExternal, xlPivotTable);
Orientation = (xlHidden, xlRowField, xlColumnField, xlPageField, xlDataField);
RangeEnd = (NoValue, xlToLeft, xlToRight, xlUp, xlDown);
ExcelPasteType = (xlAllExceptBorders,xlNotes,xlFormats,xlValues,xlFormulas,xlAll);

{CAUTION!!! THESE OUTPUTS ARE ALL GARBLED! YOU SELECT xlDBF3 AND EXCEL
OUTPUTS A xlCSV.}
FileFormat = (xlAddIn, xlCSV, xlCSVMac, xlCSVMSDOS, xlCSVWindows, xlDBF2,
xlDBF3, xlDBF4, xlDIF, xlExcel2, xlExcel3, xlExcel4,
xlExcel4Workbook, xlIntlAddIn, xlIntlMacro, xlNormal,
xlSYLK, xlTemplate, xlText, xlTextMac, xlTextMSDOS,
xlTextWindows, xlTextPrinter, xlWK1, xlWK3, xlWKS,
xlWQ1, xlWK3FM3, xlWK1FMT, xlWK1ALL);

{Add a blank WorkSheet}
Function ExcelAddWorkSheet(Excel : Variant): Boolean;
Begin
Result := True;
Try
Excel.Worksheets.Add;
Except
MessageDlg('Unable to add a new worksheet', mtError, [mbOK], 0);
Result := False;
End;
End;

{Sets Excel Visibility}
Function ExcelSetVisible(Excel : Variant;IsVisible: Boolean): Boolean;
Begin
Result := True;
Try
Excel.Visible := IsVisible;
Except
MessageDlg('Unable to Excel Visibility', mtError, [mbOK], 0);
Result := False;
End;
End;

{Close Excel}
Function ExcelClose(Excel : Variant; SaveAll: Boolean): Boolean;
Begin
Result := True;
Try
ExcelCloseWorkBooks(Excel, SaveAll);
Excel.Quit;
Except
MessageDlg('Unable to Close Excel', mtError, [mbOK], 0);
Result := False;
End;
End;

{Close All Workbooks. All workbooks can be saved or not.}
Function ExcelCloseWorkBooks(Excel : Variant; SaveAll: Boolean): Boolean;
var
loop: byte;
Begin
Result := True;
Try
For loop := 1 to Excel.Workbooks.Count Do
Excel.Workbooks[1].Close[SaveAll];
Except
Result := False;
End;
End;

{Selects an Excel Sheet By Name}
Function ExcelSelectSheetByName(Excel : Variant; SheetName: String): Boolean;
Begin
Result := True;
Try
Excel.Sheets[SheetName].Select;
Except
Result := False;
End;
End;

{Selects a cell on the currently active sheet}
Function ExcelSelectCell(Excel : Variant; RowNum, ColNum: Integer): Boolean;
Begin
Result := True;
Try
Excel.ActiveSheet.Cells[RowNum, ColNum].Select;
Except
Result := False;
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;

{Returns the the current row}
Function ExcelGetRow(Excel : Variant): Integer;
Begin
Result := 1;
Try
Result := Excel.ActiveCell.Row;
Except
Result := 1;
End;
End;

{Returns the the current column}
Function ExcelGetCol(Excel : Variant): Integer;
Begin
Result := 1;
Try
Result := Excel.ActiveCell.Column;
Except
Result := 1;
End;
End;

{Moves the cursor to the last column}
Function ExcelGoToLastCol(Excel : Variant): Boolean;
Begin
Result := True;
Try
Excel.Selection.End[xlToRight].Select;
Except
Result := False;
End;
End;

{Moves the cursor to the last row}
Function ExcelGoToLastRow(Excel : Variant): Boolean;
Begin
Result := True;
Try
Excel.Selection.End[xlDown].Select;
Except
Result := False;
End;
End;

{Moves the cursor to the Top row}
Function ExcelGoToTopRow(Excel : Variant): Boolean;
Begin
Result := True;
Try
Excel.Selection.End[xlUp].Select;
Except
Result := False;
End;
End;

{Moves the cursor to the Leftmost Column}
Function ExcelGoToLeftmostCol(Excel : Variant): Boolean;
Begin
Result := True;
Try
Excel.Selection.End[xlToLeft].Select;
Except
Result := False;
End;
End;

{Moves the cursor to Home position}
Function ExcelHome(Excel : Variant): Boolean;
Begin
Result := True;
Try
Excel.ActiveSheet.Cells[1,1].Select;
Except
Result := False;
End;
End;

{Moves the cursor to the last row and column}
Function ExcelEnd(Excel : Variant): Boolean;
Begin
Result := True;
Try
Excel.Selection.End[xlDown].Select;
Excel.Selection.End[xlToRight].Select;
Except
Result := False;
End;
End;

{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;

{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 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;

{!~Copies a range of Excel Cells to a Delphi StringGrid. If successful
True is returned, False otherwise. If SizeStringGridToFit is True
then the StringGrid is resized to be exactly the correct dimensions to
receive the input Excel cells, otherwise the StringGrid is not resized.
If ClearStringGridFirst is true then any cells outside the input range
are cleared, otherwise existing values are retained. Please not that the
Excel cell coordinates are "1" based and the Delphi StringGrid coordinates
are zero based.}
Function ExcelCopyToStringGrid(
Excel : Variant;
ExcelFirstRow : Integer;
ExcelFirstCol : Integer;
ExcelLastRow : Integer;
ExcelLastCol : Integer;
StringGrid : TStringGrid;
StringGridFirstRow : Integer;
StringGridFirstCol : Integer;
SizeStringGridToFit : Boolean; {Make the StringGrid the same size as the input range}
ClearStringGridFirst : Boolean {cells outside input range in StringGrid are cleared}
): Boolean;
Var
C,R : Integer;
Begin
Result := False;
If ExcelLastCol &lt; ExcelFirstCol Then Exit;
If ExcelLastRow &lt; ExcelFirstRow Then Exit;
If (ExcelFirstRow &lt; 1) Or (ExcelFirstRow &gt; 255) Then Exit;
If (ExcelFirstCol &lt; 1) Or (ExcelFirstCol &gt; 30000) Then Exit;
If (ExcelLastRow &lt; 1) Or (ExcelLastRow &gt; 255) Then Exit;
If (ExcelLastCol &lt; 1) Or (ExcelLastCol &gt; 30000) Then Exit;
If StringGrid = nil Then Exit;
If SizeStringGridToFit Then
Begin
StringGrid.ColCount := ExcelLastCol - ExcelFirstCol + StringGridFirstCol + 1;
StringGrid.RowCount := ExcelLastRow - ExcelFirstRow + StringGridFirstRow + 1;
End;
If ClearStringGridFirst Then
Begin
C := StringGrid.ColCount;
R := StringGrid.RowCount;
StringGrid.ColCount := 1;
StringGrid.RowCount := 1;
StringGrid.Cells[0,0] := '';
StringGrid.ColCount := C;
StringGrid.RowCount := R;
End;

Result := True;
For R := ExcelFirstRow To ExcelLastRow Do
Begin
For C := ExcelFirstCol To ExcelLastCol Do
Begin
Try
StringGrid.Cells[
C - ExcelFirstCol + StringGridFirstCol,
R - ExcelFirstRow + StringGridFirstRow] :=
Excel.Cells[R, C];
Except
Result := False;
End;
End;
End;
End;

{!~Sets the formula in a cell. Remember to include the equals sign "=".
If the function fails False is returned, True otherwise.}
Function ExcelSetCellFormula(
Excel : Variant;
FormulaString : ShortString;
RowNum, ColNum: Integer): Boolean;
Begin
Result := True;
Try
Excel.
ActiveSheet.
Cells[RowNum, ColNum].
Formula := FormulaString;
Except
Result := False;
End;
End;

{!~Returns the Column String Value from its integer equilavent.}
Function ExcelColIntToStr(ColNum: Integer): ShortString;
Var
ColStr : ShortString;
Multiplier: Integer;
Remainder : Integer;
Begin
Result := '';
If ColNum &lt; 1 Then Exit;
If ColNum &gt; 256 Then Exit;
Multiplier := ColNum div 26;
Remainder := ColNum Mod 26;
If ColNum &lt;= 26 Then
Begin
ColStr[1] := ' ';
If Remainder = 0 Then
Begin
ColStr[2] := 'Z';
End
Else
Begin
ColStr[2] := Chr(Remainder+64);
End;
End
Else
Begin
If Remainder = 0 Then
Begin
If Multiplier = 1 Then
Begin
ColStr[1] := ' ';
ColStr[2] := 'Z';
End
Else
Begin
ColStr[1] := Chr(Multiplier+64-1);
ColStr[2] := 'Z';
End;
End
Else
Begin
ColStr[1] := Chr(Multiplier+64);
ColStr[2] := Chr(Remainder+64);
End;
End;
If ColStr[1] = ' ' Then
Begin
Result := Result + ColStr[2];
End
Else
Begin
Result := Result + ColStr[1] + ColStr[2];
End;
Result := Result;
End;

{!~Returns the Column Integer Value from its Alpha equilavent.}
Function ExcelColStrToInt(ColStr: ShortString): Integer;
Var
ColStrNew : ShortString;
i : Integer;
RetVal : Integer;
Multiplier : Integer;
Remainder : Integer;
Begin
RetVal := 1;
Result := RetVal;
ColStrNew := '';
For i := 1 To Length(ColStr) Do
Begin
If ((Ord(ColStr) &gt;= 65) And
( Ord(ColStr) &lt;= 90)) Or
((Ord(ColStr) &gt;= 97) And
( Ord(ColStr) &lt;= 122)) Then
Begin
ColStrNew := ColStrNew + UpperCase(ColStr);
End;
End;
If Length(ColStrNew) &lt; 1 Then Exit;
If Length(ColStrNew) &lt; 2 Then
Begin
RetVal := Ord(ColStrNew[1])-64;
End
Else
Begin
Multiplier := Ord(ColStrNew[1])-64;
Remainder := Ord(ColStrNew[2])-64;
Retval := (Multiplier * 26) + Remainder;
End;
Result := RetVal;
End;

{!~Sets the contents of a cell as a shortstring}
Function ExcelSetCellValue(
Excel : Variant;
RowNum, ColNum: Integer;
Value : ShortString): Boolean;
Begin
Result := False;
Try
Excel.Cells[RowNum, ColNum].Value := Value;
Result := True;
Except
Result := False;
End;
End;

{!~Open the file you want to work within Excel. If you want to
take advantage of optional parameters then you should use
ExcelOpenFileComplex}
Function ExcelOpenFile(Excel : Variant; FileName : String): Boolean;
Begin
Result := True;
try
//Open the database that we want to work with
Excel.Workbooks.Open[FileName];
except
MessageDlg('Unable to locate '+FileName, mtError, [mbOK], 0);
Result := False;
end;
End;

{!~Open the file you want to work within Excel.

Excel
The OLEObject passed as an argument.

FileName
Required. Specifies the filename of the workbook to open.

UpdateLinks
Specifies how links in the file are updated. If this
argument is omitted, the user is prompted to determine
how to update links. Otherwise, this argument is one of
the values shown in the following table.
Value Meaning
0 No updates
1 Updates external but not remote references
2 Updates remote but not external references
3 Updates both remote and external references

If Microsoft Excel is opening a file in the WKS, WK1, or
WK3 format and the updateLinks argument is 2, Microsoft
Excel generates charts from the graphs attached to the file.
If the argument is 0, no charts are created.

ReadOnly
If True, the workbook is opened in read-only mode.

Format
If Microsoft Excel is opening a text file, this argument
specifies the delimiter character, as shown in the following
table. If this argument is omitted, the current delimiter
is used.

Value Delimiter
1 Tabs
2 Commas
3 Spaces
4 Semicolons
5 Nothing
6 Custom character, see the delimiter argument.

Password
A string containing the password required to open a
protected workbook. If omitted and the workbook requires
a password, the user is prompted for the password.
}

Function ExcelOpenFileComplex(
Excel : Variant;
FileName : String;
UpdateLinks : Integer;
ReadOnly : Boolean;
Format : Integer;
Password : ShortString): Boolean;
Begin
Result := True;
try
//Open the database that we want to work with
Excel.
Workbooks.
Open[
FileName,
UpdateLinks,
ReadOnly,
Format,
Password];
except
MessageDlg('Unable to locate '+FileName, mtError, [mbOK], 0);
Result := False;
end;
End;

{!~Saves the range on the currently active sheet
to a DBase 4 table.}
Function ExcelSaveAsDBase4(
Excel : Variant;
ExcelFirstRow : Integer;
ExcelFirstCol : Integer;
ExcelLastRow : Integer;
ExcelLastCol : Integer;
OutFilePath : ShortString;
OutFileName : ShortString): Boolean;
{
OutFileFormat: Use one of the following
xlAddIn xlExcel3 xlTextMSDOS
xlCSV xlExcel4 xlTextWindows
xlCSVMac xlExcel4Workbook xlTextPrinter
xlCSVMSDOS xlIntlAddIn xlWK1
xlCSVWindows xlIntlMacro xlWK3
xlDBF2 xlNormal xlWKS
xlDBF3 xlSYLK xlWQ1
xlDBF4 xlTemplate xlWK3FM3
xlDIF xlText xlWK1FMT
xlExcel2 xlTextMac xlWK1ALL
}
Begin
Result := False;
Try
If IsTable(
OutFilePath,
OutFileName+'.dbf')
Then
Begin
If Not DBDeleteTable(
OutFilePath,
OutFileName+'.dbf')
Then
Begin
Msg('Could not delete the '+
OutFilePath+OutFileName+'.dbf'+' Table');
Msg('Process Aborted');
Exit;
End;
End;
If ExcelVersion(Excel) = '8.0' Then
Begin
ExcelSelectCell(Excel,ExcelFirstRow,ExcelFirstCol);
ExcelSelectBlockWhole(Excel);
//Excel.SendKeys('^+{END}');
End
Else
Begin
Excel.
Range(
ExcelColIntToStr(ExcelFirstCol)+
IntToStr(ExcelFirstRow)+
':'+
ExcelColIntToStr(ExcelLastCol)+
IntToStr(ExcelLastRow)
).
Select;
End;
{
FileFormat = (xlAddIn, xlCSV, xlCSVMac, xlCSVMSDOS, xlCSVWindows, xlDBF2,
xlDBF3, xlDBF4, xlDIF, xlExcel2, xlExcel3, xlExcel4,
xlExcel4Workbook, xlIntlAddIn, xlIntlMacro, xlNormal,
xlSYLK, xlTemplate, xlText, xlTextMac, xlTextMSDOS,
xlTextWindows, xlTextPrinter, xlWK1, xlWK3, xlWKS,
xlWQ1, xlWK3FM3, xlWK1FMT, xlWK1ALL);
}
{
//CHECKING OUT THE GARBLED OUTPUT
// Produces an *.xls
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'02',xlCSV);

// Produces an *.txt
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'04',xlCSVMSDOS);

// Produces nothing
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'05',xlCSVWindows);

// Produces nothing
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'06',xlDBF2);

// Produces an *.txt
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'07',xlDBF3);

// Produces an *.dbf
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'08',xlDBF4);
// Produces an *.dbf
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'09',xlDIF);
// Produces an *.dif
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'10',xlExcel2);
// Produces an *.slk
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'11',xlExcel3);
// Produces an *.dbf
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'12',xlExcel4);
}
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName,xlExcel4);
Result := True;
Except
Result := False;
End;
End;

{!~Saves the range on the currently active sheet
to a text file.}
Function ExcelSaveAsText(
Excel : Variant;
ExcelFirstRow : Integer;
ExcelFirstCol : Integer;
ExcelLastRow : Integer;
ExcelLastCol : Integer;
OutFilePath : ShortString;
OutFileName : ShortString): Boolean;
{
OutFileFormat: Use one of the following
xlAddIn xlExcel3 xlTextMSDOS
xlCSV xlExcel4 xlTextWindows
xlCSVMac xlExcel4Workbook xlTextPrinter
xlCSVMSDOS xlIntlAddIn xlWK1
xlCSVWindows xlIntlMacro xlWK3
xlDBF2 xlNormal xlWKS
xlDBF3 xlSYLK xlWQ1
xlDBF4 xlTemplate xlWK3FM3
xlDIF xlText xlWK1FMT
xlExcel2 xlTextMac xlWK1ALL
}
Var
FullOutName : String;
Begin
Result := False;
Try
If OutFilePath &lt;&gt; '' Then
Begin
If Not (Copy(OutFilePath,Length(OutFilePath),1) = '/') Then
Begin
OutFilePath := OutFilePath + '/';
End;
End;
FullOutName := OutFilePath + OutFileName;
If FileExists(FullOutName) Then DeleteFile(FullOutName);

If ExcelVersion(Excel) = '8.0' Then
Begin
ExcelSelectCell(Excel,ExcelFirstRow,ExcelFirstCol);
ExcelSelectBlockWhole(Excel);
//Excel.SendKeys('^+{END}');
End
Else
Begin
Excel.
Range(
ExcelColIntToStr(ExcelFirstCol)+
IntToStr(ExcelFirstRow)+
':'+
ExcelColIntToStr(ExcelLastCol)+
IntToStr(ExcelLastRow)
).
Select;
End;
{
FileFormat = (xlAddIn, xlCSV, xlCSVMac, xlCSVMSDOS, xlCSVWindows, xlDBF2,
xlDBF3, xlDBF4, xlDIF, xlExcel2, xlExcel3, xlExcel4,
xlExcel4Workbook, xlIntlAddIn, xlIntlMacro, xlNormal,
xlSYLK, xlTemplate, xlText, xlTextMac, xlTextMSDOS,
xlTextWindows, xlTextPrinter, xlWK1, xlWK3, xlWKS,
xlWQ1, xlWK3FM3, xlWK1FMT, xlWK1ALL);
}
(*
//CHECKING OUT THE GARBLED OUTPUT
// Produces an *.xls
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'02',xlCSV);
*)
// Produces an *.txt
// Excel.
// ActiveSheet.
// SaveAs(
// FullOutName,xlCSVMSDOS);
(*
// Produces nothing
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'05',xlCSVWindows);

// Produces nothing
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'06',xlDBF2);

// Produces an *.txt comma separated
Excel.
ActiveSheet.
SaveAs(
FullOutName,xlDBF3);
*)
// Produces an *.txt
Excel.
ActiveSheet.
SaveAs(
FullOutName,xlTextMSDOS);
(*
// Produces an *.dbf
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'08',xlDBF4);
// Produces an *.dbf
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'09',xlDIF);
// Produces an *.dif
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'10',xlExcel2);
// Produces an *.slk
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'11',xlExcel3);
// Produces an *.dbf
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'12',xlExcel4);

*)
Result := True;
Except
Result := False;
End;
End;



{!~Saves the range on the currently active sheet
to to values only.}
Function ExcelPasteValuesOnly(
Excel : Variant;
ExcelFirstRow : Integer;
ExcelFirstCol : Integer;
ExcelLastRow : Integer;
ExcelLastCol : Integer): Boolean;
Var
RangeString : ShortString;
SheetName : ShortString;
SheetTemp : ShortString;
Begin
Result := True;
try
If ExcelVersion(Excel) = '8.0' Then
Begin
If Not ExcelSelectRange(
Excel,
ExcelFirstRow,
ExcelFirstCol,
ExcelLastRow,
ExcelLastCol)
Then
Begin
Result := False;
Msg('Unable to select the range to paste as values.');
Exit;
End;
Excel.Selection.Copy;
Excel.Selection.PasteSpecial(xlValues);
Excel.Application.CutCopyMode := False;
End
Else
Begin
Excel.Range(
ExcelColIntToStr(ExcelFirstCol)+IntToStr(ExcelFirstRow)+
':'+
ExcelColIntToStr(ExcelLastCol)+IntToStr(ExcelLastRow)).Select;
Excel.Selection.Copy;
Excel.Selection.PasteSpecial(xlValues);
Excel.Application.CutCopyMode := False;
Excel.Selection.Replace('#N/A','0');
End;
except
Msg('Unable to paste range as values');
Result := False;
end;
End;

{!~Sets a Column Width on the currently active sheet}
Function ExcelSetColumnWidth(Excel : Variant; ColNum, ColumnWidth: Integer): Boolean;
Var
RowWas : Integer;
ColWas : Integer;
Begin
Result := False;
Try
RowWas := ExcelGetRow(Excel);
ColWas := ExcelGetCol(Excel);
ExcelSelectCell(Excel,1,ColNum);
Excel.Selection.ColumnWidth := ColumnWidth;
ExcelSelectCell(Excel,RowWas,ColWas);
Result := True;
Except
Result := False;
End;
End;

{!~Selects a range on the currently active sheet}
Function ExcelSelectRange(
Excel : Variant;
FirstRow : Integer;
FirstCol : Integer;
LastRow : Integer;
LastCol : Integer): Boolean;
Var
r,c : Integer;
RowString : ShortString;
ColString : ShortString;
Begin
Result := False;
Try
If FirstRow &lt; 1 Then Exit;
If FirstCol &lt; 1 Then Exit;
If LastRow &lt; 1 Then Exit;
If LastCol &lt; 1 Then Exit;
If FirstCol &gt; 255 Then Exit;
If LastCol &gt; 255 Then Exit;

If Not ExcelSelectCell(
Excel,
FirstRow,
FirstCol)
Then
Begin
Exit;
End;
{Check for strange number combinations}
If FirstRow = LastRow Then
Begin
{Don't need to do anything}
End
Else
Begin
If FirstRow &lt; LastRow Then
Begin
For r := FirstRow To LastRow - 1 Do
Begin
Excel.SendKeys('+{DOWN}');
End;
End
Else
Begin
For r := LastRow To FirstRow - 1 Do
Begin
Excel.SendKeys('+{UP}');
End;
End;
End;
If FirstCol = LastCol Then
Begin
{Don't need to do anything}
End
Else
Begin
If FirstCol &lt; LastCol Then
Begin
For c := FirstCol To LastCol - 1 Do
Begin
Excel.SendKeys('+{RIGHT}');
End;
End
Else
Begin
For c := LastCol To FirstCol - 1 Do
Begin
Excel.SendKeys('+{LEFT}');
End;
End;
End;
Result := True;
Except
Result := False;
End;
End;

{!~Selects a range on the currently active sheet. From the
current cursor position a block is selected down and to the right.
The block proceeds down until an empty row is encountered. The
block proceeds right until an empty column is encountered.}
Function ExcelSelectBlock(
Excel : Variant;
FirstRow : Integer;
FirstCol : Integer): Boolean;
Begin
Result := False;
Try
ExcelSelectCell(Excel,FirstRow,FirstCol);
Excel.SendKeys('+{END}+{RIGHT}');
Excel.SendKeys('+{END}+{DOWN}');
Result := True;
Except
Result := False;
End;
End;

{!~Selects a range on the currently active sheet. From the
current cursor position a block is selected that contains
the currently active cell. The block proceeds in each
direction until an empty row or column is encountered.}
Function ExcelSelectBlockWhole(Excel: Variant): Boolean;
Var
FirstRow : Integer;
FirstCol : Integer;
LastRow : Integer;
LastCol : Integer;
RowWas : Integer;
ColWas : Integer;
Begin
Result := False;
Try
RowWas := ExcelGetRow(Excel);
ColWas := ExcelGetCol(Excel);

{If the base cell is on a side of the block, the block
will not be created properly.}

{View From Original Cell}
FirstRow := ExcelFirstRow(Excel);
FirstCol := ExcelFirstCol(Excel);
LastRow := ExcelLastRow(Excel);
LastCol := ExcelLastCol(Excel);
If (Not IsBlockColSide(Excel,RowWas,ColWas)) And
(Not IsBlockRowSide(Excel,RowWas,ColWas)) Then
Begin
{Cell is not on a side of the block}
ExcelSelectCell(Excel,FirstRow,FirstCol);
Excel.SendKeys('+{END}+{RIGHT}');
Excel.SendKeys('+{END}+{DOWN}');
Result := True;
Exit;
End;
{Row Only problem}
If (Not IsBlockColSide(Excel,RowWas,ColWas)) And
(IsBlockRowSide(Excel,RowWas,ColWas)) Then
Begin
{DEFAULT TO ASSUMING SELECTED CELLS ARE NEAR TOP LEFT AND
BLOCK IS TOWARD BOTTOM RIGHT}
ExcelSelectCell(Excel,RowWas,FirstCol);
Excel.SendKeys('+{END}+{RIGHT}');
Excel.SendKeys('+{END}+{DOWN}');
Result := True;
Exit;
End;
{Column Only problem}
If (IsBlockColSide(Excel,RowWas,ColWas)) And
(Not IsBlockRowSide(Excel,RowWas,ColWas)) Then
Begin
{DEFAULT TO ASSUMING SELECTED CELLS ARE NEAR TOP LEFT AND
BLOCK IS TOWARD BOTTOM RIGHT}
ExcelSelectCell(Excel,FirstRow,ColWas);
Excel.SendKeys('+{END}+{RIGHT}');
Excel.SendKeys('+{END}+{DOWN}');
Result := True;
Exit;
End;
{DEFAULT TO ASSUMING SELECTED CELLS ARE NEAR TOP LEFT AND
BLOCK IS TOWARD BOTTOM RIGHT}
ExcelSelectCell(Excel,RowWas,ColWas);
Excel.SendKeys('+{END}+{RIGHT}');
Excel.SendKeys('+{END}+{DOWN}');
Result := True;
Except
Result := False;
End;
End;

Function IsBlockColSide(Excel : Variant; RowNum, ColNum: Integer): Boolean;
Var
RowWas : Integer;
ColWas : Integer;
CellFirstSide : Integer;
CellLastSide : Integer;
FirstSideLastSide : Integer;
LastSideFirstSide : Integer;
Begin
ExcelSelectCell(Excel,RowNum,ColNum);
CellFirstSide := ExcelFirstCol(Excel);
CellLastSide := ExcelLastCol(Excel);
ExcelSelectCell(Excel,RowNum,CellFirstSide);
FirstSideLastSide := ExcelLastCol(Excel);
ExcelSelectCell(Excel,RowNum,CellLastSide);
LastSideFirstSide := ExcelFirstCol(Excel);
ExcelSelectCell(Excel,RowNum,ColNum);
If (LastSideFirstSide = ColNum) Or
(FirstSideLastSide = ColNum) Then
Begin
Result := True;
End
Else
Begin
Result := False;
End;
End;

Function IsBlockRowSide(Excel : Variant; RowNum, ColNum: Integer): Boolean;
Var
RowWas : Integer;
ColWas : Integer;
CellFirstSide : Integer;
CellLastSide : Integer;
FirstSideLastSide : Integer;
LastSideFirstSide : Integer;
Begin
ExcelSelectCell(Excel,RowNum,ColNum);
CellFirstSide := ExcelFirstRow(Excel);
CellLastSide := ExcelLastRow(Excel);
ExcelSelectCell(Excel,CellFirstSide,ColNum);
FirstSideLastSide := ExcelLastRow(Excel);
ExcelSelectCell(Excel,CellLastSide,ColNum);
LastSideFirstSide := ExcelFirstRow(Excel);
ExcelSelectCell(Excel,RowNum,ColNum);
If (LastSideFirstSide = RowNum) Or
(FirstSideLastSide = RowNum) Then
Begin
Result := True;
End
Else
Begin
Result := False;
End;
End;

{!~Renames a worksheet.}
Function ExcelRenameSheet(
Excel : Variant;
OldName : ShortString;
NewName : ShortString): Boolean;
Begin
Result := False;
Try
Excel.Sheets(OldName).Name := NewName;
Result := True;
Except
Result := False;
End;
End;

{!~Delete a WorkSheet by Name}
Function ExcelDeleteWorkSheet(
Excel : Variant;
SheetName : ShortString): Boolean;
Begin
Result := False;
Try
If Not ExcelSelectSheetByName(Excel,SheetName) Then
Begin
Msg('Could not select the '+SheetName+' WorkSheet');
Exit;
End;
Excel.ActiveWindow.SelectedSheets.Delete;
Result := True;
Finally
Result := False;
End;
End;

{!~Returns the name of the currently active worksheet
as a shortstring}
Function ExcelGetActiveSheetName(Excel : Variant): ShortString;
Begin
Result := '';
Try
Result := Excel.ActiveSheet.Name;
Except
Result := '';
End;
End;

{!~Saves the range on the currently active sheet
to values only.}
Function ExcelValuesOnly(
Excel : Variant;
ExcelFirstRow : Integer;
ExcelFirstCol : Integer;
ExcelLastRow : Integer;
ExcelLastCol : Integer): Boolean;
Var
r,c : Integer;
s : ShortString;
Begin
Result := False;
Try
If ExcelVersion(Excel) = '8.0' Then
Begin
For r := ExcelFirstRow To ExcelLastRow Do
Begin
For c := ExcelFirstCol To ExcelLastCol Do
Begin
s := Excel.Cells[r,c].Value;
Excel.Cells[r, c].Value := s;
End;
End;
End
Else
Begin
ExcelPasteValuesOnly(
Excel,
ExcelFirstRow,
ExcelFirstCol,
ExcelLastRow,
ExcelLastCol);
End;
Result := True;;
Except
Result := False;
End;
End;

{!~Gets the formula in a cell.}
Function ExcelGetCellFormula(
Excel : Variant;
RowNum, ColNum: Integer): ShortString;
Begin
Result := ' ';
Try
Result := Excel.
ActiveSheet.
Cells[RowNum, ColNum].
Formula;
Except
Result := ' ';
End;
End;

{!~Returns the Excel Version as a ShortString.}
Function ExcelVersion(Excel: Variant): ShortString;
Var
Version : ShortString;
Begin
Result := '';
Try
Version := Excel.Version;
Result := Version;
Except
Result := '';
End;
End;

Initialization
DelphiChecker(
RunOutsideIDE_ads,
'Advanced Delphi Systems Code',
RunOutsideIDECompany_ads,
RunOutsideIDEPhone_ads,
RunOutsideIDEDate_ads);
End.

Comment
From: Radler Date: 11/24/2000 02:30PM PST
listening
Comment
From: xiemingsuk Date: 11/24/2000 04:50PM PST
Thank you , Alvidor
Comment
From: Helge_Lorenzen Date: 11/27/2000 10:10AM PST
have a look on this solution

hae a unit for Exce:

{----------------------------------------------------------------------}
{ Excel unit : By Motaz Abdel Azeem }
{ Created : Saturday, 1, April, 2000 }
{ Updated : Saturday, 1, April, 2000 }
{----------------------------------------------------------------------}

unit Excel;

interface

function ExcelCreate(SheetName: string; Show: Boolean = True): Variant;
procedure ExcelPut(ExcelObject: Variant; SheetName: string;
Row, Col: Integer; Data: string);
procedure ExcelClose(ExcelObject: Variant);

implementation

uses
ComObj;

{-------------------------------------------------------------------------}
{ ExcelCreate: Open Excel application }
{-------------------------------------------------------------------------}

function ExcelCreate(SheetName: string; Show: Boolean = True): Variant;
const
xlWBatWorkSheet = -4167;
var
WorkBook: variant;
V: Variant;
begin
V:= CreateOleObject('Excel.Application');
WorkBook := V.Workbooks.Add(xlWBatWorkSheet);
WorkBook.WorkSheets[1].Name := SheetName;
V.Visible := Show;
Result:= V;
end;

{-------------------------------------------------------------------------}
{ ExcelPut: Put data into cells }
{-------------------------------------------------------------------------}

procedure ExcelPut(ExcelObject: Variant; SheetName: string;
Row, Col: Integer; Data: string);
var
Sheet: variant;
begin
Sheet:= ExcelObject.WorkBooks[1].WorkSheets[SheetName];
Sheet.Cells[Row, Col].Value := Data;
end;

{-------------------------------------------------------------------------}
{ ExcelClose: Close opened application }
{-------------------------------------------------------------------------}

procedure ExcelClose(ExcelObject: Variant);
begin
ExcelObject.Quit;
end;

end.


To use it:

var
Obj: OleVariant;
begin
Obj:= ExcelCreat('Test', True);
ExcelPut(Obj, 'Test', 1, 2, 'Cell 1, 2');
end;

Motaz


Comment

From: vanbeek
Date: Monday, November 06 2000 - 11:37AM PST


The following site helped me a lot to get going on Excel automation.
http://www.djpate.freeserve.co.uk/Automation.htm

Kai


Comment

From: quique
Date: Wednesday, November 15 2000 - 11:40PM
PST

Just what I was looking for! Simple and works!

Thanks very much,

QUIQUE.


Comment

From: Motaz
Date: Wednesday, November 15 2000 - 11:54PM
PST

By the way, I modify that unit to support Font, Color, Size, and Style for each
cell:

--------------------------------
{----------------------------------------------------------------------}
{ Excel unit : By Motaz Abdel Azeem }
{ Created : Saturday, 1, April, 2000 }
{ Updated : Sunday, 12, November, 2000 }
{----------------------------------------------------------------------}

unit Excel;

interface

uses Graphics;

function ExcelCreate(SheetName: string; Show: Boolean = True): Variant;
procedure ExcelPut(ExcelObject: Variant; SheetName: string;
Row, Col: Integer; Data: string; FontName: string = '';
FontSize: Integer = 0; Color: Integer = clBlack; FontStyles: TFontStyles = []);

procedure ExcelClose(ExcelObject: Variant);

implementation

uses
ComObj;

{-------------------------------------------------------------------------}
{ ExcelCreate: Open Excel application }
{-------------------------------------------------------------------------}

function ExcelCreate(SheetName: string; Show: Boolean = True): Variant;
const
xlWBatWorkSheet = -4167;
var
WorkBook: variant;
V: Variant;
begin
V:= CreateOleObject('Excel.Application');
WorkBook := V.Workbooks.Add(xlWBatWorkSheet);
WorkBook.WorkSheets[1].Name := SheetName;
V.Visible := Show;
Result:= V;
end;

{-------------------------------------------------------------------------}
{ ExcelPut: Put data into cells }
{-------------------------------------------------------------------------}

procedure ExcelPut(ExcelObject: Variant; SheetName: string;
Row, Col: Integer; Data: string; FontName: string = '';
FontSize: Integer = 0; Color: Integer = clBlack; FontStyles: TFontStyles = []);

var
Sheet: variant;
begin
Sheet:= ExcelObject.WorkBooks[1].WorkSheets[SheetName];
Sheet.Cells[Row, Col].Value:= Data;
Sheet.Cells[Row, Col].Font.Color:= Color;
if FontName &lt;&gt; '' then
Sheet.Cells[Row, Col].Font.Name:= FontName;
if FontSize &lt;&gt; 0 then
Sheet.Cells[Row, Col].Font.Size:= FontSize;
if FontSize &lt;&gt; 0 then
Sheet.Cells[Row, Col].Font.Size:= FontSize;
Sheet.Cells[Row, Col].Font.Bold:= fsBold in FontStyles;
Sheet.Cells[Row, Col].Font.Underline:= fsUnderline in FontStyles;
Sheet.Cells[Row, Col].Font.Italic:= fsItalic in FontStyles;
end;

{-------------------------------------------------------------------------}
{ ExcelClose: Close opened application }
{-------------------------------------------------------------------------}

procedure ExcelClose(ExcelObject: Variant);
begin
ExcelObject.Quit;
end;

end.
------------------------------------

The modified procedure is: ExcelPut:

ExcelPut(Obj, 1, 2, 'Hello', 'Tahoma');
or
ExcelPut(Obj, 1, 2, 'Hello', 'Tahoma', 30);
or
ExcelPut(Obj, 1, 2, 'Hello', '', 0, clBlue);
or
ExcelPut(Obj, 1, 2, 'Hello', '', 0, 0, [fsBold]);
or even can be called as the old style:
ExcelPut(Obj, 1, 2, 'Hello');

Motaz
Here are your options, chengjian
1.comment or answer for xiemingsuk

(Click here for tips on "Comments and Answers")

Comment
Answer



Email Notification is automatically enabled whenever you post a comment or answer.
To turn off Email notification, see below.


Additional Member Options
Click the button below if you'd like an email notification whenever this question is updated.

Tips on Comments and Answers
Comments
Comments are intended to be used as a collaboration tool. Many Experts choose to post their solutions as comments only.

Answers
An answer is a specific solution to a question and should be submitted if it will solve the questioner's problem and doesn't duplicate a previous comment.

Comment Vs. Answer
If you are unsure of your solution, post it as a comment. Members can accept comments as solutions and award you Expert Points for them.

For more tips on comments and answers, click here.




About Us | The EE Network | Advertise | Member Agreement | Privacy Policy | All Topics | Home
Legal Notice - &amp;copy; 1996-2001 Experts Exchange, Inc. - Patented
 
to CJ:
  我真的服了你!!!
  你以为很多人有足够的网络速度看完这些东西吗?

&lt;!-- #BeginEditable "Page%20Body" --&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H2&gt;&lt;A NAME="Top"&gt;&lt;/A&gt;Automating Microsoft Excel&lt;/H2&gt;
&lt;H3&gt;Contents&lt;/H3&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A HREF="#ExcelSourceInfo"&gt;Sources of information&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#Downloads"&gt;Downloads&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#HowDoI"&gt;How do I?&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="ExcelPrbs.htm"&gt;Common problems&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;/DIV&gt;
&lt;HR&gt;
&lt;H3&gt;&lt;A NAME="ExcelSourceInfo"&gt;&lt;/A&gt;Sources of information&lt;/H3&gt;
&lt;TABLE WIDTH="80%" BORDER="1" ALIGN="center" SUMMARY="Useful links for Excel"&gt;
&lt;TR BORDERCOLOR="#FFFF99"&gt;
&lt;TD ALIGN="left" VALIGN="top" WIDTH="22%"&gt;Web sites&lt;/TD&gt;
&lt;TD WIDTH="78%" VALIGN="top"&gt; &lt;P&gt;Delphi sites&lt;BR&gt;
&lt;A HREF="http://www.borland.com/delphi/papers/microexcel/"&gt;Borland's papers&lt;/A&gt;
&lt;BR&gt;
&lt;A
HREF="http://community.borland.com/delphi/article/1,1410,10043,00.html"&gt;Chapter
17 of C. Calvert's D4 Unleashed&lt;/A&gt;&lt;BR&gt;
&lt;A HREF="http://vzone.virgin.net/graham.marshall/excel.htm"&gt;Graham Marshall's
Delphi 3 and Excel&lt;/A&gt; &lt;BR&gt;
&lt;/P&gt;
&lt;P&gt;For catching Excel events, or general COM concepts, see also&lt;BR&gt;
&lt;A HREF="http://www.techvanguards.com/"&gt;Binh Ly's tutorials&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;Non-Delphi sites&lt;BR&gt;
&lt;A HREF="http://www.microsoft.com/officedev/articles/Opg/intro/intro.htm"&gt;MS
Visual Basic Programmer's Guide&lt;/A&gt;&lt;BR&gt;
&lt;A HREF="http://msdn.microsoft.com/default.asp"&gt; Microsoft Developer's
Network&lt;/A&gt;&lt;BR&gt;
&lt;A HREF="http://www.BMSLtd.co.uk"&gt;Stephen Bullen's web site&lt;/A&gt; &lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR BORDERCOLOR="#FFFF99"&gt;
&lt;TD ALIGN="left" VALIGN="top" WIDTH="22%"&gt;Books&lt;/TD&gt;
&lt;TD WIDTH="78%" VALIGN="top"&gt;Charlie Calvert's Delphi 4 Unleashed&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
&lt;BR&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#Top"&gt;&lt;FONT SIZE="2"&gt;Back to top&lt;/FONT&gt;&lt;/A&gt; &lt;HR&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H3&gt;&lt;A NAME="HowDoI"&gt;&lt;/A&gt;How do I ... ?&lt;/H3&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A HREF="#StartingExcel"&gt;Start Excel&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#CloseExcel"&gt; Close Excel&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#CreateWorkbook"&gt;Create a workbook&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#OpenWorkbook"&gt;Open a workbook&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#CloseWorkbook"&gt;Close a workbook&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#EnterData"&gt;Enter data&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#CopyData"&gt;Copy data&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#FormatRange"&gt;Format a range&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#AddName"&gt;Add a name to a workbook&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#AddMacro"&gt;Add a macro to a workbook&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#Top"&gt;&lt;FONT SIZE="2"&gt;Back to top&lt;/FONT&gt;&lt;/A&gt; &lt;HR&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H4&gt;&lt;A NAME="StartingExcel"&gt;&lt;/A&gt;&lt;FONT
COLOR="#FF6666"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;How to start Excel&lt;FONT
COLOR="#FF6666"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A HREF="#StartingComponents"&gt;Using D5's components&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#ExcelStartingNoExceptions"&gt;Using the type library( early binding)
&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A HREF="#ExcelStartingLateBinding"&gt;Without using the type library (late
binding)&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The last two methods check to see if Excel is already running before
starting a new instance. &lt;/P&gt;
&lt;HR WIDTH="50%" ALIGN="center"&gt;
&lt;P&gt;&lt;I&gt;&lt;A NAME="StartingComponents"&gt;&lt;/A&gt;&lt;B&gt;Using Delphi 5's Excel
components&lt;/B&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;The new Delphi 5 components make starting Excel very simple. Drop an
ExcelApplication component on your form. If the AutoConnect property is true,
Excel will start automatically when your program starts; if it's false, just
call&lt;/P&gt;
&lt;PRE&gt; ExcelApplication1.Connect;
&lt;/PRE&gt;

&lt;P&gt;when you want to start Excel. To use a running instance of Excel, if there
is one, set the ConnectKind property of TExcelApplication to&lt;B&gt;
ckRunningOrNew&lt;/B&gt;, or to &lt;B&gt;ckRunningInstance&lt;/B&gt; if you don't want to start a
new instance if Excel isn't running.&lt;/P&gt;
&lt;P&gt; Once Excel has started, you can connect other components, such as
TExcelWorkbook, using their ConnectTo methods:&lt;/P&gt;
&lt;PRE&gt; ExcelWorkbook1.ConnectTo(ExcelApplication1.ActiveWorkbook);
ExcelWorksheet1.ConnectTo(ExcelApplication1.ActiveSheet as _Worksheet);
ExcelWorksheet2.ConnectTo(ExcelApplication1.Worksheets.Item['Sheet2'] as _Worksheet);
&lt;/PRE&gt;
Note that a workbook or worksheet must be open before you can connect to it!
See &lt;A HREF="#OpenWorkbook"&gt;How to open a workbook&lt;/A&gt; or
&lt;A HREF="#CreateWorkbook"&gt;How to create a workbook&lt;/A&gt; for code to do this.
&lt;P&gt;I advise you not to try to start Excel using any component except the
application component, however. At least on some setups, calling the Connect
method (NB not the ConnectTo method!) of a Workbook or Worksheet component will
cause an exception. &lt;/P&gt;
&lt;HR WIDTH="50%" ALIGN="center"&gt;
&lt;P&gt;&lt;I&gt;&lt;A NAME="ExcelStartingNoExceptions"&gt;&lt;/A&gt;&lt;B&gt;Opening Excel (early
binding)&lt;/B&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;Before you can use this method, you must have imported the type library
(Excel8.olb for Excel 97). &lt;/P&gt;
&lt;P&gt;One way of starting Excel is to &lt;B&gt;try &lt;/B&gt;the GetActiveObject call, to get
a running instance of Excel, but put a call to CoApplication.Create in an
&lt;B&gt;except&lt;/B&gt; clause. But &lt;B&gt;except&lt;/B&gt; clauses are slow, and can cause
problems within the IDE for people who like Break On Exceptions set to True.
The following code removes the need for a &lt;B&gt;try...except&lt;/B&gt; clause, by
avoiding using OleCheck on GetActiveObject in the case when Excel is not
running.&lt;/P&gt;
&lt;PRE&gt; uses Windows, ComObj, ActiveX, Excel_TLB;
&lt;/PRE&gt;

&lt;PRE&gt;
&lt;B&gt;var&lt;/B&gt;
Excel: _Application;
AppWasRunning: boolean; // &lt;I&gt;tells you if you can close Excel when you've finished&lt;/I&gt;
lcid: integer;
Unknown: IUnknown;
Result: HResult;
&lt;B&gt;begin&lt;/B&gt;
lcid := GetUserDefaultLCID;
AppWasRunning := False;

&lt;I&gt; {$IFDEF VER120} // Delphi 4&lt;/I&gt;
Result := GetActiveObject(CLASS_Application_, nil, Unknown);
&lt;B&gt;if&lt;/B&gt; (Result = MK_E_UNAVAILABLE) &lt;B&gt;then&lt;/B&gt;
Excel := CoApplication_.Create

&lt;I&gt; {$ELSE} // Delphi 5&lt;/I&gt;
Result := GetActiveObject(CLASS_ExcelApplication, nil, Unknown);
&lt;B&gt;if&lt;/B&gt; (Result = MK_E_UNAVAILABLE) &lt;B&gt;then&lt;/B&gt;
Excel := CoExcelApplication.Create
&lt;I&gt; {$ENDIF} &lt;/I&gt;

&lt;B&gt;else begin&lt;/B&gt;
&lt;I&gt;{ make sure no other error occurred during GetActiveObject }&lt;/I&gt;
OleCheck(Result);
OleCheck(Unknown.QueryInterface(_Application, Excel));
AppWasRunning := True;
&lt;B&gt;end&lt;/B&gt;;
Excel.Visible[lcid] := True;
... &lt;/PRE&gt;

&lt;P&gt;There is one problem that you should be aware of, however: starting Excel
with GetActiveObject may result in Excel's main frame showing, but its client
area remaining hidden. Although you can restore the client area by setting
Excel to full screen view and back again, this is obviously unattractive
behaviour. It seems to happen only when Excel is running invisibly at the time
of the GetActiveObject call.&lt;/P&gt;
&lt;P&gt;Excel may be running invisibly, even after you think you've freed it, if any
of your Workbook or Worksheet variables are still 'live'. (Check this by
pressing Ctrl-Alt-Del once and looking at the list of running tasks.) If you
make sure that you free &lt;I&gt;all &lt;/I&gt;Excel variables when you close the
application, Excel will close down properly. Then starting the application
again will not normally cause problems, at least if users of your software
won't be running Excel invisibly by any other method.&lt;/P&gt;
&lt;P&gt;If your users may be using other software that automates Excel invisibly,
however, you may prefer to avoid all calls to GetActiveObject (or
GetActiveOleObject), and simply call CoApplication.Create.&lt;/P&gt;
&lt;HR WIDTH="50%" ALIGN="center"&gt;
&lt;P&gt;&lt;I&gt;&lt;A NAME="ExcelStartingLateBinding"&gt;&lt;/A&gt;&lt;B&gt;Without using the type
library&lt;/B&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;Automation is &lt;I&gt;so&lt;/I&gt; much easier and faster using type libraries (early
binding) that you should avoid managing without if at all possible. But if you
really can't, here's how to get started:&lt;/P&gt;
&lt;PRE&gt; var
Excel: Variant;
begin
try
Excel := GetActiveOleObject('Excel.Application');
except
Excel := CreateOleObject('Excel.Application');
end;
Excel.Visible := True;
&lt;/PRE&gt;

&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#HowDoI"&gt;&lt;FONT SIZE="2"&gt;Back to 'HowDoI'&lt;/FONT&gt;&lt;/A&gt; &lt;HR&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H4&gt;&lt;A NAME="CloseExcel"&gt;&lt;/A&gt;&lt;FONT
COLOR="#FF6666"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;How to close Excel&lt;FONT
COLOR="#FF6666"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;P&gt; Assuming an application variable, Excel, and an integer variable LCID that
you've assigned the value GetUserDefaultLCID:&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Early binding::&lt;/I&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;I&gt; { Uncomment the next line if you want Excel to quit without asking
&lt;/I&gt;&lt;I&gt; whether to save the worksheet }
&lt;/I&gt; // Excel.DisplayAlerts[LCID] := False;
Excel.Quit;
&lt;/PRE&gt;
If you're using the D5 server component, you should disconnect it:
&lt;PRE&gt;
Excel.Disconnect;
&lt;/PRE&gt;
If you're using an _Application interface variable, you should set it to nil
instead:
&lt;PRE&gt;
Excel := nil;
&lt;/PRE&gt;

&lt;P&gt;&lt;I&gt;Late binding:&lt;/I&gt; &lt;/P&gt;
&lt;PRE&gt;
&lt;I&gt; { Uncomment the next line if you want Excel to quit without asking
&lt;/I&gt;&lt;I&gt; whether to save the worksheet }
&lt;/I&gt; // Excel.DisplayAlerts := False;
Excel.Quit;
Excel := Unassigned;
&lt;/PRE&gt;
Note, however, that Excel will hang around in memory, running invisibly, unless
you've released &lt;I&gt;all&lt;/I&gt; your workbook and worksheet variables. Disconnect
any components, set any interface variables to nil, and set any variants to
Unassigned to prevent this.
&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#HowDoI"&gt;&lt;FONT SIZE="2"&gt;Back to 'HowDoI'&lt;/FONT&gt;&lt;/A&gt; &lt;HR&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H4&gt;&lt;A NAME="CreateWorkbook"&gt;&lt;/A&gt;&lt;FONT
COLOR="#FF6666"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;How to create a workbook&lt;FONT
COLOR="#FF6666"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;P&gt; Assuming an application variable, Excel, and an integer variable LCID that
you've assigned the value GetUserDefaultLCID:&lt;/P&gt;
&lt;I&gt;Early binding:&lt;/I&gt; &lt;PRE&gt; var
WBk: _Workbook;
...
WBk := Excel.Workbooks.Add(EmptyParam, LCID); &lt;/PRE&gt;

&lt;P&gt;&lt;BR&gt;
Putting EmptyParam as the first parameter means that a new workbook with a
number of blank sheets will be created. If you pass a filename as the first
parameter, that file will be used as the template for the new workbook.
Alternatively, you can pass in one of the following constants:
&lt;B&gt;xlWBATChart&lt;/B&gt;, &lt;B&gt;xlWBATExcel4IntlMacroSheet&lt;/B&gt;,
&lt;B&gt;xlWBATExcel4MacroSheet&lt;/B&gt;, or &lt;B&gt;xlWBATWorksheet&lt;/B&gt;. This will create a
new workbook with a single sheet of the specified type.&lt;/P&gt;
&lt;P&gt; &lt;I&gt;Late binding:&lt;/I&gt; &lt;/P&gt;
&lt;P&gt;In late binding, you don't have to specify optional parameters or LCIDs, so
you can just do this:&lt;/P&gt;
&lt;PRE&gt; WBk := Excel.WorkBooks.Add;
&lt;/PRE&gt;

&lt;P&gt;If you're not using the type library, but want to use one of the constants
mentioned above, you can define them in your code like this:&lt;/P&gt;
&lt;PRE&gt; const
xlWBATChart = $FFFFEFF3;
xlWBATExcel4IntlMacroSheet = $00000004;
xlWBATExcel4MacroSheet = $00000003;
xlWBATWorksheet = $FFFFEFB9;

&lt;/PRE&gt;

&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#HowDoI"&gt;&lt;FONT SIZE="2"&gt;Back to 'HowDoI'&lt;/FONT&gt;&lt;/A&gt; &lt;HR&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H4&gt;&lt;A NAME="OpenWorkbook"&gt;&lt;/A&gt;&lt;FONT
COLOR="#FF6666"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;How to open a workbook&lt;FONT
COLOR="#FF6666"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;P&gt; Assuming an application variable, Excel, and an integer variable LCID that
you've assigned the value GetUserDefaultLCID:&lt;/P&gt;
&lt;I&gt;Early binding:&lt;/I&gt; &lt;PRE&gt; var
WBk: _Workbook;
WS: _WorkSheet;
Filename: OleVariant;
...
Filename := 'C:/Test.xls';
WBk := Excel.Workbooks.Open(Filename, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, LCID);
WS := WBk.Worksheets.Item['Sheet1'] as _Worksheet;
WS.Activate(LCID);&lt;/PRE&gt;

&lt;P&gt;&lt;BR&gt;
&lt;I&gt;Late binding:&lt;/I&gt; &lt;BR&gt;
&lt;/P&gt;
&lt;P&gt;In late binding, you don't have to specify optional parameters, so you can
just do this:&lt;/P&gt;
&lt;PRE&gt; var
WBk, WS, SheetName: OleVariant;
...
WBk := Excel.WorkBooks.Open('C:/Test.xls');
WS := WBk.Worksheets.Item['SheetName'];
WS.Activate;&lt;/PRE&gt;

&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#HowDoI"&gt;&lt;FONT SIZE="2"&gt;Back to 'HowDoI'&lt;/FONT&gt;&lt;/A&gt; &lt;HR&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H4&gt;&lt;A NAME="CloseWorkBook"&gt;&lt;/A&gt;&lt;FONT
COLOR="#FF6666"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;How to close a workbook&lt;FONT
COLOR="#FF6666"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;P&gt; Assuming a _Workbook variable, WBk, and an integer variable LCID that
you've assigned the value GetUserDefaultLCID:&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Early binding&lt;/I&gt;&lt;/P&gt;
&lt;PRE&gt; var
SaveChanges: OleVariant;
...
SaveChanges := True;
WBk.Close(SaveChanges, EmptyParam, EmptyParam. LCID);

&lt;/PRE&gt;

&lt;P&gt;If you use EmptyParam as the SaveChanges parameter, the user will be asked
whether to save the workbook. The second parameter allows you to specify a
filename, and the third specifies whether the workbook should be routed to the
next recipient.&lt;BR&gt;
&lt;/P&gt;
&lt;P&gt;&lt;I&gt;Late binding&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;In late binding, it isn't necessary to specify the optional parameters or
the LCID, so you can just put this:&lt;/P&gt;
&lt;PRE&gt; WBk.Close(SaveChanges := True);
&lt;/PRE&gt;

&lt;P&gt; or this:&lt;/P&gt;
&lt;PRE&gt;
WBk.Close;
&lt;/PRE&gt;

&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#HowDoI"&gt;&lt;FONT SIZE="2"&gt;Back to 'HowDoI'&lt;/FONT&gt;&lt;/A&gt; &lt;HR&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H4&gt;&lt;A NAME="EnterData"&gt;&lt;/A&gt;&lt;FONT
COLOR="#FF6666"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;How to enter data&lt;FONT
COLOR="#FF6666"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;P&gt; Assuming a _Worksheet variable, WS:&lt;/P&gt;
&lt;PRE&gt; WS := Excel.ActiveSheet &lt;B&gt;as&lt;/B&gt; _Worksheet;

WS.Range['A1', 'A1'].Value := 'The meaning of life, the universe, and everything, is';
WS.Range['B1', 'B1'].Value := 42;
&lt;/PRE&gt;
You can enter data into many cells at once:
&lt;PRE&gt; WS.Range['C3', J42'].Formula := '=RAND()';
&lt;/PRE&gt;
You can access cells with row and column numbers or variables, like this:
&lt;PRE&gt; var
Row, Col: integer;
...
WS.Cells.Item[1, 1].Value := 'The very first cell';
WS.Cells.Item[Row, Col].Value := 'Some other cell';
&lt;/PRE&gt;

&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#HowDoI"&gt;&lt;FONT SIZE="2"&gt;Back to 'HowDoI'&lt;/FONT&gt;&lt;/A&gt; &lt;HR&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H4&gt;&lt;A NAME="CopyData"&gt;&lt;/A&gt;&lt;FONT COLOR="#FF6666"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;How
to copy data&lt;FONT COLOR="#FF6666"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;P&gt;&lt;I&gt;From one range to another&lt;/I&gt;&lt;/P&gt;
&lt;PRE&gt;
var
DestRange: OleVariant;
begin
DestRange := Excel.Range['C1', 'D4'];
Excel.Range['A1', 'B4'].Copy(DestRange);
&lt;/PRE&gt;
Make sure you're not still editing a cell when you try to copy the range, or
you'll get a 'Call was rejected by callee' exception.
&lt;P&gt;&lt;I&gt;From one sheet to another&lt;/I&gt;&lt;/P&gt;
This example will copy the first column of one sheet to the second column of
another: &lt;PRE&gt;
var
DestSheet: _Worksheet;
DestRange: OleVariant;
begin
DestSheet := Excel.WorkBooks['Test.xls'].Worksheets['Sheet1'] as _Worksheet;
DestRange := Destsheet.Range['B1', 'B1'].EntireColumn;
Excel.Range['A1', 'A1'].EntireColumn.Copy(DestRange);
&lt;/PRE&gt;

&lt;P&gt;&lt;I&gt;Via the clipboard&lt;/I&gt;&lt;/P&gt;
Using the Copy method without a destination parameter will place the cells in
the Windows clipboard: &lt;PRE&gt;
Excel.Range['A1', 'B4'].Copy(EmptyParam);
&lt;/PRE&gt;
You can then paste the cells to another range, like this:
&lt;PRE&gt;
var
WS: _Worksheet;
begin
WS := Excel.Activesheet as _Worksheet;
WS.Range['C1', 'D4'].Select;
WS.Paste(EmptyParam, EmptyParam, lcid);
&lt;/PRE&gt;

&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#HowDoI"&gt;&lt;FONT SIZE="2"&gt;Back to 'HowDoI'&lt;/FONT&gt;&lt;/A&gt; &lt;HR&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H4&gt;&lt;A NAME="FormatRange"&gt;&lt;/A&gt;&lt;FONT
COLOR="#FF6666"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;How to format a range&lt;FONT
COLOR="#FF6666"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/FONT&gt; &lt;/H4&gt;
&lt;P&gt; Assuming a _Worksheet variable, WS:&lt;/P&gt;
&lt;PRE&gt; &lt;B&gt;var&lt;/B&gt;
Format: OleVariant;
...
WS := Excel.ActiveSheet &lt;B&gt;as&lt;/B&gt; _Worksheet;
&lt;/PRE&gt;
&lt;I&gt;To format one cell in the General number style &lt;/I&gt;
&lt;PRE&gt; Format := 'General';
WS.Range['A1', 'A1'].NumberFormat := Format;
&lt;/PRE&gt;
&lt;I&gt;To format a range in the 'Text' style, aligned right&lt;/I&gt;
&lt;P&gt;Weirdly enough, to give a range a 'text' style you have to set its
NumberFormat property to '@': &lt;/P&gt;
&lt;PRE&gt;
&lt;B&gt;with&lt;/B&gt; WS.Range['A1', 'M10'] &lt;B&gt;do&lt;/B&gt;
&lt;B&gt;begin&lt;/B&gt;
NumberFormat := '@';
HorizontalAlignment := xlHAlignRight;
&lt;B&gt;end&lt;/B&gt;;
&lt;/PRE&gt;
&lt;I&gt;To format a range of cells with the 'March 4, 1999' date style&lt;/I&gt;
&lt;PRE&gt;
Format := 'mmmm d, yyyy';
WS.Range['B1', 'C10'].NumberFormat := Format;
&lt;/PRE&gt;
&lt;I&gt;To format an entire column in a customized currency style&lt;/I&gt;
&lt;PRE&gt; Format := '$#,##0.00_);[Red]($#,##0.00)';
WkSheet.Range['C1', 'C1'].EntireColumn.NumberFormat := Format;
&lt;/PRE&gt;
&lt;I&gt;To set the text in a cell to 20pt Arial, bold, and fuchsia&lt;/I&gt;
&lt;PRE&gt;
&lt;B&gt;with&lt;/B&gt; Excel.ActiveCell.Font &lt;B&gt;do&lt;/B&gt;
&lt;B&gt;begin&lt;/B&gt;
Size := 20;
FontStyle := 'Bold';
Color := clFuchsia;
Name := 'Arial';
&lt;B&gt;end&lt;/B&gt;;
&lt;/PRE&gt;

&lt;P&gt;&lt;I&gt;To change the cell's colour&lt;/I&gt;&lt;/P&gt;
&lt;PRE&gt; Excel.ActiveCell.Interior.Color := clBtnFace; &lt;/PRE&gt;
or
&lt;PRE&gt; Excel.Range['B2', 'C6'].Interior.Color := RGB(223, 123, 123); &lt;/PRE&gt;
&lt;I&gt;To make the first three characters in a cell bold&lt;/I&gt;
&lt;PRE&gt; &lt;B&gt;var&lt;/B&gt;
Start, Length: OleVariant;
...
Start := 1;
Length := 3;
Excel.ActiveCell.Characters[Start, Length].Font.FontStyle := 'Bold';
Start := 4;
Length := 16;
Excel.ActiveCell.Characters[Start, Length].Font.FontStyle := 'Regular';
&lt;/PRE&gt;

&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#HowDoI"&gt;&lt;FONT SIZE="2"&gt;Back to 'HowDoI'&lt;/FONT&gt;&lt;/A&gt;
&lt;/DIV&gt;
&lt;HR&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H4&gt;&lt;A NAME="AddName"&gt;&lt;/A&gt;&lt;FONT COLOR="#FF6666"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;How
to add a name to a workbook&lt;FONT COLOR="#FF6666"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/FONT&gt;
&lt;/H4&gt;
&lt;PRE&gt;
&lt;B&gt;var&lt;/B&gt;
WB: _Workbook;
N: Excel_TLB.Name; // &lt;I&gt;or N := Excel97.Name; if you're using D5 &lt;/I&gt;
&lt;B&gt;begin&lt;/B&gt;
WB := Excel.ActiveWorkbook;
N := WB.Names.Add('AddedName', '=Sheet1!$A$1:$D$3', EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
&lt;/PRE&gt;
The first parameter is the new name, the second is what the name refers to. If
the third parameter is set to False, the name will be hidden and won't appear
in the Define Name or Goto dialogs.
&lt;P&gt; Two possible problems to note here. First, to declare a variable of type
'Name', you'll probably need to scope it explicitly, so: &lt;/P&gt;
&lt;PRE&gt; N: Excel_TLB.Name; &lt;/PRE&gt;
Secondly, note that the $ signs in the RefersTo parameter are essential.
Leaving them out will cause a varied assortment of unexpected results.
&lt;I&gt;(Thanks to Airy Magnien for pointing this out.)&lt;/I&gt;
&lt;P&gt;&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#HowDoI"&gt;&lt;FONT SIZE="2"&gt;Back to 'HowDoI'&lt;/FONT&gt;&lt;/A&gt; &lt;HR&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H4&gt;&lt;A NAME="AddMacro"&gt;&lt;/A&gt;&lt;FONT COLOR="#FF6666"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;How
to add a macro to a workbook&lt;FONT COLOR="#FF6666"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/FONT&gt;
&lt;/H4&gt;
&lt;P&gt; You can modify Excel VBA code at run time, by writing directly to the code
module in the Excel VBA editor, adding or deleting lines and events. The
important object here is the CodeModule object (cunningly made hard to find in
the Excel VBA help). This is declared in the VBIDE97.pas file. Here's an
example of how to use it: &lt;/P&gt;
&lt;PRE&gt;
&lt;B&gt;uses&lt;/B&gt;
VBIDE97; // &lt;I&gt;or VBIDE_TLB for Delphi 4&lt;/I&gt;
&lt;B&gt;var&lt;/B&gt;
LineNo: integer;
CM: CodeModule;
&lt;B&gt;begin&lt;/B&gt;
CM := WBk.VBProject.VBComponents.Item('ThisWorkbook').Codemodule;
LineNo := CM.CreateEventProc('Activate', 'Workbook');
CM.InsertLines(LineNo + 1, ' Range(&amp;quot;A1&amp;quot;).Value = &amp;quot;Workbook activated!&amp;quot;');
&lt;/PRE&gt;

&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#HowDoI"&gt;&lt;FONT SIZE="2"&gt;Back to 'HowDoI'&lt;/FONT&gt;&lt;/A&gt; &lt;HR&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="left"&gt;
&lt;H4&gt;&lt;A NAME="Downloads"&gt;&lt;/A&gt;&lt;FONT
COLOR="#FF6666"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/FONT&gt;Downloads&lt;FONT
COLOR="#FF6666"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/FONT&gt;&lt;/H4&gt;
&lt;P&gt; You can download an example project for Excel automation from &lt;a href="D5Excel.zip"&gt;here&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt; &lt;a href="http://www.stefancr.yucom.be"&gt;Stefan Cruysberghs&lt;/a&gt; has written
a component to help in exporting data from a TDataset to Excel. Download TscExcelExport
&lt;a href="excelexport.zip"&gt;here&lt;/a&gt;.&lt;/P&gt;
&lt;/DIV&gt;
&lt;DIV ALIGN="center"&gt;
&lt;A HREF="#HowDoI"&gt;&lt;FONT SIZE="2"&gt;Back to 'HowDoI'&lt;/FONT&gt;&lt;/A&gt;
&lt;/DIV&gt;
&lt;HR&gt;
&lt;!-- #EndEditable --&gt;
 
大哥,你也不错哦,呵呵……
 
&lt;h1&gt;看来这里的速度真的快了很多,居然这么长的文章也可以贴!&lt;/h1&gt;

但是,“问题检索”是的出错率仍然很高啊!!!
 
查询是数据库太大,query time out,要把最大运行事件调长点才行
 
真够狠的~!
Re一下也让你们收个炸弹~!呵呵
 
各位大虾,特别是Pipi.:
  我研究了一个上午,问题2、3和4都已经解决了。但是问题1还是没有解决。虽然Pipi.已经
说了用:
1、 worksheet.HPageBreaks[1].Location 水平分页符下面的格子(range对象)
worksheet.VPageBreaks[1].Location 垂直分页符右面的格子(range对象)
  我也看了Microsoft Excel Visual Basic参考(在Office 2000的Vbaxl9.chm文件中):
&lt;b&gt;Location 属性示例&lt;/b&gt;
本示例移动水平分页符的位置。
Worksheets(1).HPageBreaks(1).Location = Worksheets(1).Range("e5")

  但是,我还是不知道怎样知道各个分页符的位置!也就是分页符的row和col是什么?
因为在我的程序中,要根据内容的多少来画表。内容少,要把两个表合并。内容多,即使是
类型相同的内容,也要分多个表显示。总之,要一页一表!!!

&lt;h1&gt;怎么办啊?&lt;/h1&gt;

下面就是我用来测试Pipi.的答案的源代码,只是问题1不好解决。
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleServer, Excel97, ComObj;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

var
APP_Excel: OleVariant;
WorkBook: OleVariant;


procedure TForm1.Button1Click(Sender: TObject);
begin
button1.Enabled:=false;
try
workbook.worksheets[1].range['A1:G1'].merge(true);//合并
workbook.worksheets[1].range['A1:G1'].font.color:=clblue;
workbook.worksheets[1].range['A1:G1'].font.name:='隶书';
workbook.worksheets[1].range['A1:G1'].font.size:=18;
workbook.worksheets[1].range['A1:G1'].horizontalalignment:=xlHAlignCenter;
workbook.worksheets[1].cells[1,1].value:='大富翁论坛';
workbook.worksheets[1].cells[2,4].value:='honestman';

//正式的。
workbook.worksheets[1].Range['A4:D6'].Borders.LineStyle:=xlContinuous;
workbook.worksheets[1].Range['A4:D6'].Borders.Color:=RGB(0,0,0);
workbook.worksheets[1].Rows[5].Insert;
workbook.worksheets[1].range['A2:G2'].copy(workbook.Worksheets[1].Range['B10']);

except
showmessage('Error!');
end;
button1.Enabled:=true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
APP_Excel := CreateOleObject('Excel.Application');
WorkBook := APP_Excel.Workbooks.Add;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
loop: byte;
begin
//关闭
Try
For loop := 1 to app_Excel.Workbooks.Count Do
app_Excel.Workbooks[loop].Close[true];
app_Excel.Quit;
Except
MessageDlg('Unable to Close Excel', mtError, [mbOK], 0);
End;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
button2click(nil);
end;

end.
 
7456,今天试了一下下面的语句:
workbook.worksheets[1].range['A4:A5'].merge(true);//合并
workbook.worksheets[1].range['B4:D4'].merge(true);//合并
workbook.worksheets[1].range['E4:G4'].merge(true);//合并
workbook.worksheets[1].range['H4:J4'].merge(true);//合并
workbook.worksheets[1].range['K4:K5'].merge(true);//合并
结果发现,列与列的合并是没有问题的,但是行与行的合并却不行。
虽然没有报告错误,但是也没有任何效果。
是否Excel 2000的合并语句和97的有所不同???
 
原来改为:
workbook.worksheets[1].range['A4:A5'].merge(false);//合并
workbook.worksheets[1].range['B4:D4'].merge(false);//合并
workbook.worksheets[1].range['E4:G4'].merge(false);//合并
workbook.worksheets[1].range['H4:J4'].merge(false);//合并
workbook.worksheets[1].range['K4:K5'].merge(false);//合并
就可以了。
 
问题解决了 就请结束吧 :)
 
还有问题1没有解决啊!
 
后退
顶部