//Sending Data to Excel
procedure TForm1.BitBtnToExcelClick(Sender: TObject);
var
WorkBk : _WorkBook; // Define a WorkBook
WorkSheet : _WorkSheet; // Define a WorkSheet
I, J, R, C : Integer;
IIndex : OleVariant;
TabGrid : Variant;
begin
if GenericStringGrid.Cells[0,0] <> '' then
begin
IIndex := 1;
R := GenericStringGrid.RowCount;
C := GenericStringGrid.ColCount;
// Create the Variant Array
TabGrid := VarArrayCreate([0,(R - 1),0,(C - 1)],VarOleStr);
I := 0;
// Define the loop for filling in the Variant
repeat
for J := 0 to (C - 1) do
TabGrid[I,J] := GenericStringGrid.Cells[J,I];
Inc(I,1);
until
I > (R - 1);
// Connect to the server TExcelApplication
ExcelApp.Connect;
// Add WorkBooks to the ExcelApplication
ExcelApp.WorkBooks.Add(xlWBatWorkSheet,0);
// Select the first WorkBook
WorkBk := ExcelApp.WorkBooks.Item[IIndex];
// Define the first WorkSheet
WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
// Assign the Delphi Variant Matrix to the Variant associated with the WorkSheet
Worksheet.Range['A1',Worksheet.Cells.Item[R,C]].Value[EmptyParam] := TabGrid;
// Customise the WorkSheet
WorkSheet.Name := 'Customers';
Worksheet.Columns.Font.Bold := True;
Worksheet.Columns.HorizontalAlignment := xlRight;
WorkSheet.Columns.ColumnWidth := 14;
// Customise the first entire Column
WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].Font.Color := clBlue;
WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].HorizontalAlignment := xlHAlignLeft;
WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].ColumnWidth := 31;
// Show Excel
ExcelApp.Visible[0] := True;
// Disconnect the Server
ExcelApp.Disconnect;
// Unassign the Delphi Variant Matrix
TabGrid := Unassigned;
end;
end;
//Retrieving Data from Excel
procedure TForm1.BitBtnFromExcelClick(Sender: TObject);
var
WorkBk : _WorkBook;
WorkSheet : _WorkSheet;
K, R, X, Y : Integer;
IIndex : OleVariant;
RangeMatrix : Variant;
XLSFile : WideString;
begin
with TOpenDialog.Create(Self) do
begin
InitialDir:=ExtractFilePath(ParamStr(0));
Filter:='Excel document (*.xls)|*.xls';
Options:=[ofHideReadOnly, ofPathMustExist, ofFileMustExist];
if Execute then XLSFile:=FileName;
Free;
end;
if XLSFile='' then Exit;
IIndex := 1;
ExcelApp.Connect;
// Open the Excel File
ExcelApp.WorkBooks.Open(XLSFile,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);
WorkBk := ExcelApp.WorkBooks.Item[IIndex];
WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
// In order to know the dimension of the WorkSheet, i.e the number of rows and the
// number of columns, we activate the last non-empty cell of it
WorkSheet.Cells.SpecialCells(xlCellTypeLastCell,EmptyParam).Activate;
// Get the value of the last row
X := ExcelApp.ActiveCell.Row;
// Get the value of the last column
Y := ExcelApp.ActiveCell.Column;
// Define the number of the columns in the TStringGrid
GenericStringGrid.ColCount := Y;
// Assign the Variant associated with the WorkSheet to the Delphi Variant Matrix
RangeMatrix := ExcelApp.Range['A1',ExcelApp.Cells.Item[X,Y]].Value[EmptyParam];
// Quit Excel and Disconnect the Server
ExcelApp.Quit;
ExcelApp.Disconnect;
// Define the loop for filling in the TStringGrid
K := 1;
repeat
for R := 1 to Y do
GenericStringGrid.Cells[(R - 1),(K - 1)] := RangeMatrix[K,R];
Inc(K,1);
GenericStringGrid.RowCount := K + 1;
until
K > X;
// Unassign the Delphi Variant Matrix
RangeMatrix := Unassigned;
end;
Word的
procedure TForm1.Button1Click(Sender: TObject);
var
templateName: OleVariant;
newTemplate: OleVariant;
ItemIndex: OleVariant;
vSelection: WordSelection;
vBookMark: BookMark;
vTable: Table;
I : Integer;
begin
// get dot file name
templateName := ExtractFilePath(Application.ExeName) + 'WordApp.dot';
vTable.Cell(5, 1).Range.Text := 'LYSoft powered by Liu Yang';
vTable.Cell(7, 1).Range.Text := 'LY';
vTable.Cell(9, 2).Range.Text := 'Demo project';
for I := 1 to WordApp.ActiveDocument.Bookmarks.Count do
begin
ItemIndex := I;
// get the Index of specified bookmark
vBookMark := WordApp.ActiveDocument.Bookmarks.Item(ItemIndex);
//ShowMessage(vBookMark.Name+IntToStr(wordApp.ActiveDocument.Bookmarks.Count)+'Now='+IntToStr(i));
if CompareText(vBookMark.Name, 'bm_ID') = 0 then
begin
vBookMark.Select;
vSelection.InsertAfter(IntToStr(GetTickCount));
end;
end;
with WordApp do
begin
Visible := CheckBox1.Checked;
UserName := 'LY';
// minimize
WindowState := 2;
// maxmize
WindowState := 1;
// print preview
PrintPreview := CheckBox2.Checked;
// print now
if CheckBox3.Checked then PrintOutOld;