I have a sheet with a bar chart like this
When I insert a row using EPPlus
sheet.InsertRow(20, 1);
and save the sheet again the result is
I would expect the chart to respond the same as if I where to have added it in excel and still reference the last row.
This is an over simplified example of my problem, but is there another way of making the chart be relative to the editing I am doing (without doing manual adjustments)?
Related
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.
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
I tried making an excel file with borders on its cells. I tried the following code:
xlWorksheet.Range[xlWorksheet.Cells[13, 1], xlWorksheet.Cells[13,14]]
.BorderAround(Excel.XlLineStyle.xlContinuous,
Excel.XlBorderWeight.xlMedium);
but it is giving me the following output:
How will I add all borders of cells within specified range including the middle borders?
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!
I was wondering if anyone has come up with a workaround to this problem. I've noticed that the AutoFitColumns() method is failing on columns with merged cells. I've included a basic code example below:
var cellRange = worksheet.Cells[1, column, 2, column];
cells.Merge = true;
cells.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
cells.Value = "Some Text Goes Here";
worksheet.Cells.AutoFitColumns();
The resulting worksheet will have the cells in rows 1 and 2 merged (correctly) for the column in the column variable, but that particular cell is disregarded by the AutoFitColumns() method.
Any help would be appreciated.
Basically...
AutoFitColumns is documented to ignore merged cells. It isn't failing, at least in the sense of being defective.
AutoFit within Excel ignores merged cells, too.
Apparently up through Excel 2007, you cannot use the AutoFit feature for rows or columns that contain merged cells at all.
I've only tested with Excel 2013, which seems to behave differently:
Auto-fitting a row ignores all row-merged cells in that row.
Auto-fitting a column ignores all column-merged cells in that column.
In other words, you can use AutoFit in rows and columns with merged cells, it'll just ignore any cells that have been merged along the same dimension you're auto-fitting.
Desired effect of AutoFit w/r/t merged cells? (+ workarounds)
Finally, I'm not sure whether I see how it makes sense to auto-fit with respect to a merged cell. For example, suppose you have a merged cell at A1:B1 with content that fills a default two-column width.
If you auto-fit on column A, what is supposed to happen? Is column A supposed to become wide enough to fit all of A1:B1, sort of like treating the merged cell as if it's content existed only in A1, the top-left original cell? That might be reasonable, insofar as I can't immediately see whether strange behaviors might be implied in some circumstances.
If something like that is desired, I'd insert the content, auto-fit, and only then merge.
But what if you want to auto-fit on column B, in a situation like this:
Here, you might want column B to get wide enough so that all of the content in A1:B1 shows. (The content is just the text "well hello world".)
There's no one-liner for this, I'm afraid. Here's one way to do it, though:
Insert the content in the as-yet-unmerged top-left cell.
Save the current width of this cell's column.
Auto-fit the column.
Save the new column width.
Reset the column width to what you saved in step 2.
Merge the cells you want to merge.
Total up the widths of all your merged columns but the last one.
Subtract this total from the width you saved in step 4.
Set the last merged column's width to the result of step 8.
More generally, you can split up the total auto-fitted width from step 4 among the merged columns in any way you see fit.
To do all this, you'll want to use ExcelColumn.AutoFit and ExcelColumn.Width (and ExcelWorksheet.Column to grab the ExcelColumn objects).
But most simply...
If your content is static (or at least dynamic but not too variable in length), you can simply set reasonable fixed width(s) for the column(s) in question, distributed however you'd like.