Excel format function equivalent in c# - c#

I have an application that uses excel. The user needs to write an excel format in my application and I need to place it excel to some cells. However I need to provide an example to the user to know if the format will be correct or not.
I know in VBA there is the function FORMAT. Is there an equivalent in C# for this function?
It needs to work for example with the format "[$-409]d.m.yy h:mm AM/PM;#" but it has to support anything that the Excel supports.

This was bothering me too. I didn't want to have to apply NumberFormat to the cells, paste the values and then retrieve the text.
After some investigation, I found the function that does this.
WorksheetFunction.Text: https://msdn.microsoft.com/en-us/library/office/ff841121.aspx
The first argument is the value that you want to format, this could be a String, DateTime, Double, Integer etc, the second argument is the Excel NumberFormat string.
MessageBox.Show(ExcelApp.WorksheetFunction.Text(DateTime.Now, "[$-409]d.m.yy h:mm AM/PM;#"));

Related

Passing value in IXLWorksheet convert string to date

I have an excel builder with IXLWorksheet.
When I try to pass the values decimal to the excel worksheet, it convert thes numbers 3.10,3.11 and 3.12 into date.
I really don't have an idea why is this happening, someone know?
Print Example:
4.30 converts normal
3.10 turns into datetime.
I used this to resolve:
ws.Cell(vLinha, coluna).SetValue(Convert.ToString(propertyValue));

How to Distinguish date and other numeric formats (like number/currency) in open xml sdk SpreadSheet?

I am reading xlsx file using OpenXML SDK. There is no issue while reading shared string values but I don't know how to distinguish the actual numbers from the dates as both of them have DataType of null and they are stored as number.
Using DateTime.FromOADate() I can convert numberic value into date format but first I need to identify whether cell format is date or not, otherwise number/currency formatted cell value would also get converted into date.
I tried solution mentioned in this post
How to distinguish inline numbers from OLE Automation date numbers in OpenXML SpreadSheet?.
but it is not working for currency format,number format (negative number formatting please see attached screenshot) and some custom formats number and currency format screenshot.It would convert currency and number formats into date
I am looking for
Generic solution to distinguish date from all other numeric formats so that all the build in formats (like number,currency,Scientific) and custom formatted numeric values would not be converted into date
Is there generic way to format cell values as per formatting applied in excel sheet.Right now we are handling each formatting separately
Each cell has 2 properties r (CellReference) and s(StyleIndex)
StyleIndex for numbers is 2 and for date is 3
33938

Format Entire Excel Column Using C#

I have column F that I want to format as money so add $ sign and commas. It's to late for me to attempt to format it in the Source so I was going to format it via C# after all data has been written to Excel. I know you can use this syntax to change a column to a date - what would be the appropriate syntax for money?
range.EntireColumn.NumberFormat = "MM/DD/YYYY";
Sorry for my stupidity on this one...it's actually quiet simple
range3.EntireColumn.NumberFormat = "$#,##0.00";

display a timespan in excel

I have a timespan stored as a varchar in sql in the format of totalhours-totalminutes-totalseconds.
Now I have to export that information into excel.
The problem I'm having is that I can not for the life of me find a format within excel that will display this information correctly. Is there a way in which this information can be displayed correctly? I am using XLS in C# to output this info.
Sample input: 48:00:11
Sample output: 1900/02/17 12:11:00 AM or 48.0076388888889
For formatting, you need to set a custom time format:
[h]:mm:ss
...which will show numbers of hours for durations longer than 24 hours. If you type "48:00:11" into an Excel cell, it will create that custom format for you (at least, it did in Excel 2007 on my machine just now). Removing formatting shows the value to be the expected double (which all non-strings are): 2.000127, which is the number of days (with time as fraction) for the duration.
Putting that string value into a worksheet from C# may have a different outcome: I suspect so from the 48.007638... value you obtained. Since you have C# at your disposal I would be inclined to calculate the timespan as a number of days prior to pushing the value to Excel, then formatting as above.

c# - Is there any way to access the standard numeric format strings like "N02"?

In a data grid I'm displaying numeric values formatted using a standard format string, "N02" in this case. When I export this data to excel, I'd like to format the excel column the same. Is there a way to evaluate the "N02" format for the current locale and get the actual format string?
If not, I'm thinking I'd have to try and reconstruct it using NumberNegativePattern, NumberGroupSizes, NumberGroupSeparator, and NumberDecimalSeparator properties of the current NumberFormat but I really don't like my chances of getting that right for my than my locale/culture.
This is what I want to set for my Excel interop call:
((Range)excelSheet.Columns[columnLetter + ":" + columnLetter, Type.Missing]).NumberFormat = xxxxx;
I'd like xxxxx for both "N" and for "N02", I know I can use something like this:
"#,##0.00;-#,##0.00" for "N02"
But that's made the whole thing culture specific.
The problem here is twofold; firstly, Excel interop exposes only a very limited set of functionality when it comes to working with number styles. Secondly, Excel itself pays little attention to culture-specific formatting.
For example, in the Open XML standard for Excel, all of the built-in number format styles are hard-coded (see the ECMA standard document, page 2128). Although different styles are used according to the user's locale, Excel does not pay attention to any overrides in the user's regional settings like the .NET Framework's globalization features do. Even if you were to marry-up the standard numeric format strings with their Excel equivalents (by region), the format could still differ due to the regional settings in Windows.
As flindeberg mentioned in their comment, formatting the cell values as text is the only way to really be sure that the way your numbers are formatted in .NET is how they will be appear in Excel.
Due to the limitations you're working with, I would suggest targeting an invariant culture when generating spreadsheets, instead of trying to make specialisations for each culture. By sticking to the rules of the invariant culture, you can fairly easily write an algorithm to translate standard numeric format strings into their expanded form, for use with Excel.

Categories

Resources