EPPlus and Excel's Camera Tool - c#

Is it possible to use Excel's Camera Tool with EPPlus? that is: programatically copy a range of cells from one sheet and paste them as a drawing object into another sheet?
Update:
I noticed that Excel's Camera Tool simply creates a picture with a formula. The formula is the range of cells to be watched/observed by Excel. If any of these cells change the picture is updated by Excel.
But with EPPlus is not possible to inject a formula to a Picture object, eg:
var picture = worksheet.Drawings.AddPicture("picture", (FileInfo)null);
picture.SetPosition(1, 0, 1, 0);
picture.Formula = "A1:D9"; // ...there is no "Formula" property for ExcelPicture object
Any workaround?

Bad news when I record a VBA Macro and replay it, it doesn't work. This is the syntax thats generated:
Range("A2").Select
Selection.Copy
ActiveSheet.Shapes.AddShape(, 355.5, 32.25, 72#, 72#).Select
ActiveSheet.Shapes.Range(Array("Picture 3")).Select
Application.CutCopyMode = False
Working with Images in Excel via automation is limited. You are pretty much limited to Shapes (or shudder - clipboard):
Set shp = ws.Shapes.AddPicture("C:\You.png", msoFalse, msoTrue, l, t, w, h)
shp.Name = strPic
shp.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue
shp.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue
What I am suggesting is create a screenshot of the selected cell and workaround it that way.
'Select the cells you want to copy to image
Range("A2").Select
'Copy selected cells contents to clipboard as image
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
'Choose destination
Range("B3").Select
'Paste
ActiveSheet.Paste
'Restore previous clipboard to memory
Hopefully the above will be enough to help you get it working in EPPPlus.
ps Converting VBA to C# is really easy, and it should be
trivial converting the above to EPPPlus: https://stackoverflow.com/a/34055947/495455

Related

How to rotate Excel sheet horizontally using C# with Excel interop?

I created an Excel sheet with different size of columns and in some cells their are images placed using Excel interop api and some third party dll (gembox).
Here's a screenshot of that Excel sheet:
Now client want to rotate it horizontally because last column text getting outside of the sheet.
I tried with some existing answers, like using transpose etc.
Is there anyone who knows method to directly rotate sheet with the images pasted in cell?
Thank you
Try below code.
xlWorkSheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
// here xlWorkSheet is your worksheet.

Interop - Excel to Word copy - Shrink table width to fit Word document page width

I am copying a cell range from Excel to Word which contains several tables.
I need to have my document set to portrait. The issue is the copied tables width is larger than width of Word document page. So I can only see part of the table, the other part goes out of the Word document, hence can't see it.
Once the Word doc is created I can manually shrink the table width to fit Word documents page width. How can I achieve that in programmatically using Office Interop?
document.PageSetup.PageWidth = (float)500 didn't work out for me.
I saw there is a property as AutoFitBehavior: Word.WdAutoFitBehavior.wdAutoFitWindow but not sure how it can be apply to the Word document.
Appreciate any help on this?
As you asked some questions in your question body, I can answer some of them as:
I need to have my document set to portrait:
You should insert a section break then change the orientation:
doc.Words.Last.InsertBreak(Wd.WdBreakType.wdSectionBreakNextPage);
doc.Sections.Last.PageSetup.Orientation = orientation;
Or set orientation after creating document
doc.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
Once the Word doc is created I can manually shrink the table width to fit Word documents page width. How can I achieve that in programmatically using Office Interop?
You need just to set PreferredWidth too 100 percent after setting its type to wdPreferredWidthPercent:
table.PreferredWidthType = Wd.WdPreferredWidthType.wdPreferredWidthPercent;
table.PreferredWidth = 100.0f;
I saw there is a property as AutoFitBehavior: Word.WdAutoFitBehavior.wdAutoFitWindow but not sure how it can be apply to the Word document:
You can use AutoFitBehavior like this:
table.AllowAutoFit = true;
table.AutoFitBehavior(Wd.WdAutoFitBehavior.wdAutoFitWindow);

change merged cells height

I create C# programmatically excel file with merged cell. User OpenXml library. I need AutoFit merge cells as single cell. But I can't change merged cells hight. Is it possible to programmatically change?
You can't change the cell height as far as i know. But, a nice workaround would be to change the row heigth instead of the cell heigth.
Remember that in vb you can do something like this: Rows(3).RowHeight = 25;.For the C# way you should take a look at this: MSDN Reference. Hope it helps!

C# PDF work with tables

I have a pdf file with some table inside.
I want to read this document and change some existing row and add new rows too.
Is there a way do do this?
I'm afraid you won't have any luck.
Unfortunately you don't get 'rows' in a PDF - there is no concept of a table in PDF - only text and vector graphics.
This means you will have to manually add the vectors and the text in the right places to give the illusion of a table.

Excel VSTO call results different between v2003 and v2007 - why?

I have this piece of c# code in my Excel 2003 add-in:
var leafPoint = m_worksheet.Shapes.Item("aPoint").Duplicate();
leafPoint.Name = "Shape" + (m_shapesNameIndex++).ToString();
leafPoint.OnAction = m_worksheet.CodeName + ".PointClicked";
leafPoint.AlternativeText =
string.Format("Correlation Value: {0}",
item.PointData.Correlation.ToString("0.0000;-0.0000"));
leafPoint.Top = item.LeftChildNode.Top +
((item.RightChildNode.Top - item.LeftChildNode.Top) / 2) +
(leafPoint.Height / 2);
When I run it in Excel 2003, it works perfectly. However, when I run it in Excel 2007, the shape's Top value is off... It always ends up a few pixels above the intended location!
When I looked at the logs, in Excel 2003 the shapes were consistently placed in the correct position, but in Excel 2007, when the code attempts to place the shape's Top position, Excel 2007 seems to be overriding the value for some reason (I think).
For instance, in one case the leafPoint.Top value resolved to 206.25. In Excel 2003, that was indeed the result. However, in Excel 2007, this value ends up becoming 204.2954...
Does anyone have some insight into this issue?
It's the .Duplicate function that does this. It doesn't duplicate the same .Top or .Left. My guess is the change is a result from customer feedback about not being able to find their duplicated shapes (you can duplicate manually as well be selecting the shape and click Ctrl+D.
You can just sent those to the original shape's .Top and.Left. . Here's an example in VBA:
Sub AddShapeAndDuplicate()
Dim sh As Shape
Set mysheet = Worksheets(1)
With mysheet.Shapes
Set sh = .AddShape(msoShapeRectangle, 144, 144, 72, 72)
With sh
.Name = "Red Square"
.Fill.ForeColor.RGB = RGB(255, 0, 0)
End With
End With
Dim sh2 As Shape
Set sh2 = sh.Duplicate
With sh2
.Top = sh.Top
.Left = sh.Left
End With
End Sub
I finally figured it out. Based on this link Excel 2007 Service Pack 2 – the official fix list, it mentions among the fixed bugs:
The value of the Top property of the
Line object is affected by Zoom level.
This leads to incorrect placement of
line shapes when this value is set
programmatically to lines on a sheet
that is zoomed to anything other than
100%.
I figured if the Line object was affected, possibly could there have been a problem with the Shape object as well?
So I checked my template, and indeed, it was set to 80%. (Line objects work fine, by the way, since my Excel version has SP2 installed). As soon as I set the zoom level to 100%, the shape objects suddenly rendered to the position they were expected to.
Another note: this problem only occurs during rendering. Once all the shapes have been drawn, I can set the zoom level to whatever I want, and the shapes position themselves properly.
Conclusion: whenever drawing shapes in Excel, always set the zoom level to 100%. After all the drawing is done, you can set the zoom level back to the original desired zoom.

Categories