Setting chart gap width in EPPlus / Office Open Xml - c#

How can I set the Gap Width value on a column/bar chart in EPPlus?
I have not found a built-in property for this and am expecting I will need to manipulate the chart's Xml, but I've been unable to find the Office Open Xml syntax for doing this.

This is supported in EPPLus.
For example:
var barChart = chart.PlotArea.ChartTypes[0] as ExcelBarChart;
barChart.GapWidth = 0;

I found the answer by creating what I wanted in a standard Excel sheet, and then looking at the underlying XML it had crated.
I set the Gap Width on an existing chart using the Excel, saved the workbook, renamed the file extension to .zip, and found the corresponding chart XML file within the Zip.
In this case I found the element <c:gapWidth val="15" /> at the following location in the chart's XML: c:chartSpace/c:chart/c:plotArea/c:barChart
I highly recommend this method for solving similar OOXML syntax queries, as the documentation I've seen so far is very dense and not easy to navigate, whereas learning by example in this way helped me find the answer in seconds.

Related

Embed file in excel worksheet/cell

I hope someone can help me. Is there a way to embed a specific file (.txt) into an excel cell? I'm currently using epplus, and I would like to embed programmatically a file into a specific excel cell. I did manage to add a hyperlink, but my goal is to have it embedded.
Worksheet.Cells[rowNumber, colNumber].Value = ....
Is there any way to do it? I couldn't find anything online.
As mentioned in the comments, you can certainly put text within a cell, but bear in mind Excel does have a limit to the number of characters it will allow in a single cell. It's pretty large, but conceivably the contents of a text file could exceed that limit -- even if future versions of Excel keep increasing what the limit is (as they have in the past).
You can also embed an OLE object in your worksheet, and a text file qualifies for that. I don't know that you can assign it to a cell, per se. You can change the location, shape and behavior to fit in a cell and behave as though it's part of a cell, but I don't know that it ever belongs to a range the way formulas do. I could be wrong.
The basic construct of how to embed an OLE object into a worksheet is as follows:
Excel.OLEObject ole = ws.OLEObjects().Add(Filename: #"C:\Users\hambone\Documents\foo.txt");
This is the equivalent of the VBA:
Set ole = sh.OLEObjects.Add(Filename:="C:\Users\hambone\Documents\foo.txt")
The method returns an OLEObject object, which you can then shape to behave the way you want:
ole.Height = 5;

How can I parse through a table in a pdf file?

I have a custom table with name, firstname, place of birth and place of living in a PDF file which I want to parse through in C#. One of the simplest way of doing it would be:
using (PdfLoadedDocument document = new PdfLoadedDocument("foobar"))
{
for (var i = 0; i < document.Pages.Count; i++)
{
Console.WriteLine($"============ PAGE NO. {i+1} ============");
Console.WriteLine(document.Pages[i].ExtractText());
}
}
But the problem is the output:
============ PAGE NO. 38 ============
John L.SmithSan Francisco5400 Baden
There's no way I can seperate this with a regex so I need a way to parse through each column of each row in order to get all the values of the customers separated. How can I parse through a table in a pdf file with syncfusion?
You will need a methods that returns you the coordinate of each character found in the pdf. Then you have some math to do (basically to compute the distance between characters) in order to know if the character is part of a word and where the word itself is located along the x-axe. It requires quite a lot of work and efforts and I didn't find such a method in syncfusion documentation.
I wrote a class which do what you want but this is for java project:
PDFLayoutTextStripper (upon PDFBox)
Syncfusion control extracting the text from PDF document based on the structure of content present in the PDF document. So, based on current implementation of Syncfusion control we cannot recognize the rows and columns present in the table of the PDF document.
Also, it is not possible to extract the text in correct order as same as the PDF document displayed using Syncfusion control since the content present in the PDF document follows fixed layout.
But we can populate the table of the PDF document in Excel using Tabula (Open source library). I have modified the Tabula java (Open Source) to achieve layout based text extraction from the PDF document based on your requirement.
Please find the sample for this implementation in below link:
http://www.syncfusion.com/downloads/support/directtrac/171585/ze/TextExtractionSample649531336
Kindly ensure the following things before executing the sample:
Install Java Runtime Environment (JRE) from the below link.
http://www.oracle.com/technetwork/java/javase/downloads/
Restart your machine.
Execute the above sample.
Try this and check whether it meets your requirement.

What does '#' in a Chart ToolTip string mean?

I'm using classes from the C# Charting namespace to create a line graph, which is working fine so far. I have set a ToolTip so I can hover over a plot line and see its XY coordinates as follows:
chart_MPPTs.Series[seriesName].ToolTip = seriesName + " #VALX : #VALY{C}";
However, I got the " #VALX : #VALY{C}" portion from some sample code I found on the Web and I don't fully understand it. #VALX and #VALY appear to be macros or some other type of replacement mechanism and {C} is for formatting. However, I've been unable to find any documentation on these or the full set of such things that are available. Can anyone direct me to this information?
Also, the Microsoft documentation of the Charting classes and their contents seems even more terse than usual when it comes to examples or explanations of some of the terms used. Is there a book or any other resource that provides some good examples of using all of the various features of these classes?
The # is part of the expression syntax for Chart Keywords. And yes, the part in curlies is about formatting, as explained at the bottom of the link..
Here are the settings you will need to make it work as mentioned iin the comments, i.e. display the label going from 0 - 24:
Make sure your x-value data are in fact added as DateTime and tell the chart about it:
chart_MPPTs.Series[seriesName].XValueType = ChartValueType.DateTime;
Now your tooltips should look right. Then to style the chart further try these settings:
chart_MPPTs.ChartAreas[0].AxisX.Interval = 1;
chart_MPPTs.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Hours;
chart_MPPTs.ChartAreas[0].AxisX.LabelStyle.Format = "hh";
Or "hh\\h"; or "h\\h"; for 00h - 24h or 0h - 24h..
All settings can also be done in the designer.

Formatting cell values from OpenXML

I'm opening up xlsx files as a package and reading the contents of the xml files. I'm able to get the shared strings, borders, etc that I need and it's orders of magnitude faster than when I was using Interop. The only issue I have is when it comes to pulling out numbers and formatting them properly based on what the formatting is in the Excel file.
Is there a generic function somewhere that takes a value and a format and returns the formatted string? For example, if I have the value 31502008 and the custom format "$* #,##0_);$* (#,##0)" is there a simple way to get what Excel shows (which is $31,502,008). Obviously Excel knows how to handle it, but I have some sheets that have a crazy number of custom formats and I'm wondering how best to ensure that the string I get back in code matches what is seen in Excel.
Any ideas?
Thanks a lot for any help.

How to embed a picture into a comment using OpenXml or EPPlus?

I'm pretty new to OpenXML and have found the EPPlus project extremely helpful, however I have a need to set the background image of a comment to a picture. I can do this in Excel, however, I do not see any interfaces exposed for doing this.
Has anyone done this before in OpenXML? Or can point me to a good starting point?
Thanks, much appreciated
I believe EPPlus only allows you to set the background colour, but not with a picture. In OpenXML, comment styling (including the background image) is stored in a VML file.
If you use the Open XML SDK, then it's the LegacyDrawing under the Worksheet class. You'll have to get the ID from the LegacyDrawing class, then retrieve the VmlDrawingPart class and manipulate the contents. You'll also have to work with ImageParts of the VmlDrawingPart. It's kind of a mess to work with, actually...
For convenience, you might want to consider SpreadsheetLight. Here's how to do comments:
SLDocument sl = new SLDocument();
SLComment comm = sl.CreateComment();
comm.SetText("There's a picture background.");
comm.Fill.SetPictureFill("julia.png", 0, 0, 0, 0, 0);
sl.InsertComment(2, 2, comm);
sl.SaveAs("CommentBackground.xlsx");
That will stretch the picture fully across the comment box background.
Disclaimer: I wrote SpreadsheetLight.

Categories

Resources