In my opinion, knowing how to create an Excel file using
Before we go further, create a user interface like picture below :
The OLE function for creating an Excel application is :
function CreateOleObject(const ClassName: string)
This function is in ComObj unit. Add this unit into the uses declaration in the program. Then type the codes below :
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComObj;
type
TForm1 = class(TForm)
CreateExcel: TButton;
Exit: TButton;
procedure ExitClick(Sender: TObject);
procedure CreateExcelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.CreateExcelClick(Sender: TObject);
var
ExcelApplication : variant;
Sheet : variant;
begin
try
begin
ExcelApplication := CreateOleObject('Excel.Application');
ExcelApplication.Visible := true;
end;
except
Showmessage('Cannot create an Excel file, make sure that MS Excel is installed on your system');
Application.Terminate;
end;
ExcelApplication.WorkBooks.Add(-4167);
ExcelApplication.WorkBooks[1].WorkSheets[1].Name := 'my data';
Sheet := ExcelApplication.WorkBooks[1].WorkSheets['my data'];
end;
procedure TForm1.ExitClick(Sender: TObject);
begin
Application.Terminate;
end;
end.
The explanation :
Action | Codes |
---|---|
Declare two variables : ExcelApplication and Sheet as variant | ExcelApplication : variant; Sheet : variant; |
Create an MS Excel application using try…except… clause and CreateOleObject function. Then make it visible | ExcelApplication := CreateOleObject('Excel.Application'); ExcelApplication.Visible := true; |
Create a blank Excel workbook | ExcelApplication.WorkBooks.Add(-4167); |
Create a blank Excel worksheet | ExcelApplication.WorkBooks[1].WorkSheets[1].Name := 'my data'; |
Set the working worksheet into Sheet variable | Sheet := ExcelApplication.WorkBooks[1].WorkSheets['my data']; |
Why do we need to set the working worksheet into Sheet variable ? It is in order to avoid typing a long character string : ExcelApplication.WorkBooks[1].WorkSheets['my data'] to access the worksheet.
Go to Next Tutorial ( Setting Attributes of an Excel File )
For me exist some new softwares which I frequently use for work with excel files. But one day I ran upon a tool, which stroke me and helped with old issues. I conceive that this tool can many good resources for resolving varied troubles with excel documents - Excel files fix.
ReplyDeletenice posting
ReplyDeleteAnd what about if not having installed EXCEL?
ReplyDeleteHow can be done this if no M$EXCEL / OppenOffice Calc, etc is installed...
-Create XLS (Excel97 format) from Delphi code
-Include Border styles for each cell
-Include background color and font format for each cell
-Include some cells with ColSpan and/or RowSpan
-Add more than just one sheet
-Set Sheets names
-Set each sheet header and footer
-Set how many rows and cols will be repeated on each page, different for each sheet
-etc
All without having M$EXCEL, etc... on developing computer, also without having them on destination computer.
It is so tricky i did not found yet a solution.
Of course a Freeware (no charge at all) solution... paid solutions seem to be out there from near $80 to $300 or more.
s The best and a lot of secret drop-shipping suppliers in the world which will provide your products globally available for you directly to your customers doors. Also get the best drop-shipping manufacturer on your organization.
ReplyDelete