Re: Delphi и Excel
ключевые слова:
delphi excel helloworld
найти и заботать статью "по волнам интеграции", в трёх частях если у меня не склероз.
OK, спасибо.
procedure TFormMain.ExportTitle(SomeDBGrid: TDBGrid);
var Matrix : Variant;
AddDTitle,IntBeginRow,IntEndRow,IntColumn:integer;
begin
AddDTitle:=0;
ExcelWorksheet1.Range['A1','A1'].Select;
ExcelApplication1.ActiveCell.FormulaR1C1 :='Дата создания документа: '+DateToStr(date);
//форма обучения
if SearchIndex(CBFormObuch,1)>0 then begin
inc(AddDTitle);
ExcelWorksheet1.Range['A'+IntToStr(AddDTitle+1'A'+IntToStr(AddDTitle+1)].Select;
ExcelApplication1.ActiveCell.FormulaR1C1 :='Форма обучения: '+CBFormObuch.Text;
end;
//... другие заголовки
end;
//экспорт данных
procedure TFormMain.ExportFieldData(SomeDBGrid: TDBGrid);
var
IntColumn : Integer;
IntBeginRow, IntEndRow : Integer;
IntMatrixRow : Integer;
PtBookmark : TBookmark;
Matrix : Variant;
begin
//работа с набором данных
FIntBlockOfRecords:=SomeDBGrid.DataSource.DataSet.RecordCount;
FIntRecordNo := 0;
try
// Create a matrix of variants
// - Columns = number of fielss
// - Rows = block of records (FIntBlockOfRecords)
Matrix := VarArrayCreate([1,FIntBlockOfRecords,1,SomeDBGrid.DataSource.Dataset.Fields.Count-DCol],varVariant);
if SomeDBGrid.DataSource.Dataset.RecordCount>0 then begin
IntBeginRow :=DTitle;
IntEndRow :=SomeDBGrid.DataSource.Dataset.RecordCount+DTitle-1;
end;
IntMatrixRow := 0;
while not SomeDBGrid.DataSource.Dataset.EOF do
begin
Inc(FIntRecordNo);
Inc(IntMatrixRow);
for IntColumn := DCol+1 to SomeDBGrid.DataSource.Dataset.Fields.Count do
Matrix[IntMatrixRow,IntColumn-DCol] := SomeDBGrid.DataSource.Dataset.Fields.Fields[IntColumn-1].asVariant;
// Create a new block of records to export to Excel
// Don't export all data to one variant matrix because memory has it limitations
// Property FIntBlockOfRecords is default 20 records
ExcelWorksheet1.Range['A'+IntToStr(IntBeginRowGetColumnCharacters(SomeDBGrid.DataSource.Dataset.Fields.Count-DCol)+IntToStr(IntEndRow)].Value := Matrix;
IntBeginRow := IntBeginRow+1;// + FIntBlockOfRecords;
IntEndRow := IntBeginRow+1 ;//+ FIntBlockOfRecords;
IntMatrixRow := 0;
SomeDBGrid.DataSource.Dataset.Next;
end;
finally
end;
end;
function TFormMain.ExpotrExcel(DSomeDBGrid: TDBGrid):Boolean;
var
W:_WorkBook;
begin
//открытие объекта автоматизации Excel;
LCID := LOCALE_USER_DEFAULT; //
ExcelApplication1.Connect; //Установить соединение
W:=ExcelApplication1.Workbooks.add(xlWBATWorksheet,LCID);//добавление книги
ExcelWorkbook1.ConnectTo(W);//установить соединение с книгой
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);//установить соединение с листом
//экспорт заголовков
ExportTitle(SomeDBGrid);
//экспорт данных
ExportFieldData(SomeDBGrid);
//...
end;
О, спасибо .
Оставить комментарий
sgabi
Ребята, если кто-то сталкивался, подскажите, как сохранять таблицу в икселе?Допустим, что таблицу можно посмотреть с Memo- окна.