Arsip: Export fild Image dari SQLServer 2005 ke Excell

 
user image
more 8 years ago

mrzie05

Dear Para Master Delphi, Mohon masukkan dan pencerahannya tentang Coding mengeksport Data Image di SQL Server 2005 kedalam Excell dalam bentuk Gambar utuh. Mohon masukkannya dan Terimakasih :oops:
user image
more 8 years ago

halimanh

Di db, ada tabel 'induk' dengan column 'image' type image. Kode dibawah akan connect ke db, ambil column image (jpg), create jpg image, copy ke clipboard, create excel app & worksheet, paste dari clipboard ke excel. Kode ini cuma ambil satu jpg image doang dan tampilkan ke excel.

procedure TForm1.btn3Click(Sender: TObject);
var
  oExcel, ws : OleVariant;
  clipboard : TClipboard;
  jpg  : TJPEGImage;
  adoQry : TADOQuery;
  memStream : TMemoryStream;
begin
  try
    adoQry := TADOQuery.Create(nil);
    adoQry.ConnectionString := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Test;Data Source=BLANK';
    adoQry.SQL.Clear;
    adoQry.SQL.Add('select image from induk;'); //image adalah column di database (type Image).
    adoQry.Open;
    clipboard := TClipboard.Create;
    jpg := TJPEGImage.Create;
    memStream := TMemoryStream.Create;
    TBlobField(adoQry.FieldByName('image')).savetoStream(memStream);
    memStream.Seek(0, soFromBeginning);
    memStream.Position := 0;
    jpg.LoadFromStream(memStream);
    clipboard.Assign(jpg);
    oExcel := CreateOleObject('excel.application');
    oExcel.visible := True;
    oExcel.Workbooks.add;
    ws := oExcel.Worksheets.item['Sheet1'];
    ws.activate;
    ws.cells.item[1,3].value := 'Halo...excel nih';
    ws.paste;
  finally
    clipboard.Free;
    jpg.Free;
    adoQry.Close;
    adoQry.Free;
    memStream.Free;
  end;
end;
user image
more 8 years ago

halimanh

di baris 34 ditambahin

ws.cells.item[2,6].select;
biar gambarnya nggak nutupin kata 'Halo...excel nih'
user image
more 8 years ago

mrzie05

@halimanh: di baris 34 ditambahin

ws.cells.item[2,6].select;
biar gambarnya nggak nutupin kata 'Halo...excel nih'
Mas @halimanh, Makasih Atass Repleynya... Saya coba dulu ya Mass.... :D :idea:
user image
more 8 years ago

mrzie05

@mrzie05: [quote:4db7719ad7="halimanh"]di baris 34 ditambahin

ws.cells.item[2,6].select;
biar gambarnya nggak nutupin kata 'Halo...excel nih'[/quote:4db7719ad7] Mas @halimanh, Makasih Atass Repleynya... Saya coba dulu ya Mass.... :D :idea:
Mas @halimanh, ada error nih.... [Error] Unit2.pas(30): Undeclared identifier: 'TClipboard' Mohon Masukkannya....
user image
more 8 years ago

mrzie05

@mrzie05: Mas @halimanh, ada error nih.... [Error] Unit2.pas(30): Undeclared identifier: 'TClipboard' Mohon Masukkannya....
Yg diatas Sudah Bisa, di tambahkan di : uses --> jpeg,clipbrd; Muncul baru lg : " Access violation at address 00485ED0 in Modul 'Project2.exe'. Read of address 00000258 " Kenapa ya Mas @halimanh... Maaf banyak tanya
user image
more 8 years ago

halimanh

Pake debugger dong. Dipasang breakpoint aja (F5), jalankan program (F9), terus F8 sampe program error, kan ketauan errornya di baris berapa.
user image
more 8 years ago

mrzie05

@halimanh: Pake debugger dong. Dipasang breakpoint aja (F5), jalankan program (F9), terus F8 sampe program error, kan ketauan errornya di baris berapa.
Akhirnya bisa juga Mas Halimanh, Terimakasih Atass Supportnya... Bagi yg berminat, berikut listing Program yg dikirim oleh mass Halimah yg sudah saya modifikasi :

unit Unit2;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, StdCtrls,jpeg,clipbrd,Comobj;
type
  TForm1 = class(TForm)
    Button1: TButton;
    adoQry: TADOQuery;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
oExcel, ws : OleVariant;
clipboard : Tclipboard;
bmp : TBitmap;
jpg : TJPEGImage;
adoQry : TADOQuery;
memStream : TMemoryStream;
begin
try
adoQry := TADOQuery.Create(nil);
adoQry.ConnectionString := 'Provider=SQLOLEDB.1;Password=dilarangmasuk;Persist Security Info=True;User ID=sa;Initial Catalog=DB_KAR;Data Source=10.23.0.59';
adoQry.SQL.Clear;
adoQry.SQL.Add('select NIK,Foto from Foto where (NIK='+Label1.Caption+') or (NIK='+Label2.Caption+')' ); //image adalah column di database (type Image).
adoQry.Open;
clipboard := TClipboard.Create;
bmp := TBitmap.Create;
//jpg := TJPEGImage.Create;
memStream := TMemoryStream.Create;
TBlobField(adoQry.FieldByName('Foto')).savetoStream(memStream);
memStream.Seek(0, soFromBeginning);
memStream.Position := 0;
//jpg.LoadFromStream(memStream);
bmp.LoadFromStream(memStream);
//clipboard.Assign(jpg);
clipboard.Assign(bmp);
oExcel := CreateOleObject('excel.application');
oExcel.visible := True;
oExcel.Workbooks.add;
ws := oExcel.Worksheets.item['Sheet1'];
ws.activate;
ws.cells.item[1,3].value := 'Halo...excel nih';
ws.cells.item[2,6].select;
ws.paste;
finally
clipboard.Free;
bmp.Free;
//jpg.Free;
adoQry.Close;
adoQry.Free;
memStream.Free;
end;
end;
end.
more ...
  • Pages:
  • 1
Share to
Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com