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).
Related
I am exporting RDLC to excel, I have id , joining date field in rdlc. I have set tablix Id textbox as "number" and for joining date as "date" . But in excel the format cells for id is showing as general instead of number.
How to make format cells for id column as number
When defining TextBox formatting, try with the Custom format.
If your ID is an integer, just set custom format to zero, like this:
After that, you can click again on "Number" and see how it is represented as a number without decimal places, like this:
The rest is easy: From ReportViewer, save the report as Excel. The format of the ID column should be Custom, but it is actually numeric. Honestly, I don’t know why Excel says it is a Custom format and not a number, but I wouldn’t worry about that.
For more info about Excel Number formatting check this out:
Number Formatting in Excel - All You Need to Know (spreadsheetweb.com)
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.
I'm generating excel report in c# in excel.binding data to excel through datatable.
There is one column which will consist of 16 digit number for eg.,1317722825000285 but in excel it is coming as 1317722825000280 last digit is getting replaced by 0.
So how to can I format this column value as excel Text ?
There's a great post here that may answer your question: Format an Excel column (or cell) as Text in C#?
Try formatting the cell as text and making your variable a string in C# before inserting.
Hope this helps.
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.
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?