alexvdb
asked on
C# Excel Shapes.AddPicture
Visual Studeio 2008
I am creating an Excel file and exporting it to PDF. In the beginning of my code, I add a picture to the top left of the worksheet. I want to add the picture when I pagebreak. I need to know how to figure out the "top" value (offset from the top of the page to the current location). Can anyone assist with this?
Thank you in advance for your help.
Alex
I am creating an Excel file and exporting it to PDF. In the beginning of my code, I add a picture to the top left of the worksheet. I want to add the picture when I pagebreak. I need to know how to figure out the "top" value (offset from the top of the page to the current location). Can anyone assist with this?
Thank you in advance for your help.
Alex
Excel.Application myExcelApplication = new Excel.Application();
Excel._Workbook myWorkbook;
Excel._Worksheet mySheet;
Excel.Range myRange;
Excel.Range firstRange;
int currentRow = 0;
mySheet.Shapes.AddPicture(Server.MapPath(imageFile),
Office.Core.MsoTriState.msoFalse,
Office.Core.MsoTriState.msoTrue,
left,
top,
width,
height);
ASKER
This is close. I get the string below in the cell.
System.Drawing.Bitmap
Thank you for your help.
System.Drawing.Bitmap
Thank you for your help.
if the comment helped you, you can rate the comment. Or you want to wait some more time?
ASKER
Well, it hasn't solved my issue yet. I am not getting the image.
are you getting the image in the object "oImage" (in my previous code). Can you do a null check on that? if image is getting, i can check in the function set_Item.
ASKER
I tested for not null and it is not. Is there perhaps something I could do with Range.AutoFormat to format the object as an image?
can you try this?
Excel.Range oRange = (Excel.Range)mySheet.Cells[yourSheetRow,yourSheetCol];
Image oImage = Image.FromFile(Server.MapPath(imageFile));
oRange.set_Item(1,1,oImage);
System.Windows.Forms.Clipboard.SetDataObject(oImage,true);
mySheet.Paste(oRange,imageFile);
ASKER
I got the error below on the SetDataObject.
at System.Windows.Forms.Clipb oard.SetDa taObject(O bject data, Boolean copy, Int32 retryTimes, Int32 retryDelay) at System.Windows.Forms.Clipb oard.SetDa taObject(O bject data, Boolean copy) at InstructorDB.ACICSExcel.Ma keExcelFil e() in c:\TFS\Westwood Faculty\InstructorDB\ACICS Excel.aspx .cs:line 931
at System.Windows.Forms.Clipb
oh, Web-forms cannot use the Clipboard I think. let me check any other options
can you try this?
Excel.Application myExcelApplication = new Excel.Application();
Excel._Workbook myWorkbook;
Excel._Worksheet mySheet;
Excel.Range myRange;
Excel.Range firstRange;
int currentRow = 0;
Excel.Range oRange = (Excel.Range)mySheet.Cells[yourSheetRow,yourSheetCol];
mySheet.Shapes.AddPicture(Server.MapPath(imageFile),
Office.Core.MsoTriState.msoFalse,
Office.Core.MsoTriState.msoTrue,
(float)oRange.Left,
(float)oRange.Top,
width,
height);
ASKER
I'm getting a rather generic error message on the new code.
at InstructorDB.ACICSExcel.Ma keExcelFil e() in c:\TFS\Westwood Faculty\InstructorDB\ACICS Excel.aspx .cs:line 946
at InstructorDB.ACICSExcel.Ma
have you filled the values of row, column in yourSheetRow,yourSheetCol?
can you show me the latest function of yours?
can you show me the latest function of yours?
ASKER
Here it is. I think the cast to float of the range.top and left is what is failing.
Excel.Range oRange = (Excel.Range)mySheet.Cells [currentRo w, 1];
mySheet.Shapes.AddPicture( Server.Map Path(image File),
Office.Core.MsoTriState.ms oFalse,
Office.Core.MsoTriState.ms oTrue,
(float)oRange.Left,
(float)oRange.Top,
width,
height);
Excel.Range oRange = (Excel.Range)mySheet.Cells
mySheet.Shapes.AddPicture(
Office.Core.MsoTriState.ms
Office.Core.MsoTriState.ms
(float)oRange.Left,
(float)oRange.Top,
width,
height);
can you cast it to double?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well, I got an error on thet too. But, I checked the actual value of top. It was 540.75. WHen I tried to set the value of my float (top) I got a message indicateing I need to add a "F" suffix. Itried this line of code and it worked.
top = 540.75F;
So I added "F" to the convert lines of code like below and it now works.
mySheet.Shapes.AddPicture( imageFile,
Microsoft.Office.Core.MsoT riState.ms oFalse,
Microsoft.Office.Core.MsoT riState.ms oTrue,
System.Convert.ToSingle(oR ange.Left. ToString() + "F"),
System.Convert.ToSingle(oR ange.Top.T oString() + "F"),
100,
100);
Thank you so muc for sticking with me on this. I really appreciate it.
Alex
top = 540.75F;
So I added "F" to the convert lines of code like below and it now works.
mySheet.Shapes.AddPicture(
Microsoft.Office.Core.MsoT
Microsoft.Office.Core.MsoT
System.Convert.ToSingle(oR
System.Convert.ToSingle(oR
100,
100);
Thank you so muc for sticking with me on this. I really appreciate it.
Alex
ASKER
Check that I ran the wrong file. I need to figure out how to get the "F" suffix in there somehow.
ASKER
OK. Here is the code that workds...
Single left = (Single)(1.5);
Single top = (Single)(1.5);
top = System.Convert.ToSingle(oR ange.Top.T oString()) ;
mySheet.Shapes.AddPicture( Server.Map Path(image File),
Office.Core.MsoTriState.ms oFalse,
Office.Core.MsoTriState.ms oTrue,
left,
top,
width,
height);
Thanks again.
Single left = (Single)(1.5);
Single top = (Single)(1.5);
top = System.Convert.ToSingle(oR
mySheet.Shapes.AddPicture(
Office.Core.MsoTriState.ms
Office.Core.MsoTriState.ms
left,
top,
width,
height);
Thanks again.
I think you have used some of the code that is mentioned in my comment, that deserves some points :)
ASKER
roshmon,
You deserve all of the credit or points or what ever. It was not my intention to claim anythin. My only intention was to mark the last comment as the one that work by setting the top variable with System.Convert before using it in the AddShapes. You get all of the kudos. Can you tell me how I can correct this?
Thank you.
You deserve all of the credit or points or what ever. It was not my intention to claim anythin. My only intention was to mark the last comment as the one that work by setting the top variable with System.Convert before using it in the AddShapes. You get all of the kudos. Can you tell me how I can correct this?
Thank you.
ASKER
roshmon, you were most helpful and I appreciate the fact that you stuck with me on this.
Thank you very much.
Thank you very much.
Open in new window