B bluedna Unregistered / Unconfirmed GUEST, unregistred user! 2002-12-12 #1 怎样将DBgrid中的内容导到EXCEL中? 给个详细一点的例子吧,我试试, 还有我用Ttable控件怎么实现SQL中的GROUP BY 功能?
影 影 子 Unregistered / Unconfirmed GUEST, unregistred user! 2002-12-12 #2 http://www.delphibbs.com/delphibbs/dispq.asp?lid=630160
T tommy_linux Unregistered / Unconfirmed GUEST, unregistred user! 2002-12-12 #3 procedure DataToExcel(Grid:TDbGrid;DataSet:TDataset;Title:String;sExcelFile:String); var &nbsp; &nbsp; i,j,Row:integer; &nbsp; &nbsp; WB: _WorkBook; WBs: Workbooks; FExcelWasFound:Boolean; ExcelFile:string; FileHandle: integer; irange:range; iWidth:integer; //oFontlevariant; begin try Screen.Cursor :=crHourGlass ; {for i:=0 to Grid.Columns.Count -1 do begin Dataset.Fields.DisplayWidth :=Grid.Columns.Width; end; } ExcelFile:=sExcelFile; if not fileExists(ExcelFile) then begin FileHandle:=FileCreate(ExcelFile); Fileclose(FileHandle); end; FExcelWasFound := True; try FApp := CreateOleObject('Excel.Application.9') as _Application;&nbsp; //调用Excel2000 &nbsp; except &nbsp; &nbsp; FExcelWasFound := False; &nbsp; end; &nbsp; if not FExcelWasFound then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //如果不存在,则调用Excel97 &nbsp; &nbsp; try &nbsp; &nbsp; &nbsp; FApp := CreateOleObject('Excel.Application.8') as _Application; &nbsp; &nbsp; &nbsp; FExcelWasFound := True; &nbsp; &nbsp; except &nbsp; &nbsp; &nbsp; FExcelWasFound := False; &nbsp; &nbsp; &nbsp; ShowMessage('Excel调用失败!'); &nbsp; &nbsp; end; &nbsp; if FExcelWasFound then &nbsp; begin &nbsp; &nbsp; InitVariables; &nbsp; &nbsp; New(FSPms); &nbsp; &nbsp; with FApp ,FSPms^ do &nbsp; &nbsp; begin &nbsp; &nbsp; &nbsp; App_SheetsInNewWorkbook := Get_SheetsInNewWorkbook(0); &nbsp; &nbsp; &nbsp; App_DisplayFormulaBar := Get_DisplayFormulaBar(0); &nbsp; &nbsp; &nbsp; App_ReferenceStyle := Get_ReferenceStyle(0); &nbsp; &nbsp; &nbsp; App_DisplayStatusBar := Get_DisplayStatusBar(0); &nbsp; &nbsp; &nbsp; Set_SheetsInNewWorkbook(0, 1); &nbsp; &nbsp; &nbsp; WBs := Get_Workbooks;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //打开Excel文件 &nbsp; &nbsp; &nbsp; WB := WBs.Open(excelFile, 3, false, 1, &nbsp; &nbsp; &nbsp; &nbsp; '', '', True, $00000002, 1, False, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; False, Null, False, 0); &nbsp; &nbsp; &nbsp; MakeVBScript(WB);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //初始化文件属性 &nbsp; &nbsp; end; &nbsp; &nbsp; with FApp do &nbsp; &nbsp; begin &nbsp; &nbsp; &nbsp; Set_DisplayFormulaBar(0, False); &nbsp; &nbsp; &nbsp; Set_ReferenceStyle(0, Integer(xlR1C1)); &nbsp; &nbsp; &nbsp; Set_DisplayStatusBar(0, False); &nbsp; &nbsp; &nbsp; Set_Caption(Title); &nbsp; &nbsp; end; &nbsp; &nbsp; try &nbsp; &nbsp; Row:=1; &nbsp; &nbsp; irange:=Fapp.ActiveCell ; &nbsp; &nbsp; irange.Font.Size :=9; &nbsp; &nbsp; for j:=0 to Grid.FieldCount -1 do &nbsp; &nbsp; begin &nbsp; &nbsp; if Grid.Columns[j].Visible =true then begin if DataSet.Fields[j].displaywidth>254 then iRange.ColumnWidth:=100 else begin //iWidth:=Grid.Columns[j].Width; iRange.ColumnWidth :=Grid.Columns[j].Field.DisplayWidth ; end; irange.Font.Size :=9; //ljq 2001/03/09 irange.value:=Grid.Columns[j].Title.Caption&nbsp; ; &nbsp; &nbsp; &nbsp; &nbsp; irange:=irange.Next; &nbsp; &nbsp; &nbsp; end; &nbsp; &nbsp; end; &nbsp; &nbsp; except &nbsp; &nbsp; &nbsp; ShowMessage('调用Excel出错!'); &nbsp; &nbsp; &nbsp; fApp._Release; &nbsp; &nbsp; &nbsp; Screen.Cursor :=crDefault ; &nbsp; &nbsp; &nbsp; exit; &nbsp; &nbsp; end; &nbsp; &nbsp; Row:=Row+2; &nbsp; &nbsp; DataSet.DisableControls; &nbsp; &nbsp; DataSet.First; FApp.Get_ActiveWindow.DisplayZeros := True; irange.NumberFormat:=10; for i:=0 to DataSet.RecordCount -1 do begin irange:=Fapp.Range['A'+IntToStr(Row),'A'+intToStr(Row)]; for j:=0 to Grid.FieldCount -1 Do begin if Grid.Columns[j].Visible =True then begin if Grid<>nil then begin iRange.Font.Size :=Grid.Font.Size; iRange.Font.Name :=Grid.Font.Name; end else begin irange.Font.Size :=FFontSize; irange.Font.Name :=FFontName; end; //edit by ljq 2001/03/09 iRange.Value :=Grid.Columns[j].Field.AsString ; irange:=iRange.Next ; end; end; DataSet.next; Row:=Row+1; &nbsp; &nbsp; end; &nbsp; &nbsp; Screen.Cursor :=crDefault ; &nbsp; &nbsp; DataSet.EnableControls; &nbsp; &nbsp; irange:=FApp.Range['A1','K'+intToStr(Row-1)]; &nbsp; &nbsp; FApp.Set_Visible(0,True); &nbsp; &nbsp; CreateToolBar(False);&nbsp; &nbsp; &nbsp; //屏蔽Excel的系统菜单,采用自定义菜单实现 &nbsp; end else &nbsp; begin &nbsp; &nbsp; ShowMessage('调用Excel2000或Excel97失败,请确认是否安装!'+#13#13+' 如果未安装,请先安装office'); &nbsp; &nbsp; Screen.Cursor :=crDefault ; &nbsp; end; &nbsp; except &nbsp; &nbsp; ShowMessage('调用Excel出错!'); &nbsp; &nbsp; fApp._Release; &nbsp; &nbsp; Screen.Cursor :=crDefault ; &nbsp; &nbsp; exit; &nbsp; end; end; //转贴的:)
procedure DataToExcel(Grid:TDbGrid;DataSet:TDataset;Title:String;sExcelFile:String); var &nbsp; &nbsp; i,j,Row:integer; &nbsp; &nbsp; WB: _WorkBook; WBs: Workbooks; FExcelWasFound:Boolean; ExcelFile:string; FileHandle: integer; irange:range; iWidth:integer; //oFontlevariant; begin try Screen.Cursor :=crHourGlass ; {for i:=0 to Grid.Columns.Count -1 do begin Dataset.Fields.DisplayWidth :=Grid.Columns.Width; end; } ExcelFile:=sExcelFile; if not fileExists(ExcelFile) then begin FileHandle:=FileCreate(ExcelFile); Fileclose(FileHandle); end; FExcelWasFound := True; try FApp := CreateOleObject('Excel.Application.9') as _Application;&nbsp; //调用Excel2000 &nbsp; except &nbsp; &nbsp; FExcelWasFound := False; &nbsp; end; &nbsp; if not FExcelWasFound then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //如果不存在,则调用Excel97 &nbsp; &nbsp; try &nbsp; &nbsp; &nbsp; FApp := CreateOleObject('Excel.Application.8') as _Application; &nbsp; &nbsp; &nbsp; FExcelWasFound := True; &nbsp; &nbsp; except &nbsp; &nbsp; &nbsp; FExcelWasFound := False; &nbsp; &nbsp; &nbsp; ShowMessage('Excel调用失败!'); &nbsp; &nbsp; end; &nbsp; if FExcelWasFound then &nbsp; begin &nbsp; &nbsp; InitVariables; &nbsp; &nbsp; New(FSPms); &nbsp; &nbsp; with FApp ,FSPms^ do &nbsp; &nbsp; begin &nbsp; &nbsp; &nbsp; App_SheetsInNewWorkbook := Get_SheetsInNewWorkbook(0); &nbsp; &nbsp; &nbsp; App_DisplayFormulaBar := Get_DisplayFormulaBar(0); &nbsp; &nbsp; &nbsp; App_ReferenceStyle := Get_ReferenceStyle(0); &nbsp; &nbsp; &nbsp; App_DisplayStatusBar := Get_DisplayStatusBar(0); &nbsp; &nbsp; &nbsp; Set_SheetsInNewWorkbook(0, 1); &nbsp; &nbsp; &nbsp; WBs := Get_Workbooks;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //打开Excel文件 &nbsp; &nbsp; &nbsp; WB := WBs.Open(excelFile, 3, false, 1, &nbsp; &nbsp; &nbsp; &nbsp; '', '', True, $00000002, 1, False, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; False, Null, False, 0); &nbsp; &nbsp; &nbsp; MakeVBScript(WB);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //初始化文件属性 &nbsp; &nbsp; end; &nbsp; &nbsp; with FApp do &nbsp; &nbsp; begin &nbsp; &nbsp; &nbsp; Set_DisplayFormulaBar(0, False); &nbsp; &nbsp; &nbsp; Set_ReferenceStyle(0, Integer(xlR1C1)); &nbsp; &nbsp; &nbsp; Set_DisplayStatusBar(0, False); &nbsp; &nbsp; &nbsp; Set_Caption(Title); &nbsp; &nbsp; end; &nbsp; &nbsp; try &nbsp; &nbsp; Row:=1; &nbsp; &nbsp; irange:=Fapp.ActiveCell ; &nbsp; &nbsp; irange.Font.Size :=9; &nbsp; &nbsp; for j:=0 to Grid.FieldCount -1 do &nbsp; &nbsp; begin &nbsp; &nbsp; if Grid.Columns[j].Visible =true then begin if DataSet.Fields[j].displaywidth>254 then iRange.ColumnWidth:=100 else begin //iWidth:=Grid.Columns[j].Width; iRange.ColumnWidth :=Grid.Columns[j].Field.DisplayWidth ; end; irange.Font.Size :=9; //ljq 2001/03/09 irange.value:=Grid.Columns[j].Title.Caption&nbsp; ; &nbsp; &nbsp; &nbsp; &nbsp; irange:=irange.Next; &nbsp; &nbsp; &nbsp; end; &nbsp; &nbsp; end; &nbsp; &nbsp; except &nbsp; &nbsp; &nbsp; ShowMessage('调用Excel出错!'); &nbsp; &nbsp; &nbsp; fApp._Release; &nbsp; &nbsp; &nbsp; Screen.Cursor :=crDefault ; &nbsp; &nbsp; &nbsp; exit; &nbsp; &nbsp; end; &nbsp; &nbsp; Row:=Row+2; &nbsp; &nbsp; DataSet.DisableControls; &nbsp; &nbsp; DataSet.First; FApp.Get_ActiveWindow.DisplayZeros := True; irange.NumberFormat:=10; for i:=0 to DataSet.RecordCount -1 do begin irange:=Fapp.Range['A'+IntToStr(Row),'A'+intToStr(Row)]; for j:=0 to Grid.FieldCount -1 Do begin if Grid.Columns[j].Visible =True then begin if Grid<>nil then begin iRange.Font.Size :=Grid.Font.Size; iRange.Font.Name :=Grid.Font.Name; end else begin irange.Font.Size :=FFontSize; irange.Font.Name :=FFontName; end; //edit by ljq 2001/03/09 iRange.Value :=Grid.Columns[j].Field.AsString ; irange:=iRange.Next ; end; end; DataSet.next; Row:=Row+1; &nbsp; &nbsp; end; &nbsp; &nbsp; Screen.Cursor :=crDefault ; &nbsp; &nbsp; DataSet.EnableControls; &nbsp; &nbsp; irange:=FApp.Range['A1','K'+intToStr(Row-1)]; &nbsp; &nbsp; FApp.Set_Visible(0,True); &nbsp; &nbsp; CreateToolBar(False);&nbsp; &nbsp; &nbsp; //屏蔽Excel的系统菜单,采用自定义菜单实现 &nbsp; end else &nbsp; begin &nbsp; &nbsp; ShowMessage('调用Excel2000或Excel97失败,请确认是否安装!'+#13#13+' 如果未安装,请先安装office'); &nbsp; &nbsp; Screen.Cursor :=crDefault ; &nbsp; end; &nbsp; except &nbsp; &nbsp; ShowMessage('调用Excel出错!'); &nbsp; &nbsp; fApp._Release; &nbsp; &nbsp; Screen.Cursor :=crDefault ; &nbsp; &nbsp; exit; &nbsp; end; end; //转贴的:)