Number stored as Text - c#

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.

Related

In C# Writing to Excel, How to show a big number as it is (Not with scientific notation)?

I am sending an excel file from backend. I have got long numbers for some cards' numbers. I need to show them in Excel but I want all of them to be number and edit them as it is (sometimes the value and the shown text differs, I want them to be the same).
The proplem is when I set the data type of the cell like
var currentRow = 1;
var cardNo = "9993232323232320";
worksheet.Column(6).CellsUsed().SetDataType(XLDataType.Text);
worksheet.Cell(currentRow, 6).Value = cardNo;
worksheet.Column(6).CellsUsed().SetDataType(XLDataType.Text);//Doesn't matter if it is before or after
I can set the value with SetValue method, which makes the cell string:
worksheet.Cell(currentRow, 6).SetValue(cardNo);
But in this case, excel adds an apostrophe in front of the number, which makes it a text seemingly. When you try to edit the cell, the apostrophe exists in front of the number.
Lastly I tried to set the NumberFormatId of the cell by
worksheet.Cell(currentRow, 6).Value = cardNo;
worksheet.Column(6).Style.NumberFormat.NumberFormatId = 1;
For information about NumberFormatId: https://github.com/ClosedXML/ClosedXML-wiki/blob/master/NumberFormatId-Lookup-Table.md
In this case the cell is a number, seemingly a number, but again when you try to edit the row, the row contains a scientific number.
I can achieve what I want over excel like this:
Go to a column, right click, Format Cells
Numbers tab -> Number
Options-> Decimal Places, set to 0, Format Code set to 0.
And then add a number to a row in that column.
Or like this:
Go to a column -> right click -> format cells
Numbers Tab -> Text -> OK.
And edit the lines.
How can I achieve the same thing in ClosedXML in C#
Here is the wiki for number formats: https://github.com/ClosedXML/ClosedXML-wiki/blob/master/NumberFormatId-Lookup-Table.md
In the question what was trying to be achieved was this:
Go to a column -> right click -> format cells
Numbers Tab -> Text -> OK. And edit the lines.
By looking at the NumberFormatId table in the given link, the Id of this operation is 49 so the code must be worksheet.Column(6).CellsUsed().Style.NumberFormat.NumberFormatId = 49;
And another problem is you have to use SetValue instead of setting Value like:
worksheet.Cell(currentRow, 6).SetValue(cardNo);
After that it must work.

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.

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

How can i change excel cell value

i m exporting data from gridview to excel.
This is my exported column:
POSTING KEY
1
50
50
but the column seems in the gridview like that:
POSTING KEY
01
50
50
Excel convert my string values to numeric when after export. But i must see "01". How can i change excel cell type or cell value in code when before save file.?
From Excel, highlight the column or the cell, right click and select "Format Cells", under the "Number" tab, click "Custom" under the Category:. In the Type: textbox, key in "00".
You can export text with a apostrophe suffix. for e.g. to display 01 instead of 1, you need to set cell's value as '01 and not just 01
Excel will take this value as text instead of number. But in this case you will see a Error SamrtTag in Excel saying "Number stored as Text" which can be turned off from preferences.
When you read or write a cell there are two values you can look at. The first one is value (e.g. cell.Value) which is the one you are probably setting. The second is NumberFormat (e.g. cell.NumberFormat). This is a string a will represent the format of the value when displayed in Excel, set this to an appropriate value (use the string representations in Excel if you're not sure which one you want).

Categories

Resources