Insert an image into sheet

Questions and answers on how to use XLSReadWriteII 3/4/5.
Post Reply
graphicequaliser
Posts: 6
Joined: Wed Feb 06, 2013 9:44 am

Insert an image into sheet

Post by graphicequaliser »

I use the following code to insert an image held in the filename "imgnam" into row position "rw" in my sheet :-

Code: Select all

    TMSOPicture* msoPict=XLSRW1->MSOPictures->Add();
    msoPict->Filename=imgnam;
    TDrwPicture *Pict=XLSRW1->Sheets->Items[0]->DrawingObjects->Pictures->Add();
    Pict->PictureId=XLSRW1->MSOPictures->Count;
    Pict->Col1=0; Pict->Row1=rw; Pict->Col2=5; Pict->Row2=rw+9;
This scales the image into the area defined by Pict->Col1, Pict->Row1, Pict->Col2 and Pict->Row2 and the clear PNG image becomes blurry because of this scaling. I do not want the image scaled at all, and I want it to be 100% its original size starting at row rw col 0. How do I do this?

P.S. In Excel itself, when I click on the blurry scaled image, a picture toolbar appears which has a "Reset Picture" button on it. When I click this, the image reverts to its original clear form and this is the result I want.
graphicequaliser
Posts: 6
Joined: Wed Feb 06, 2013 9:44 am

Re: Insert an image into sheet

Post by graphicequaliser »

This seems to work, where "chwid" is the image width and "chhei" is the image height :-

Code: Select all

    int ii,jj,lpx; TSheet *mjsh=XLSRW1->Sheets->Items[0];
    TMSOPicture* msoPict=XLSRW1->MSOPictures->Add();
    msoPict->LoadFromFile(imgnam);
    TDrwPicture* drwPict=mjsh->DrawingObjects->Pictures->Add();
    drwPict->PictureId=XLSRW1->MSOPictures->Count; drwPict->Col1=0; drwPict->Row1=rw;
    drwPict->Col1Offset=0; drwPict->Row1Offset=0;
    jj=0; ii=0;
    while (jj<chhei)
    {
      mjsh->Rows->AddIfNone(0); lpx=mjsh->Rows->Items[0]->PixelHeight; if (jj+lpx>chhei) break;
      jj+=lpx; ++ii;
    }
    rw+=ii; drwPict->Row2=rw; if (jj<chhei) drwPict->Row2Offset=(double)(chhei-jj)/lpx;
    jj=0; ii=0;
    while (jj<chwid)
    {
      mjsh->Columns->AddIfNone(ii,1); lpx=mjsh->Columns->Items[ii]->PixelWidth; if (jj+lpx>chwid) break;
      jj+=lpx; ++ii;
    }
    drwPict->Col2=ii; if (jj<chwid) drwPict->Col2Offset=(double)(chwid-jj)/lpx;
I have fixed row heights and variable column widths. I step through height and width until I would overflow the image width and height and then set offsets to accomodate the remainder (overflow beyond a whole row or column height or width). 8)
Mojoala
Posts: 19
Joined: Wed Feb 29, 2012 5:54 pm

Re: Insert an image into sheet

Post by Mojoala »

Can someone convert this to delphi code please?
Mojoala
Posts: 19
Joined: Wed Feb 29, 2012 5:54 pm

Re: Insert an image into sheet

Post by Mojoala »

Here is Delphi Code. It is a passed in procedure ( We have a lot of excel exports, so it was refactored down to a single line call and have one routine do all of the coding) LOGO is a Global varible:
Procedure LoadPic(var aXLS: TXLSReadWriteII4; var cl, rw, ImgWid, ImgHgt, ts : Integer);
var ii, jj, lpx: Integer;
aSht : TSheet;
msoPict : TMSOPicture;
drwPict : TDrwPicture;
begin
aSht := aXLS.Sheets.Items[ts];
msoPict := aXLS.MSOPictures.Add;
msoPict.LoadFromFile(LOGO);

drwPict := aSht.DrawingObjects.Pictures.Add;
drwPict.PictureId := aXLS.MSOPictures.Count;

drwPict.Col1 := cl;
drwPict.Row1 := rw;

drwPict.Col1Offset := 0;
drwPict.Row1Offset := 0;

jj := 0;
ii := 0;

while jj < ImgHgt do
begin
aSht.Rows.AddIfNone(ts);
lpx := aSht.Rows.Items[ts].PixelHeight;
if (jj + lpx) > ImgHgt then break;
Inc(jj, lpx);
inc(ii);
end;

inc(rw, ii);
drwPict.Row2 := rw;
if jj < ImgHgt then
drwPict.Row2Offset := ( ImgHgt - jj ) / lpx;
jj := 0;
ii := 0;

while jj < ImgWid do
begin
aSht.Columns.AddIfNone(ii, 1);
lpx := aSht.Columns.Items[ii].PixelWidth;
if (jj + lpx) > ImgWid then break;
inc(jj, lpx);
inc(ii);
end;

drwPict.Col2 := ii;
if jj < ImgWid then
drwPict.Col2Offset := (ImgWid-jj) / lpx;
end;
Post Reply