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

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.

Related

EPPlus chart does not keep relative cell references on row insert

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)?

c# excel radar chart - how to change horizontal axis?

In my WPF application I allready exported data to Excel to plot them in a radar chart, just to re-import this chart to the application. I need to do this for real-time analysis of the data and with WPF toolkit I couldn't create radar charts yet (I spend lot of time by searching for a solution before I got to Excel).
My problem now is, that I cant change the horizontal axis of the radar chart via C#. In Excel itself I would do that easy by setting up the chart options, but in my code I didn't got a clue yet what to do and also in debugging I didn't find any chart item, that told me more. My code so far is following:
Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Open(fileName);
Excel.Worksheet worksheet = workbook.Worksheets[1] as Excel.Worksheet;
CreateData(worksheet, sortangle, sortmu);
// Add chart.
Excel.ChartObjects xlCharts = worksheet.ChartObjects() as Excel.ChartObjects;
Excel.ChartObject myChart = xlCharts.Add(120, 10, 300, 300) as Excel.ChartObject;
Excel.Chart chart = myChart.Chart;
chart.ChartType = Excel.XlChartType.xlRadar;
var scale_range = worksheet.get_Range("A1", "A" + sortangle.Count.ToString());
var data_range = worksheet.get_Range("B1", "B" + sortangle.Count.ToString());
chart.SetSourceData(data_range);
chart.HasLegend = false;
// Set chart properties.
chart.ChartWizard(Source: data_range);
So with this I get such a result:
But the rotation axis need to be scaled by 0 to 360 degree, cause the data I process are stored with the information of an angle.
So I tried, by looking to other C# Excel like questions here e.g. this one, adding different lines like chart.SeriesCollection(1).XValues = scale_range; or chart.Axes(Excel.XlAxisType.xlValue).Source = scale_range;. Even by changing the ChartWizard to chart.ChartWizard(Source: data_range, SeriesLabels: scale_range); I didn't got far.
For any suggestions I would be grateful.
EDIT:
By checking the code you may see, that scale_range is not used atm. I also tried to bring it in the data_range item, but then I get two lines.
Okay. Finally got the solution (with help from here) and hope I can help anyone else with the same question. This works for radar charts too. Just had to correct the code for changing the horizontal axis by this:
Excel.Axis xAxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
xAxis.CategoryNames = scale_range;

EPPlus and Excel's Camera Tool

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

Add all borders on cells including center borders in excel using C# excel interop

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?

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!

Categories