




已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
delphi dbgrid 導(dǎo)出Excel表 / 利用剪貼板,速度很快!適合裝有Excel的機(jī)器/ USES Clipbrd,ComObj; procedure TForm1.Button1Click(Sender: TObject); var str:string; i:Integer; excelapp,sheet:Variant; begin / lbl2.Caption:=DateTimeToStr(Now); str:=; dbgrd1.DataSource.DataSet.DisableControls; for i:=0 to dbgrd1.DataSource.DataSet.FieldCount-1 do str:=str+dbgrd1.DataSource.DataSet.fieldsi.DisplayLabel+char(9); str:=str+#13; dbgrd1.DataSource.DataSet.First; while not(dbgrd1.DataSource.DataSet.eof) do begin for i:=0 to dbgrd1.DataSource.DataSet.FieldCount-1 do str:=str+dbgrd1.DataSource.DataSet.Fieldsi.AsString+char(9); str:=str+#13; dbgrd1.DataSource.DataSet.next; lbl1.Caption:=IntToStr(dbgrd1.DataSource.DataSet.RecNo); Application.ProcessMessages; end;/end while dbgrd1.DataSource.DataSet.EnableControls; clipboard.Clear; Clipboard.Open; Clipboard.AsText:=str; Clipboard.Close; excelapp:=createoleobject(excel.application); excelapp.workbooks.add(1); / excelapp.workbooks.add(-4167); sheet:=excelapp.workbooks1.worksheets1; :=sheet1; sheet.paste; Clipboard.Clear; / sheet.columns.font.Name:=宋體; / sheet.columns.font.size:=9; / sheet.Columns.AutoFit; excelapp.visible:=true; / lbl3.Caption:=DateTimeToStr(Now); end; /利用TStringList,速度很快!適合沒有裝Excel的機(jī)器/ procedure TForm1.Button1Click(Sender: TObject); var s:TStringList; str:string; i:Integer; begin / lbl1.Caption:=DateTimeToStr(Now); str:=; dbgrd1.DataSource.DataSet.DisableControls; for i:=0 to dbgrd1.DataSource.DataSet.FieldCount-1 do str:=str+dbgrd1.DataSource.DataSet.fieldsi.DisplayLabel+char(9); str:=str+#13; dbgrd1.DataSource.DataSet.First; while not(dbgrd1.DataSource.DataSet.eof) do begin for i:=0 to dbgrd1.DataSource.DataSet.FieldCount-1 do str:=str+dbgrd1.DataSource.DataSet.Fieldsi.AsString+char(9); str:=str+#13; dbgrd1.DataSource.DataSet.next; / lbl3.Caption:=IntToStr(dbgrd1.DataSource.DataSet.RecNo); / Application.ProcessMessages; end;/end while dbgrd1.DataSource.DataSet.EnableControls; s:=TStringList.Create; s.Add(str); s.SaveToFile(c:temp.xls);/保存到c:temp.xls s.Free; / lbl2.Caption:=DateTimeToStr(Now); end; /*(Delphi)Excel的快速導(dǎo)入*(Delphi)Excel的快速導(dǎo)入/怎樣可以提高EXCEL的導(dǎo)出速度?uses ADODB,excel97,adoint;function TForm1.ExportToExcel: Boolean;varxlApp,xlBook,xlSheet,xlQuery: Variant;adoConnection,adoRecordset: Variant;beginadoConnection := CreateOleObject(ADODB.Connection);adoRecordset := CreateOleObject(ADODB.Recordset);adoConnection.Open(Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:Tree.mdb;Persist Security Info=False);adoRecordset.CursorLocation := adUseClient;adoRecordset.Open(SELECT * FROM tree,adoConnection,1,3);tryxlApp := CreateOleObject(Excel.Application);xlBook := xlApp.Workbooks.Add;xlSheet := xlBook.Worksheetssheet1;/設(shè)置這一列為 文本列 ,讓 00123 正確顯示, 而不是自動轉(zhuǎn)換為123xlSheet.ColumnsC:C.NumberFormatLocal := ;xlApp.Visible := True;/把查詢結(jié)果導(dǎo)入EXCEL數(shù)據(jù)xlQuery := xlSheet.QueryTables.Add(adoRecordset,xlSheet.RangeA1); /關(guān)鍵是這一句xlQuery.FieldNames := True;xlQuery.RowNumbers := False;xlQuery.FillAdjacentFormulas := False;xlQuery.PreserveFormatting := True;xlQuery.RefreshOnFileOpen := False;xlQuery.BackgroundQuery := True;/xlQuery.RefreshStyle := xlInsertDeleteCells;xlQuery.SavePassword := True;xlQuery.SaveData := True;xlQuery.AdjustColumnWidth := True;xlQuery.RefreshPeriod := 0;xlQuery.PreserveColumnInfo := True;xlQuery.FieldNames := True;xlQuery.Refresh;xlBook.SaveAs(d:fromD.xls,xlNormal,False,False);finallyif not VarIsEmpty(XLApp) then beginXLApp.displayAlerts:=false;XLApp.ScreenUpdating:=true;XLApp.quit;end;end;end;/procedure saveToExcel();varEclapp,workbook:variant;i,n:integer;beginif not adoquery1.Active then exit;if adoquery1.RecordCount=0 then exit;if application.MessageBox(確認(rèn)導(dǎo)出excel表嗎?,提示,mb_okcancel+mb_iconinformation)=idcancel then exit;Eclapp := createoleobject(Excel.Application);Eclapp.workbooks.add;for i:=0 to dbgrid2.FieldCount-1 dobeginEclapp.cells1,i+1:=dbgrid2.Columnsi.Title.Caption;end;Eclapp.cells1,5:=簽字;adoquery1.First;n:=2;while not adoquery1.Eof dobegineclapp.cellsn,1 := adoquery1.Fields0.AsString;eclapp.cellsn,2 := adoquery1.Fields1.AsString;eclapp.cellsn,3 := adoquery1.Fields2.AsString;eclapp.cellsn,4 := adoquery1.Fields4.AsString;eclapp.cellsn,6 := ;inc(n);adoquery1.Next;end;eclapp.cellsn,1 := 滿足條件記錄的總數(shù)為:+inttostr(adoquery1.RecordCount)+條;application.MessageBox(數(shù)據(jù)導(dǎo)出完成!,提示,mb_ok+mb_iconinformation);eclapp.visible := true;end;方法二procedure CopyDbDataToExcel(Args: array of const); var iCount, jCount: Integer; XLApp: Variant; Sheet,range: Variant; I: Integer; begin Screen.Cursor := crHourGlass; if not VarIsEmpty(XLApp) then begin XLApp.DisplayAlerts := False; XLApp.Quit; VarClear(XLApp); end;try XLApp:=CreateOleObject(Excel.Application); except Screen.Cursor := crDefault; Exit; end;XLApp.WorkBooks.Add; XLApp.SheetsInNewWorkbook := High(Args) + 1;for I := Low(Args) to High(Args) do begin XLApp.WorkBooks1.WorkSheetsI+1.Name := TDBGrid(ArgsI.VObject).Name; Sheet := XLApp.Workbooks1.WorkSheetsTDBGrid(ArgsI.VObject).Name;if not TDBGrid(ArgsI.VObject).DataSource.DataSet.Active then begin Screen.Cursor := crDefault; Exit; end; TDBGrid(ArgsI.VObject).DataSource.DataSet.first; for iCount := 0 to TDBGrid(ArgsI.VObject).Columns.Count - 1 do range:=sheet.rangesheet.cells1,1,sheet.cells1,iCount + 1; range.select; range.merge; sheet.cells1,1:=+fqueryhuman.dbedit2.text+個(gè)人報(bào)銷記錄(普通報(bào)銷、特殊報(bào)銷)查詢; jCount :=2; for iCount := 0 to TDBGrid(ArgsI.VObject).Columns.Count - 1 do Sheet.Cells2, iCount + 1:=TDBGrid(ArgsI.VObject).Columns.ItemsiCount.Title.Caption; while not TDBGrid(ArgsI.VObject).DataSource.DataSet.Eof do begin for iCount := 0 to TDBGrid(ArgsI.VObject).Columns.Count - 1 do Sheet.CellsjCount + 1, iCount + 1 := TDBGrid(ArgsI.VObject).Columns.ItemsiCount.Field.AsString;Inc(jCount); TDBGrid(ArgsI.VObject).DataSource.DataSet.Next; end; XlApp.Visible := True; end; Screen.Cursor := crDefault; end;方法三delphi導(dǎo)入/導(dǎo)出excel2008年03月02日 星期日 16:39從Excel文件中,導(dǎo)入數(shù)據(jù)到SQL數(shù)據(jù)庫中,很簡單,直接用下面的語句:-如果接受數(shù)據(jù)導(dǎo)入的表已經(jīng)存在insert into 表 select * fromOPENROWSET(MICROSOFT.JET.OLEDB.4.0,Excel 5.0;HDR=YES;DATABASE=c:test.xls,sheet1$)-如果導(dǎo)入數(shù)據(jù)并生成表select * into 表 fromOPENROWSET(MICROSOFT.JET.OLEDB.4.0,Excel 5.0;HDR=YES;DATABASE=c:test.xls,sheet1$)-如果從SQL數(shù)據(jù)庫中,導(dǎo)出數(shù)據(jù)到Excel,如果Excel文件已經(jīng)存在,而且已經(jīng)按照要接收的數(shù)據(jù)創(chuàng)建好表頭,就可以簡單的用:insert into OPENROWSET(MICROSOFT.JET.OLEDB.4.0,Excel 5.0;HDR=YES;DATABASE=c:test.xls,sheet1$)select * from 表-如果Excel文件不存在,也可以用BCP來導(dǎo)成類Excel的文件,注意大小寫:-導(dǎo)出表的情況EXEC master.xp_cmdshell bcp 數(shù)據(jù)庫名.dbo.表名 out c:test.xls /c -/S服務(wù)器名 /U用戶名 -P密碼-導(dǎo)出查詢的情況EXEC master.xp_cmdshell bcp SELECT au_fname, au_lname FROM pubs.authors ORDER BY au_lname queryout c:test.xls /c -/S服務(wù)器名 /U用戶名 -P密碼-下面是導(dǎo)出真正Excel文件的方法:if exists (select * from dbo.sysobjects where id = object_id(Ndbo.p_exporttb) and OBJECTPROPERTY(id, NIsProcedure) = 1)drop procedure dbo.p_exporttbGOcreate proc p_exporttbtbname sysname, -要導(dǎo)出的表名path nvarchar(1000), -文件存放目錄fname nvarchar(250)= -文件名,默認(rèn)為表名asdeclare err int,src nvarchar(255),desc nvarchar(255),out intdeclare obj int,constr nvarchar(1000),sql varchar(8000),fdlist varchar(8000)-參數(shù)檢測if isnull(fname,)= set fname=tbname+.xls-檢查文件是否已經(jīng)存在if right(path,1) set path=path+create table #tb(a bit,b bit,c bit)set sql=path+fnameinsert into #tb exec master.xp_fileexist sql-數(shù)據(jù)庫創(chuàng)建語句set sql=path+fnameif exists(select 1 from #tb where a=1)set constr=DRIVER=Microsoft Excel Driver (*.xls);DSN=;READONLY=FALSE+;CREATE_DB=+sql+;DBQ=+sqlelseset constr=Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;HDR=YES+;DATABASE=+sql+-連接數(shù)據(jù)庫exec err=sp_oacreate adodb.connection,obj outif err0 goto lberrexec err=sp_oamethod obj,open,null,constrif err0 goto lberr-創(chuàng)建表的SQLselect sql=,fdlist=select fdlist=fdlist+,++,sql=sql+,++ +casewhen like %charthen case when a.length255 then memoelse text(+cast(a.length as varchar)+) endwhen like %int or =bit then intwhen like %datetime then datetimewhen like %money then moneywhen like %text then memoelse endFROM syscolumns a left join systypes b on a.xtype=b.xusertypewhere not in(image,uniqueidentifier,sql_variant,varbinary,binary,timestamp)and object_id(tbname)=idselect sql=create table +tbname+(+substring(sql,2,8000)+),fdlist=substring(fdlist,2,8000)exec err=sp_oamethod obj,execute,out out,sqlif err0 goto lberrexec err=sp_oadestroy obj-導(dǎo)入數(shù)據(jù)set sql=openrowset(MICROSOFT.JET.OLEDB.4.0,Excel 8.0;HDR=YES;IMEX=1;DATABASE=+path+fname+,+tbname+$)exec(insert into +sql+(+fdlist+) select +fdlist+ from +tbname)returnlberr:exec sp_oageterrorinfo 0,src out,desc outlbexit:select cast(err as varbinary(4) as 錯(cuò)誤號,src as 錯(cuò)誤源,desc as 錯(cuò)誤描述select sql,constr,fdlistgoif exists (select * from dbo.sysobjects where id = object_id(Ndbo.p_exporttb) and OBJECTPROPERTY(id, NIsProcedure) = 1)drop procedure dbo.p_exporttbGOcreate proc p_exporttbsqlstr varchar(8000), -查詢語句,如果查詢語句中使用了order by ,請加上top 100 percentpath nvarchar(1000), -文件存放目錄fname nvarchar(250), -文件名sheetname varchar(250)= -要創(chuàng)建的工作表名,默認(rèn)為文件名asdeclare err int,src nvarchar(255),desc nvarchar(255),out intdeclare obj int,constr nvarchar(1000),sql varchar(8000),fdlist varchar(8000)-參數(shù)檢測if isnull(fname,)= set fname=temp.xlsif isnull(sheetname,)= set sheetname=replace(fname,.,#)-檢查文件是否已經(jīng)存在if right(path,1) set path=path+create table #tb(a bit,b bit,c bit)set sql=path+fnameinsert into #tb exec master.xp_fileexist sql-數(shù)據(jù)庫創(chuàng)建語句set sql=path+fnameif exists(select 1 from #tb where a=1)set constr=DRIVER=Microsoft Excel Driver (*.xls);DSN=;READONLY=FALSE+;CREATE_DB=+sql+;DBQ=+sqlelseset constr=Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;HDR=YES+;DATABASE=+sql+-連接數(shù)據(jù)庫exec err=sp_oacreate adodb.connection,obj outif err0 goto lberrexec err=sp_oamethod obj,open,null,constrif err0 goto lberr-創(chuàng)建表的SQLdeclare tbname sysnameset tbname=#tmp_+convert(varchar(38),newid()set sql=select * into +tbname+ from(+sqlstr+) aexec(sql)select sql=,fdlist
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2022年廣東省中考地理真題及答案
- 企事業(yè)單位場地?zé)o償使用與員工活動協(xié)議
- 草牧場承包權(quán)流轉(zhuǎn)與草原生態(tài)保護(hù)項(xiàng)目合同
- 國際貿(mào)易跨境物流全程服務(wù)質(zhì)量承諾書
- 工業(yè)工程教學(xué)課件
- 2024-2025學(xué)年湖南師大附中高一下學(xué)期第二次大練習(xí)地理試題及答案
- 節(jié)慶活動教學(xué)課件
- 冷藏車運(yùn)輸過程中的冷鏈物流運(yùn)輸車輛冷卻系統(tǒng)性能檢測考核試卷
- 安全通道及疏散路線設(shè)置考核試卷
- 農(nóng)產(chǎn)品加工產(chǎn)業(yè)鏈國際合作案例分析考核試卷
- 湖北2024年湖北省高級人民法院及直屬法院招聘雇員制審判輔助人員22人筆試歷年典型考題及考點(diǎn)附答案解析
- 2024年上海市中考數(shù)學(xué)真題試卷及答案解析
- 生物-云南省昆明市2023-2024學(xué)年高二下學(xué)期期末質(zhì)量檢測試題和答案
- 跨境電商海外倉租賃協(xié)議
- 《污水處理企業(yè)安全生產(chǎn)標(biāo)準(zhǔn)化建設(shè)規(guī)范》(T-GDPAWS 11-2022)
- 繪畫基礎(chǔ)(師范教育專業(yè)繪畫的基礎(chǔ)知識和創(chuàng)作方法)全套教學(xué)課件
- 湖南省長沙市2024年七年級下學(xué)期數(shù)學(xué)期末考試試卷附答案
- 2023年中國美術(shù)學(xué)院輔導(dǎo)員真題
- 2024年江西石城縣城投集團(tuán)與贛江源農(nóng)業(yè)發(fā)展有限公司招聘筆試參考題庫含答案解析
- 《經(jīng)濟(jì)學(xué)基礎(chǔ)》課后題答案
- 沖壓作業(yè)安全管理措施
評論
0/150
提交評論