Friday, April 25, 2014

Export Delphi Stringgrid into EMF file

Delphi Tstringgrid can be exported into EMF and then pasted into MSWORD. Delphi stringgrids are more flexible than MS WORD tables. Bellow is the code  for doing that.

procedure StringGrid2EMF(grid : Tstringgrid; Horlines,Verlines : boolean; filename : string);
var i,j,w,h,h1,w1 : integer; m : tmetafile;  c: tmetafilecanvas;  r : trect;
begin m:=tmetafile.Create;w:=0; h:=0;
  with grid do for i:=0 to colcount-1 do w:=w+ColWidths[i]+gridlinewidth;
  with grid do for i:=0 to rowcount-1 do h:=h+RowHeights[i]+gridlinewidth;
  m.Width:=w; m.Height:=h;
  c:=tmetafilecanvas.Create(m,0); c.Font:=grid.Font;
  c.Brush.Color:=grid.Color; c.Brush.Style:=bssolid;
  c.FillRect(rect(0,0,m.width,m.height));
  with grid do
  for i:=0 to colcount-1 do
  for j:=0 to rowcount-1 do
  begin r:=grid.CellRect(i,j);
        if (i=0)or(j=0) then
        c.Brush.Color:=grid.FixedColor
         else
         c.Brush.Color:=grid.Color;
         c.FillRect(r);
        c.TextOut(r.left+3,r.top+1,cells[i,j])
  end;
  c.pen.Color:=$b0b0b0;
  w1:=0; h1:=0;
  with grid do
  begin c.MoveTo(0,0); c.LineTo(w,0); //first hor line
     for i:=0 to rowcount-1 do
     if HorLines then
     begin h1:=h1+RowHeights[i]+gridlinewidth;
         c.MoveTo(0,h1);
         c.LineTo(w,h1);
     end;
     c.MoveTo(0,h); c.LineTo(w,h); //do last hor line
     c.MoveTo(0,0); c.LineTo(0,h); //first ver line
     if VerLines then
     for i:=0 to colcount do
     begin w1:=w1+colwidths[i]+gridlinewidth;
           c.MoveTo(w1,0);
           c.LineTo(w1,h);
     end;
     c.MoveTo(w,0); c.LineTo(w,h) //last ver line
  end;
  c.Free; clipboard.Assign(m);  m.SaveToFile(filename);
  m.free;
  showmessage('Meta file saved and put in clipboard')
end;

No comments:

Post a Comment