excel 的一些操作
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls,
System.DateUtils,
cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer,
cxEdit, dxCore, cxDateUtils, dxSkinsCore, dxSkinBlack, dxSkinBlue,
dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins,
dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013White,
dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010,
dxSkinWhiteprint, dxSkinXmas2008Blue, cxTextEdit, cxMaskEdit, cxDropDownEdit,
cxCalendar, System.IniFiles, System.Win.ComObj, db, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
IdMessageClient, IdSMTPBase, IdSMTP, IdMessage, IdAttachment,
IdAttachmentFile, ShellAPI, System.Zip;
type
Tform1 = class(TForm)
btnSendEmail: TButton;
Label1: TLabel;
beginDate: TcxDateEdit;
endDate: TcxDateEdit;
smtp: TIdSMTP;
msg: TIdMessage;
procedure btnSendEmailClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
ini: TIniFile;
excelApp: Variant;
procedure DataSetToExcel(cds: TDataSet; sheet: Variant);
// function GetFieldLabel(const fieldName: string): string;
public
{ Public declarations }
end;
var
form1: Tform1;
implementation
{$R *.dfm}
uses untDB;
procedure Tform1.btnSendEmailClick(Sender: TObject);
var
ini: TIniFile;
shopname, filename, zipfile: string;
zip: TZipFile;
begin
try
excelApp := CreateOleObject('Excel.Application');
except
MessageDlg('请安装EXCEL', mtInformation, [mbOK], 0);
Exit;
end;
excelApp.Visible := False;
excelApp.WorkBooks.Add;
excelApp.WorkBooks[1].WorkSheets.Add;
excelApp.WorkBooks[1].WorkSheets.Add;
excelApp.WorkBooks[1].WorkSheets[1].name := '收款报表';
excelApp.WorkBooks[1].WorkSheets[2].name := '畅销商品报表';
excelApp.WorkBooks[1].WorkSheets[3].name := '滞销商品报表';
excelApp.WorkBooks[1].WorkSheets[4].name := '客流量报表';
excelApp.WorkBooks[1].WorkSheets[5].name := '销售报表';
ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'config.ini');
try
shopname := ini.ReadString('shop', 'shopName', '');
finally
ini.Free;
end;
// 收款报表
frmDB.qry.Close;
frmDB.qry.SQL.Clear;
frmDB.qry.SQL.Text := 'execute sp_report_saleDate ' +
QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', beginDate.Date)) + ',' +
QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', endDate.Date));
frmDB.qry.open;
if not frmDB.qry.IsEmpty then
begin
DataSetToExcel(frmDB.qry, excelApp.WorkBooks[1].WorkSheets[1]);
end;
// 畅销商品报表
frmDB.qry.Close;
frmDB.qry.SQL.Clear;
frmDB.qry.SQL.Text := 'execute sp_report_saleIndex ' +
QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', beginDate.Date)) + ',' +
QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', endDate.Date));
frmDB.qry.open;
if not frmDB.qry.IsEmpty then
begin
DataSetToExcel(frmDB.qry, excelApp.WorkBooks[1].WorkSheets[2]);
end;
// 滞销商品报表
frmDB.qry.Close;
frmDB.qry.SQL.Clear;
frmDB.qry.SQL.Text := 'execute sp_report_saleIndex2 ' +
QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', beginDate.Date)) + ',' +
QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', endDate.Date));
frmDB.qry.open;
if not frmDB.qry.IsEmpty then
begin
DataSetToExcel(frmDB.qry, excelApp.WorkBooks[1].WorkSheets[3]);
end;
// 客流量报表
frmDB.qry.Close;
frmDB.qry.SQL.Clear;
frmDB.qry.SQL.Text := 'execute sp_report_tcac ' +
QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', beginDate.Date)) + ',' +
QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', endDate.Date));
frmDB.qry.open;
if not frmDB.qry.IsEmpty then
begin
DataSetToExcel(frmDB.qry, excelApp.WorkBooks[1].WorkSheets[4]);
end;
// 销售报表
try
frmDB.qry.Close;
frmDB.qry.SQL.Clear;
frmDB.qry.SQL.Text := 'execute sp_report_all ' +
QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', beginDate.Date)) + ',' +
QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', endDate.Date));
frmDB.qry.open;
if not frmDB.qry.IsEmpty then
begin
DataSetToExcel(frmDB.qry, excelApp.WorkBooks[1].WorkSheets[5]);
end;
except
on E: Exception do
ShowMessage(E.Message);
end;
// 保存为EXCEL文件
filename := ExtractFilePath(Application.ExeName) + shopname + '销售数据.xls';
if FileExists(filename) then
DeleteFile(filename);
excelApp.ActiveWorkbook.SaveAs(filename);
excelApp.ActiveWorkbook.Saved := true;
excelApp.WorkBooks.Close;
excelApp.Quit;
// 压缩excel文件
zipfile := ExtractFilePath(Application.ExeName) + shopname + '销售数据.zip';
if FileExists(zipfile) then
DeleteFile(zipfile);
zip := TZipFile.Create;
zip.Open(zipfile, TZipMode.zmWrite); //准备要压缩为 001.zip
zip.Add(filename, shopname + '销售数据.xls'); //参1是要压缩的文件; 参2是要使用的文件名; 参数3可指定压缩算法
zip.Free;
// 发送电子邮件
ShellExecute(handle, 'open', PChar(ExtractFilePath(Application.ExeName) +
'gmail.vbs'), nil, nil, SW_HIDE);
end;
procedure Tform1.DataSetToExcel(cds: TDataSet; sheet: Variant);
var
j, x: integer;
begin
// 标题行
for j := 0 to cds.FieldCount - 1 do
begin
sheet.Cells[1, j + 1].Value := ini.ReadString('fields', cds.Fields[j].FieldName, cds.Fields[j].FieldName);
end;
// 设置为文本格式
sheet.Cells.NumberFormatLocal := '@ ';
// 导数据
cds.DisableControls;
try
x := 2;
cds.First;
while not cds.Eof do
begin
for j := 0 to cds.FieldCount - 1 do
begin
sheet.Cells[x, j + 1] := Trim(cds.Fields[j].Text);
end;
x := x + 1;
cds.Next;
end;
finally
cds.EnableControls;
end;
end;
procedure Tform1.FormDestroy(Sender: TObject);
begin
ini.Free;
end;
procedure Tform1.FormShow(Sender: TObject);
begin
beginDate.Date := StartOfTheDay(Date);
endDate.Date := EndOfTheDay(Date);
ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'config.ini');
end;
end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/3843593.html