I'm trying to generate a excel file(.xlsx) using C# in visual studio 2019, using the NPOI library. I ran into the issue where the data inserted is format incorrectly.
I have a field which can have both numbers and text, when it is displayed in the excel the cell gives warning "Number Stored As Text" only on number fields like in the image attached.
I tried following methods to make cell a string cell.
By giving Style to the cell.
ICellStyle cellStyle = book.CreateCellStyle();
cellStyle.DataFormat = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat("text");
By settng cell Type
ICell cell = row.CreateCell(cellIndex);
cell.CellStyle = cellStyle;
cell.SetCellType(CellType.String);
cell.SetCellValue(itemData.PropertyName.ToString());
Still I get same warning in generated excel file
Any help is appreciated.
This is correct and valid behaviour. The Excel message is informational, not an error. It is telling you that you HAVE managed to store a text string consisting of only numeric characters. Your options in Excel are to convert it to a number or ignore the "error".
If you wanted the value stored as a number then you have to NOT make the cell type string, doing something like this:
if (itemData.PropertyName.ToString().Any(c => !char.IsNumber(c)))
cell.SetCellType(CellType.String);
else
cell.SetCellType(CellType.Numeric);
This might be of use: NPOI Cell Formatting Link
Related
I have winform project in c# that I want to copy data from Excel cells to my datagirdview. But the excell cells is formatted with 2 digits after the comma.I want to get all value.
What i mean :
The excell cell value = 884,79258
Formatted value = 884,79
I am getting the data with Clipboard.GetText() method. Bu it gives me formatted value(884,79).
But i want the get all value which is 884,79258.
These are my codes
String[] lines = Clipboard.GetText().Split('\n');
string result = lines[0];
Thanx.
Edit:
Thanx to dr.nulll. The link that he shared is worked.
I am sharing the link below for those who have similar request.
Copying decimal values from Excel to C# causes to copy only displayed values
Don’t go via the clipboard. Either use Excel via interop or a 3rd party library like NPOI. If you must use the clipboard then the value copied reflects the current formatting of the cell, so you need it set to show 4 digits after the decimal point.
Problem--> Recently I completed an import program for which the input is excel.
To make the matter worse or good I assumed that every column of excel sheet will be of type string.
In excel we have some field that has MPN and partnumber. Internally the partnumber is stored as xx/xx/xxxx, which looks same as UK Date format.
The excel has made these column as number and data, which I never asked it to do.
If I query this sheet using odbc then I get .0 appended with MPN and part number I get as datetime.
To solve this problem I tried to paste the sheet in another blank sheet, I also tried to paste special (Values), but I am not able to paste it in text format. I even tried to paste it in note pad, but doing so is converting numbers in scientific notation.
My question: How can I change the type to text, without converting it to values.
Select the column, right click format cells and choose Text. But there would be a caveat there, since the internal format of dates is a number then you would get the underlying number as text I guess.
Saving as CSV would be a simple workaround.
Change the import-program in such a way that it casts all imported values to string? In Excel VBA it would look something like this:
Function importDateAsString(rng As Range)
importDateAsString = CStr(rng.Value)
End Function
I am exporting some fields to an excel sheet in c#. All but one column are coming correctly, but in that column I need to export the value 0 to 0.00 format.
When my code behind code is running I am appending the value to a stringBuilder object as 0.00 format, but when I opened that exported document it shows the value as 0. Then I must change the column number format in the excel sheet and change the number format by selecting the type 12.3323.
I don't want to do it like this. I want to define the column number format dynamically in c# code behind file.
How can I do this? Please suggest me if you know.
I think C# does right what it does. The issue is that you need to specify the "column format" in excel in order for it to show that way. Just add the format mask.
You can use Format function to convert your value when writing into cell.
Like :
Format(Convert.ToDouble(data.ToString()), "0.00")
You can use Range.NumberFormat = "0.00" (English version) or "0,00" (for the German version of Office);
e.g. ws.get_Range("A1", "C2").NumberFormat = "0.00";
Im trying to copy some cells from one workbook-sheet to another workbook-sheet and preserve the style and formatting using C#.
I can get the attributes one by one like this:
string fontName = ((Excel.Range) workSheet.Cells[3, 2]).Font.Name.ToString();
But Im looking for at way to get it all at once. Thanks
If you copy the cell(s) via the clipboard using the Range.Copy Destination:= method then both the content and formatting will be copied. If you just want to copy the formatting use .Copy and then .PasteSpecial formats
I am using the .Net 4.0 and excel 2003
How can i use an oledb connection to retrieve the cell format of an excel spreadsheet... I specifically want to find out if a cell column (or cell itself) is in a numeric percentage format.
I cannot seem to find this information in the GetOleDbSchemaTable method.
EX: My web app reads numbers from an excel spreadsheet. This works fine; However, if the numbers are in a percentage format, excel displays it as (fraction*100) but the actual value is a fractional decimal (1/3 = .3333..) - Excel displays as 33.33% - (Notice the decimal point).
Therefore, i need a way of distinguishing between what is a percentage & what is not to allow my webapp to work properly...
Any ideas?
Thanks in advance.....
You might be able to get out this information with OleDbConnection.GetSchema, but I'm not sure what information you'll get for an Excel sheet with that. Documentation here.
Search the NumberFormat property of the Cell (Range) in question for a % sign.
Also, if you can get the format type, then you're looking for a format that starts with 'P', like 'P1'.
EDIT: The only way I can see is either using XML or Automation. For automation you need to use the Interop Assembly. Namespace: Microsoft.Office.Interop.Excel, Interface: DisplayFormat, Property: NumberFormat.
Can you just read the first row of data as a string and parse it looking for '%' in the string.