Delphi Export Excel

Source: Internet
Author: User

Var
Fexcel:olevariant; Excel application
Fworkbook:olevariant; Work table
Temsheet:olevariant; Working thin
fpicture:olevariant;//Pictures
tmpstr:string;
range:variant;//Range
I,j,temint:integer;
temfilename:string;
real_american_price:double;
Begin
if (kind = ' Xiaomi list ') or (kind = ' commodity list ') then
Begin
If Cxxiaomilist. Datacontroller.recordcount = 0 Then
Begin
Application.messagebox (PChar (' No data exported! '), ' hint ', MB_OK + mb_iconwarning);
Abort
End

savedialog1.filter:= '. xlsx ';
If Savedialog1.execute Then
Begin
Temfilename:=savedialog1.filename+ '. xlsx ';

Screen.cursor:=crhourglass;
temint:=0;
Try
fexcel:= createoleobject (' Excel. Application ');
Fworkbook:=fexcel.workbooks.add (-4167); A new worksheet
Temsheet:=fworkbook.worksheets.add;
Temsheet.name:=kind;
Except
Application.messagebox (' cannot create Excel file, please verify that Excel software is installed ',
PChar (Application.title), MB_OK + mb_iconwarning);
Exit;
End

Temsheet.select;
temsheet.range[' A1 ', ' A1 ']. RowHeight: = 60;

For I: = 1 to 111 do
Begin
Temsheet.columns[i]. columnwidth:=15;
Temsheet.cells[1,i]: = ";
If i = 1 Then
Temsheet.cells[1,i]: = Cxxiaomilist. BANDS[111]. Caption;
If i = Then
Temsheet.cells[1,i]: = Cxxiaomilist. BANDS[112]. Caption;
Temsheet.cells[2,i]: = Cxxiaomilist. BANDS[I-1]. Caption;
////////////////////////////////////////////////////////////
If I < Then
Temsheet.cells[3,i]: = Cxxiaomilist. COLUMNS[I-1]. Caption;
End
TEMSHEET.COLUMNS[1]. numberformat:= ' ##################### ';

range:=temsheet.range[temsheet.cells[3,22],temsheet.cells[3,23]];//Selected table
Range.Select;
Range.merge;
range:=temsheet.range[temsheet.cells[3,22],temsheet.cells[3,24]];//Selected table
Range.Select;
Range.merge;
TEMSHEET.CELLS[3,22]. horizontalalignment:=-4108; Center Word
TEMSHEET.CELLS[3,22]: = Cxxiaomilist. bands[112 + 1]. Caption;

range:=temsheet.range[temsheet.cells[3,25],temsheet.cells[3,26]];//Selected table
Range.Select;
Range.merge;
range:=temsheet.range[temsheet.cells[3,25],temsheet.cells[3,27]];//Selected table
Range.Select;
Range.merge;
TEMSHEET.CELLS[3,25]. horizontalalignment:=-4108; Center Word
TEMSHEET.CELLS[3,25]: = Cxxiaomilist. bands[112 + 2]. Caption;


range:=temsheet.range[temsheet.cells[3,109],temsheet.cells[3,110]];//Selected table
Range.Select;
Range.merge;
range:=temsheet.range[temsheet.cells[3,109],temsheet.cells[3,111]];//Selected table
Range.Select;
Range.merge;
TEMSHEET.CELLS[3,109]. horizontalalignment:=-4108; Center Word
TEMSHEET.CELLS[3,109]: = Cxxiaomilist. bands[112 + 30]. Caption;

For I: = 0 to Cxxiaomilist. Datacontroller.recordcount-1 do
Begin
for J: = 1 to Cxxiaomilist. ColumnCount do
Begin
Temsheet.cells[4 + i,j]: = Vartostr (cxxiaomilist. Datacontroller.getvalue (i,j-1));
If J = Ten Then
If Trystrtofloat (Vartostr (cxxiaomilist. Datacontroller.getvalue (I,J)), Real_american_price) Then
If Real_american_price >
Begin
Temsheet.cells[4 + i,j]. Interior.Color: = clred;
Temsheet.cells[4 + I,j + 4]. Interior.Color: = $0075c76b;
End
End
End



For i:=0 to Dbgrid2.columns.count-1 do
Begin
Temsheet.cells[temint,i+2]:=dbgrid2.columns[i]. Title.caption;
TEMSHEET.CELLS[TEMINT,I+2]. horizontalalignment:=-4108; Center Word
TEMSHEET.CELLS[TEMINT,I+2].     Interior.color:=clgray; Cell background color
range:=temsheet.range[temsheet.cells[temint,i+2],temsheet.cells[temint,i+2]];//Selected table
range.borders.linestyle:=1;//Hua Line
End
//
temint:=temint+1;
//    //////////////////////////////////////////////
j:=0;
DBGrid2.DataSource.DataSet.First;
While not DBGrid2.DataSource.DataSet.Eof do
Begin
temsheet.cells[temint+j,1]. value:=j+1;
temsheet.cells[temint+j,1]. horizontalalignment:=-4108; Center Word
range:=temsheet.range[temsheet.cells[temint+j,1],temsheet.cells[temint+j,1]];//Selected table
range.borders.linestyle:=1;//Hua Line
//
For i:=0 to Dbgrid2.columns.count-1 do
Begin
TEMSHEET.CELLS[TEMINT+J,I+2]. Value:=dbgrid2.fields[i]. asstring;
range:=temsheet.range[temsheet.cells[temint+j,i+2],temsheet.cells[temint+j,i+2]];//Selected table
range.borders.linestyle:=1;//Hua Line
End
DBGrid2.DataSource.DataSet.Next;
j:=j+1;
End


Application.processmessages;

Screen.cursor:=crdefault;
Fexcel.workbooks[1].saveas (temfilename);//Save File
Fexcel.workbooks[1].close; Close Worksheet
Application.processmessages;
MessageBox (Handle, ' export success ', ' hint ', MB_OK);
Fexcel.visible:=true;
Fexcel.quit; Close Excel
Fexcel: = unassigned;
ShellExecute (0, ' open ', PChar (Extractfilename (Temfilename)), Nil,pchar (Extractfilepath (Temfilename)), sw_show);

End
End;

This article is from the "All for the law, such as Fleeting Dream" blog, please be sure to keep this source http://kaixinbuliao.blog.51cto.com/2567365/1982749

Delphi Export Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.