EPPlus Number Format according to different Culture - c#

This the Excel Screenshot link to show example In a particular cell, It consists double value but showing rounded value in the cell.
like If in the cell the value is 553236 but the original value is 553236.456001
and can only see this value on top bar when click on that particular cell.
But my question is I have converted the original value into Italian currency, How to make it do the same as above
In Italian Culture 553,236.456 value will be converted as 553.236,456
Have to show only 553.236 and when Click on Particular cell On top bar value should be 553,236.456
For converting the value into Italian culture I used the code as below
Convert.ToDouble(cellValue).ToString("C", CultureInfo.CreateSpecificCulture("it-IT")).Split(' ')[0]
used split to not show currency symbol

After many trials like
cell.Value = Convert.ToDouble(cellValue,new CultureInfo("it-IT").NumberFormat);
and
cell.Value = Convert.ToDouble(cellValue).ToString("N",System.Globalization.CultureInfo.CreateSpecificCulture("it-IT")).Split(',')[0];
I understood that number format or number according to culture is dependent on your system's region. If numbers are getting converted to different culture and were not showing or showing wrong format, that is cause of local system's region. If u change ur region to that particular culture. You'll get the perfect answer.
I just did as per my country format in code like using
cell.Style.Numberformat.Format = "#,##0";
it worked fine with this. After committing code to svn, when I tried the Export of Excel from main website it was working beautifully according to system's region. If I change to Italian, Excel output numbers were in italian format and when I change to Indian , the numbers were in Indian number format.
So Don't worry about whatever culture u want, do write according to ur culture all work's fine.
Over this if u still want to get number according to particular culture use the above second code, but problem is u will get numbers stored as text. that u can't add or do any valuations.

You need to format the data cell.
Assign a number or custom format by setting the following property:
dataCell.Style.Numberformat.Format
Generate your custom format in Excel itself then just copy that format and set it to above property.
You can access cell formats in Excel by right clicking on a cell and selecting Format cell.

Related

While downloading the excel file i want the excel cell value will be displaying as currency format which one has currency format

I want to download an excel file which has some column which contains number value, text value and currency value.
Now I want to display the number format on every cell. Every number format works fine except the currency one.
Suppose I have a value $100.00. Now I want to display that value as currency (number format). How can we do this?
Enter the money value as you would a double and enter the format like:
worksheet.Cells[1, 1].Style.Numberformat.Format = "$###,###,##0.00";
This is a duplicate of your question I believe.

Dealing with OADate OpenXml Html import during Data Refresh

I am facing a peculiar problem with Excel with Data Refresh.
A solution that I am working on generates an Excel on the Server using OpenXml.
In this Excel, I also add a Connection Part, which basically is a link to a webpage of my website.
This particular problem is not with English user, but if the User has Danish as the language.
When the Excel is generated, I specify a cell type as Date type (FormatId = 14)
I set the text for the cell as an OADate.
The first rendering works fine and the date is displayed correctly.
But in case of Danish, the decimal value of the OADate is stripped off and a large date is rendered making Excel show ######## (date to large)
But during Data Refresh, when Excel tries to refresh based on the data my webpage sends as Html table, Excel strips the decimal and a long number is placed in the date time cell.
I tried setting the style of the cell based on advice available here: Format HTML table cell so that Excel formats as text?
But it seems there is a difference in the way OADate works. Does anyone know what is the best way to construct the Html for Excel Data Refresh in case of OADate.
By changing the Html to have a cell value as Text work, but I want my Html to pass on the OADate to Excel.
regards,
~Mayur
The delimiter "," or "." should be set as per user and then value should be set.

Number stored as Text

I'm currently updating values in an Excel template and I keep getting my currency fields to have the green triangle in the top corner and the "Number Stored as Text" message. How do I get Excel to recognize the cell is a number and I want it to treat it that way since I already have the cell formatted as currency? (just like what happens if I'm on the cell in Excel, hit F2, and then hit enter)
Here's a simplified version of what I'm currently doing:
UInt32Value moneyFormat = report.createCellFormat(report.Stylesheet, fontIndex, backgroundIndex, borderIndex, 168);
report.UpdateValue("Workbook", "I29", "1234.56", moneyFormat, true, String.Empty);
And here's an image of what my cells look like
You might find the answer in this post:
Open XML SDK 2.0 - how to update a cell in a spreadsheet?
Probably the DataType of the cell is not set correct.

Retrieve excel cell text that is not wide enough

I want to read excel display value, not value that excel internally saves. Problem is that when this value is wider that the cell it makes cell to display ####### which is OK in excel. But I want to read this value from excel API. When I access property range. Text I also get value ########. Is there a way to get value which will be displayed when cell is wide enough?
i.e some third property, not Value2 nor Text.
My colleague said that there are three values in excel, one internal value how excel represents cell value, one what is currently displayed and one what will be displayed when there is enough space, is he right?
If this is not possible, I have another solution which might work. I used AutoFit() method to make cell wide enough and that I read value. But the problem is that I don't want to change width of columns. Is there a way to remember width, then change width so I can read cell, and that return it again to previous width?
Or maybe I should somehow apply formatting option to value of cell to get what will be displayed?
How to solve this?
In VBA I would do this by getting the Value and the number Format, and then useing the VBA Format Function to format it, but I am not sure how to do this using c#.
As a matter of curiosity, why would you want to get the formatted value anyway?

Changing Excel cell format in C#

How can I change the format of a cell in Excel using the Microsoft Excel 12.0 Library in C#? More specifically, I want to change a given cell to text format.
I have read .net c# changing excel cell format, but the cell still says general (although it does display as text).
It depends on the format you're actually trying to implement. There's not a silver bullet for changing to any format you want. If it's formatting numbers that you need, you could use something like this. I ran into a lot of suggestions to create a macro, you can do some pretty specific tasks that you define yourself.
No need to do that just change the NumberFormat.
// Set all cells from A1 to B2 to be text
ws.get_Range("A1", "B2")).NumberFormat = "#";
different formats can be looked up here
http://office.microsoft.com/en-us/excel-help/number-format-codes-HP005198679.aspx

Categories

Resources