【delphi开源代码栏目提醒】:网学会员,鉴于大家对delphi开源代码十分关注,论文会员在此为大家搜集整理了“frm_DBToExcel.pas”一文,供大家参考学习!
unit frm_DBToExcel;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Excel2000, OleServer, DB, ADODB, Grids, DBGrids, shellAPI,
XPMan;
type
TFrmDBToExcel = class(TForm)
ATList: TADOTable;
fontselectbox: TFontDialog;
ADOConn: TADOConnection;
GBxList: TGroupBox;
DSList: TDataSource;
DBGList: TDBGrid;
GroupBox1: TGroupBox;
BtnClose: TButton;
btnSetFont: TButton;
btnExcel: TButton;
XPManifest1: TXPManifest;
procedure btnExcelClick(Sender: TObject);
procedure BtnCloseClick(Sender: TObject);
procedure btnSetFontClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure Printanjuan(dir:string);
{ Private declarations }
public
{ Public declarations }
end;
var
FrmDBToExcel: TFrmDBToExcel;
implementation
{$R *.dfm}
procedure TFrmDBToExcel.Printanjuan(dir:string);
var
nowhangi, jilushu, LCID: integer;
ExcelApp: TExcelApplication;
Excelbook: TExcelWorkbook;
ExcelSheet: TExcelWorksheet;
begin
LCID := LOCALE_USER_DEFAULT;
begin
try
ExcelApp := TExcelApplication.Create(Self);
Excelbook := TExcelWorkbook.Create(Self);
ExcelSheet := TExcelWorksheet.Create(Self);
except
showmessage('对不起,您没有安装Excel 2000!');
Exit;
end;
end;
ExcelApp.Connect;
ExcelApp.Workbooks.Add(null, 0);
Excelbook.ConnectTo(ExcelApp.Workbooks[1]);
ExcelSheet.ConnectTo(Excelbook.Sheets[1] as _WorkSheet);
if ATList.RecordCount > 0 then
begin
nowhangi := 1;
try
ExcelSheet.cells.Item[nowhangi , 1].font.Name := fontselectbox.Font.Name;
ExcelSheet.cells.Item[nowhangi , 1].font.size := 12;
ExcelSheet.cells.Item[nowhangi , 1].Font.Bold := True;
ExcelSheet.cells.Item[nowhangi , 1].Orientation := xlHorizontal;
ExcelSheet.cells.Item[nowhangi , 1].VerticalAlignment := xlTop;
ExcelSheet.cells.Item[nowhangi , 1] := '顺序号';
ExcelSheet.cells.Item[nowhangi , 2].font.Name := fontselectbox.Font.Name;
ExcelSheet.cells.Item[nowhangi , 2].font.size := 12;
ExcelSheet.cells.Item[nowhangi , 2].Font.Bold := True;
ExcelSheet.cells.Item[nowhangi , 2].Orientation := xlHorizontal;
ExcelSheet.cells.Item[nowhangi , 2].VerticalAlignment := xlTop;
ExcelSheet.cells.Item[nowhangi , 2] := '物品名称';
ExcelSheet.cells.Item[nowhangi , 3].font.Name := fontselectbox.Font.Name;
ExcelSheet.cells.Item[nowhangi , 3].font.size := 12;
ExcelSheet.cells.Item[nowhangi , 3].Font.Bold := True;
ExcelSheet.cells.Item[nowhangi , 3].Orientation := xlHorizontal;
ExcelSheet.cells.Item[nowhangi , 3].VerticalAlignment := xlTop;
ExcelSheet.cells.Item[nowhangi , 3] := '物品编号';
ExcelSheet.cells.Item[nowhangi , 4].font.Name := fontselectbox.Font.Name;
ExcelSheet.cells.Item[nowhangi , 4].font.size := 12;
ExcelSheet.cells.Item[nowhangi , 4].Font.Bold := True;
ExcelSheet.cells.Item[nowhangi , 4].Orientation := xlHorizontal;
ExcelSheet.cells.Item[nowhangi , 4].VerticalAlignment := xlTop;
ExcelSheet.cells.Item[nowhangi , 4] := '价格';
ExcelSheet.cells.Item[nowhangi , 5].font.Name := fontselectbox.Font.Name;
ExcelSheet.cells.Item[nowhangi , 5].font.size := 12;
ExcelSheet.cells.Item[nowhangi , 5].Font.Bold := True;
ExcelSheet.cells.Item[nowhangi , 5].Orientation := xlHorizontal;
ExcelSheet.cells.Item[nowhangi , 5].VerticalAlignment := xlTop;
ExcelSheet.cells.Item[nowhangi , 5] := '库存';
for jilushu := 1 to (ATlist.RecordCount) do
begin
nowhangi := nowhangi + 1;
ExcelSheet.cells.Item[nowhangi, 1].RowHeight := 20;
ExcelSheet.cells.Item[nowhangi, 1].font.Name := fontselectbox.Font.Name;
ExcelSheet.cells.Item[nowhangi, 1].font.size := fontselectbox.Font.Size;
ExcelSheet.cells.Item[nowhangi, 1].WrapText := True;
ExcelSheet.cells.Item[nowhangi, 1] := inttostr(jilushu);
ExcelSheet.cells.Item[nowhangi, 2].font.Name := fontselectbox.Font.Name;
ExcelSheet.cells.Item[nowhangi, 2].WrapText := True;
ExcelSheet.cells.Item[nowhangi, 2].font.size := fontselectbox.Font.Size;;
ExcelSheet.cells.Item[nowhangi, 2] := ATList.Fieldbyname('Name').AsString;
ExcelSheet.cells.Item[nowhangi, 3].font.Name := fontselectbox.Font.Name;
ExcelSheet.cells.Item[nowhangi, 3].WrapText := True;
ExcelSheet.cells.Item[nowhangi, 3].font.size := fontselectbox.Font.Size;;
ExcelSheet.cells.Item[n