请问在三层结构中,怎样将DBGRID中的数据导出至EXCEL表格中?(200分)

  • 主题发起人 主题发起人 royhgf
  • 开始时间 开始时间
R

royhgf

Unregistered / Unconfirmed
GUEST, unregistred user!
请问,我用的是三层结构,用的是ClientDataSet控件,如何将我的记录导出为EXCEL文件?急求!!!具体代码。
 
很奇怪 记录导出为EXCEL文件 跟三层有什么关系
 
一個非常快的轉excel<br><br>unit Unit1;<br><br>interface<br><br>uses Classes,Forms,Windows,ExtCtrls,StdCtrls,SConnect,Controls,SysUtils,ComObj,DB,Dialogs;<br><br>Procedure ExportExcelFile(FileName: string; bWriteTitle: Boolean; aDataSet: TDataSet);stdcall;<br><br>var<br>&nbsp; &nbsp;arXlsBegin:array[0..5] of Word=($809, 8, 0, $10, 0, 0);<br>&nbsp; &nbsp;arXlsEnd:array[0..1] of Word = ($0A, 00);<br>&nbsp; &nbsp;arXlsString:array[0..5] of Word = ($204, 0, 0, 0, 0, 0);<br>&nbsp; &nbsp;arXlsNumber:array[0..4] of Word = ($203, 14, 0, 0, 0);<br>&nbsp; &nbsp;arXlsInteger:array[0..4] of Word = ($27E, 10, 0, 0, 0);<br>&nbsp; &nbsp;arXlsBlank:array[0..4] of Word = ($201, 6, 0, 0, $17);<br><br>implementation<br><br>Procedure ExportExcelFile(FileName: string; bWriteTitle: Boolean; aDataSet: TDataSet);<br>var<br>&nbsp; &nbsp;i:integer;<br>&nbsp; &nbsp;Col,row: word;<br>&nbsp; &nbsp;ABookMark: TBookMark;<br>&nbsp; &nbsp;aFileStream: TFileStream;<br>procedure incColRow;<br>begin<br><br>&nbsp; &nbsp;if Col = ADataSet.FieldCount - 1 then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; Inc(Row);<br>&nbsp; &nbsp; &nbsp; Col :=0;<br>&nbsp; &nbsp;end<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; Inc(Col);<br>end;<br><br>procedure WriteStringCell(AValue: string);<br>var<br>&nbsp; &nbsp;L: Word;<br>begin<br>&nbsp; &nbsp;L := Length(AValue);<br>&nbsp; &nbsp;arXlsString[1] := 8 + L;<br>&nbsp; &nbsp;arXlsString[2] := Row;<br>&nbsp; &nbsp;arXlsString[3] := Col;<br>&nbsp; &nbsp;arXlsString[5] := L;<br>&nbsp; &nbsp;aFileStream.WriteBuffer(arXlsString, SizeOf(arXlsString));<br>&nbsp; &nbsp;aFileStream.WriteBuffer(Pointer(AValue)^, L);<br>&nbsp; &nbsp;IncColRow;<br>end;<br><br>procedure WriteIntegerCell(AValue: integer);//<br>var<br>&nbsp; &nbsp;V: Integer;<br>begin<br>&nbsp; &nbsp;arXlsInteger[2] := Row;<br>&nbsp; &nbsp;arXlsInteger[3] := Col;<br>&nbsp; &nbsp;aFileStream.WriteBuffer(arXlsInteger, SizeOf(arXlsInteger));<br>&nbsp; &nbsp;V := (AValue shl 2) or 2;<br>&nbsp; &nbsp;aFileStream.WriteBuffer(V, 4);<br>&nbsp; &nbsp;IncColRow;<br>end;<br><br>procedure WriteFloatCell(AValue: double);<br>begin<br>&nbsp; &nbsp;arXlsNumber[2] := Row;<br>&nbsp; &nbsp;arXlsNumber[3] := Col;<br>&nbsp; &nbsp;aFileStream.WriteBuffer(arXlsNumber, SizeOf(arXlsNumber));<br>&nbsp; &nbsp;aFileStream.WriteBuffer(AValue, 8);<br>&nbsp; &nbsp;IncColRow;<br>end;<br><br>begin<br>&nbsp; &nbsp;if FileExists(FileName) then DeleteFile(FileName);<br>&nbsp; &nbsp; &nbsp; &nbsp;aFileStream := TFileStream.Create(FileName, fmCreate);<br>&nbsp; &nbsp;Try<br>&nbsp; &nbsp; &nbsp; aFileStream.WriteBuffer(arXlsBegin, SizeOf(arXlsBegin));<br>&nbsp; &nbsp; &nbsp; Col := 0; Row := 0;<br>&nbsp; &nbsp; &nbsp; if bWriteTitle then &nbsp; &nbsp; &nbsp; &nbsp; //寫Excel的Column<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for i := 0 to aDataSet.FieldCount - 1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WriteStringCell(aDataSet.Fields.FieldName); &nbsp;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; aDataSet.DisableControls;<br>&nbsp; &nbsp; &nbsp; ABookMark := aDataSet.GetBookmark;<br>&nbsp; &nbsp; &nbsp; aDataSet.First;<br>&nbsp; &nbsp; &nbsp; while not aDataSet.Eof do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for i := 0 to aDataSet.FieldCount - 1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ADataSet.Fields.DataType of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ftSmallint, ftInteger, ftWord, ftAutoInc, ftBytes:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WriteIntegerCell(aDataSet.Fields.AsInteger);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ftFloat, ftCurrency, ftBCD:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WriteFloatCell(aDataSet.Fields.AsFloat)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WriteStringCell(aDataSet.Fields.AsString);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;aDataSet.Next;<br>&nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; AFileStream.WriteBuffer(arXlsEnd, SizeOf(arXlsEnd));<br>&nbsp; &nbsp; &nbsp; if ADataSet.BookmarkValid(ABookMark) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;aDataSet.GotoBookmark(ABookMark);<br>&nbsp; &nbsp;Finally<br>&nbsp; &nbsp; &nbsp; ADataSet.EnableControls;<br>&nbsp; &nbsp;end;<br>end;<br><br>end.
 
導Excel和三層結構有關係嗎?
 
回楼上的,没有关系。<br>建议楼主用cxGrid<br>在引用cxExportGrid4Link单元后。<br>一句话ExportGrid4ToExcel(FileName,cxGridName,true,true,False); <br>就直接导到EXCEL中了,方便吧。。。
 
进我的笔记里看吧..什么都有....
 
用DBGridEh转Excel也非常方便
 
用F1BOOK吧.导出数据集里的数据为EXCEL,很方便,但是数据量大时就出错,直接写调用的COM组件吧.
 
Function DaoChuExcel(TmpFileName,Title:string;Tempdataset:Tdataset;tmpdbgrid:tdbgrid):boolean;<br><br><br><br><br>Function DaoChuExcel(TmpFileName,Title:string;Tempdataset:Tdataset;tmpdbgrid:tdbgrid):boolean;<br>var i,k,row,column,xx,ColNum:integer;<br>&nbsp; &nbsp; xlapp,range1:olevariant;<br>&nbsp; &nbsp; str:string;<br>begin<br>&nbsp; result:=false;<br>&nbsp; ColNum:=0;<br>&nbsp; k:=1;<br>&nbsp; try<br>&nbsp; &nbsp; xlapp:=createoleobject('excel.application');<br>&nbsp; except<br>&nbsp; &nbsp; application.MessageBox('你没有安装Excel或安装不正确?所以不能导出','提示',mb_ok+MB_ICONQUESTION);<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br>&nbsp; try<br>&nbsp; &nbsp; Fguage:=TFguage.Create(nil);<br>&nbsp; &nbsp; Fguage.Show;<br>&nbsp; &nbsp; Fguage.Update;<br>&nbsp; &nbsp; Tempdataset.DisableControls;<br>&nbsp; &nbsp; Fguage.Gauge1.MaxValue := TempDataSet.RecordCount;<br>&nbsp; &nbsp; Fguage.Gauge1.Progress :=0;<br>&nbsp; &nbsp; Fguage.Update;<br>&nbsp; &nbsp; xlapp.Visible:=false;<br>&nbsp; &nbsp; xlapp.application.Caption := Title;<br>&nbsp; &nbsp; for i:=1 to tmpdbgrid.FieldCount-1 do<br>&nbsp; &nbsp; &nbsp; if tmpdbgrid.Columns[i-1].Visible then<br>&nbsp; &nbsp; &nbsp; &nbsp; ColNum:=ColNum+1;<br>&nbsp; &nbsp; xx:=ColNum-26;<br>&nbsp; &nbsp; if xx&gt;0 then<br>&nbsp; &nbsp; &nbsp; str:='A'+chr(65+xx)<br>&nbsp; &nbsp; else str:=chr(65+ColNum);<br>&nbsp; &nbsp; xlapp.Workbooks.Add(xlwbatworksheet);<br>&nbsp; &nbsp; xlapp.workbooks[1].worksheets[1].name:=Title;<br>&nbsp; &nbsp; range1:=xlapp.workbooks[1].worksheets[Title].range['a2:'+str+inttostr(Tempdataset.RecordCount+2)];<br>&nbsp; &nbsp; range1.borders.linestyle:=xlcontinuous;<br>&nbsp; &nbsp; xlapp.Cells.Item[1,5]:=Title;<br>&nbsp; &nbsp; for i:=1 to Tmpdbgrid.FieldCount do<br>&nbsp; &nbsp; &nbsp; if Tmpdbgrid.Columns[i-1].Visible then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; xlapp.Cells[2,k]:=Tmpdbgrid.Columns[i-1].Title.caption;<br>&nbsp; &nbsp; &nbsp; &nbsp; k:=k+1;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; row:=3;<br>&nbsp; &nbsp; TempDataset.First;<br>&nbsp; &nbsp; while not TempDataset.Eof do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; column:=1;<br>&nbsp; &nbsp; &nbsp; for i:=1 to tmpdbgrid.FieldCount do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if tmpdbgrid.Columns[i-1].Visible then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xlapp.Cells[row,column]:=TempDataset.fieldbyname(tmpdbgrid.Columns[i-1].FieldName).AsString;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; column:=column+1;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; row:=row+1;<br>&nbsp; &nbsp; &nbsp; TempDataset.Next;<br>&nbsp; &nbsp; &nbsp; Fguage.Gauge1.Progress :=Fguage.Gauge1.Progress+1 ;<br>&nbsp; &nbsp; &nbsp; Fguage.Update;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; xlapp.workbooks[1].worksheets[1].saveas(tmpFileName);<br>&nbsp; &nbsp; TempDataset.EnableControls;<br>&nbsp; &nbsp; xlapp.quit;<br>&nbsp; &nbsp; application.MessageBox('数据导出完毕','提示',mb_ok+mb_iconinformation);<br>&nbsp; finally<br>&nbsp; &nbsp; Fguage.Close;<br>&nbsp; &nbsp; Fguage.Free;<br>&nbsp; &nbsp; Fguage:=nil;<br>&nbsp; end;<br>end;
 
建议使用cxGrid,然后use &nbsp;cxExportGrid4Link;<br>ExportGrid4ToExcel(FileName,cxGridName); <br>一句搞定
 
如果用DbGridEh 引用DBGridEhImpExp单元<br>SaveDBGridEhToExportFile(TDBGridEhExportAsXLS,DBGridEh1,FileName,true)
 
我有现成的DEMO,之种CASE也搞了好多次,直接DBGRID导出到EXCEL的.也有通过数据集的,不过后一种必需保证你用的是ADO
 
很简单的,BS老是用第三方控件的人.
 
后退
顶部