Excel Copy to DBGrid |
|
Shelin
一般會員 發表:8 回覆:23 積分:6 註冊:2003-09-22 發送簡訊給我 |
由Excel複製資料到DBGrid..並寫入資料庫..
這是將資料寫在SQL Server資料庫的
所以要先建立table...修改server連結路徑
才能順利看到程式結果!! 感謝hagar大哥指導
|
Shelin
一般會員 發表:8 回覆:23 積分:6 註冊:2003-09-22 發送簡訊給我 |
貼出程式碼如下:
unit price1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DB, ADODB, DdeMan, ExtCtrls, Grids, DBGrids, Menus,Clipbrd; type TForm1 = class(TForm) AC1: TADOConnection; Button1: TButton; DBGrid2: TDBGrid; PM2: TPopupMenu; MenuItem1: TMenuItem; Button2: TButton; AT2: TADOTable; StringField1: TStringField; StringField2: TStringField; StringField3: TStringField; StringField4: TStringField; StringField5: TStringField; StringField6: TStringField; StringField7: TStringField; DS2: TDataSource; AQ2: TADOQuery; StringField14: TStringField; StringField15: TStringField; StringField16: TStringField; StringField17: TStringField; StringField18: TStringField; StringField19: TStringField; StringField20: TStringField; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure MenuItem1Click(Sender: TObject); procedure DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); procedure Button2Click(Sender: TObject); private { Private declarations } public procedure PasteClipboardToDBGrid; end; var Form1: TForm1; strStockID:string; implementation {$R *.dfm} //結束程式 procedure TForm1.Button1Click(Sender: TObject); begin close; end; //程式起始 procedure TForm1.FormCreate(Sender: TObject); begin AQ2.SQL.Clear; AQ2.SQL.Add('Delete from DataStock2'); AQ2.Close; AQ2.ExecSQL; end; //選擇貼上 procedure TForm1.MenuItem1Click(Sender: TObject); begin PasteClipboardToDBGrid; end; //執行貼上 procedure TForm1.PasteClipboardToDBGrid; var sl1, sl2: TStringList; i, j,iCol: integer; begin if Clipboard.HasFormat(CF_Text) then begin sl1 := TStringList.Create; sl2 := TStringList.Create; try sl1.Text := Clipboard.AsText; for i := 0 to sl1.Count -1 do begin AT2.Append; sl2.Clear; sl2.Delimiter := #9; sl2.DelimitedText := sl1.Strings[i]; iCol := 0; for j := 0 to sl2.Count -1 do begin AT2.Fields[icol].Text:= sl2.Strings[j]; Inc(iCol); end; AT2.Post; end; finally sl2.Free; sl1.Free; end; end; AQ2.SQL.Clear; AQ2.SQL.Add('select * from DataStock2'); AQ2.Close; AQ2.open; end; //列顏色改變 procedure TForm1.DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var Canvas:TCanvas; begin Canvas := (Sender as TDBGrid).Canvas; if((AQ2.RecNo mod 2)=0) then Canvas.Brush.Color := clMoneyGreen else Canvas.Brush.Color := clSilver; Canvas.FillRect(Rect); DBGrid2.DefaultDrawDataCell(Rect, Column.Field, State); end; //確認下單 procedure TForm1.Button2Click(Sender: TObject); begin AQ2.Edit; AQ2.post; AQ2.SQL.Clear; AQ2.SQL.Add('select * from DataStock2'); AQ2.Close; AQ2.open; showmessage('已存檔'); end; end. |
bmail
一般會員 發表:0 回覆:1 積分:0 註冊:2007-01-10 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |