Avoid Date Numbering format in excel - c#

I have been writing an excel using C# and found this issue while i pick a HTML field and write the contents into excel.
Actual Content : 5 8 3
Excel writes : 05/08/2018
Code:
worksheet.Cells[iRow, 5].SetValue(string.Format("{0}", columnValue));
Is there any format that i have to set something like this? I use the below code for DateFormat but for a different Cell.
CellSelection cellSelection = workbook.ActiveWorksheet.Cells[iRow, 1];
cellSelection.SetFormat(new CellValueFormat("yyyy-MM-dd HH:mm:ss"));
How can i make the excel write the same value ('5 8 3') ?
Note:
Also tried removing the above DateFormat code and the issue remains the same.

To format a cell to "Text" you can use the Format Provider "#".
In this case the value can be retained by,
worksheet.Cells[iRow, 5].SetFormat(new CellValueFormat("#"));
Try adding the code before you write into the cell and this should work.

Related

ClosedXML Changing Number Formats

I am doing the following in ClosedXML (standard accounting format for numbers):
ws.Column(col).Style.NumberFormat.Format = "_($* #,##0.00_);_($* (#,##0.00);_($* \" - \"??_);_(#_)";
When the output renders, it doesn't look like what I specified. If I inspect the format, it has been changed to this:
[$$-en-US]#,##0.00_);([$$-en-US]#,##0.00)
Close, and passable, but not what I input.
Anyone know what gives? Is this ClosedXml or is it Excel? I can do it manually in Excel, and it works perfectly.
Looks like Excel is doing some magic on opening a file, propably for localization compatibility. Also on editing the cell format some sort of 'assistance' happens.
Following code should work for you or at least point you in the right direction:
ws.Column(col).Style.NumberFormat.Format = "_([$$-en-US]_(#,##0.00_);_([$$-en-US]_((#,##0.00)_);_(-_);_(#_)";
Please note that on editing the format afterwards in Excel it will likely 'assist' you again and propably mess up. You will find the original format string under 'Userdefined', tho.
Help on format strings is also provided by Microsoft.

EPPlus returns #VALUE! instead of Value from the cell content randomly [epplus]

As per Current requirement I need to read value from the cell using EPPlus. This cell contains the formula and showing value correctly in XL Sheet. but when i am reading that value using EPPlus some cells are giving correct value but some cells are giving error "#VALUE!". I have used ".Calculate()" before read the value but still facing the same problem. Please find the code below in c#.
totalRecycleWorksheet.Cells[row, colval + 5].Style.Numberformat.Format = "#";
totalRecycleWorksheet.Cells[row, colval + 5].Calculate();
var value = totalRecycleWorksheet.Cells[row, colval + 5].Value;
if (!totalRecycleWorksheet.Cells[row, colval + 5].Value.ToString().Equals("#VALUE!")) {}
and here is the formula in every cells:
=IF(('Failure Item'!E348+ROUNDUP(('Output'!E348)*$B$1,0)-'Purchased Items'!F348)>0,('Failure Item'!E348+ROUNDUP(('Output'!E348)*$B$1,0)-'Purchased Items'!F348),0)
and values are as per the screenshot:
Also you can check the Output I have stored in datatable to check the value:
The only examples I see call Calculate at the workbook level such as
excelPackage.Workbook.Calculate();
I had a similar problem. In my case the Excel workbook was a macro-enabled (.xlsm) file. It was macro enabled because I had made use of VBA functions.
When reading Excel.Range.Value2 property from cells the numerical result was consistently -2146826273. I searched this error code as the Hex (800A 07DF) with no luck, but eventually used a bit of debugging to find it resulted from Excel outputting #VALUE! in the cell I was trying to read.
This was because the macros weren't enabled when I'd loaded it via C#, so calls to the VBA functions were failing.
I followed the advice in: Programmatically enable Excel macro in C#
to enable macros on the workbook and all my #VALUE! problems disappeared.

Format cell as Percentage with NPOI C#

I am trying to format a specific XLS cell to the "Percentage" format using NPOI.
I am able to convert it to text using
(dataRow.GetCell(17) ?? dataRow.CreateCell(17)).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("Text");
But if I try do it with percentage it doesnt work. This is the code I am trying
(dataRow.GetCell(17) ?? dataRow.CreateCell(17)).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("Percentage");
It will just set that cell as Generic. How would I go about setting it as percentage?
Please keep in mind I want the value e.g 0.05 to be in the cell (not 5%), but with the format as Percentage.
I found the answer. I had to use:
(dataRow.GetCell(17) ?? dataRow.CreateCell(17)).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
It make it 2dp percentage without the symbol

how to get correct format from xlsx - to interpret floating point number formated as text

i am aware of the floating point inaccurrancy, this is just how to get the correct text FORMAT!! by openxml from the xlsx and show the value like excel does. I am not able to edit the excel file and change the format or something like that, because of some reasons.
while working with numbers formated as numbers everything runs fine
formatted as text or general, the number 0.813 is saved as 0.812999.., excel shows it correct!, but via openxml i can't get the used format (0.000), anyone an idea to get the format?
maybe this is a standard format, not saved and choosen by other values?
finally I am using the ugly solution to get rid of this ..
if the cellFormat NumberFormatId is 2 I try to parse the value to double and then to string with two decimals
double.Parse(displayedValue).ToString("0.00");
not my desired solution, but for the moment this works .. of course also with all other unit tests

How to get cell value with applied formatting (formatted cell value) with OpenXML SDK

I've been googling and searching on the site for the answer, but I couldn't find a solution - everywhere people mostly discuss how to add new number format to the document and apply it.
What I need is to get the cell value as a string with applied formatting - i.e. same string as would be displayed by Excel.
I already figured that there's no easy way or built-in function which would return the readymade formatted value for a cell.
So it seems to me that to get the value I need to do two things:
1. Get the format string.
2. Format the cell value using this string.
But I have problems with both steps.
One can easily get CellFormat instance which would contain NumberFormatId:
CellFormat cellFormat = (CellFormat) document.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.ElementAt(cell.StyleIndex);
But how to get the format string with this NumberFormatId, if the id corresponds to one of standard predefined formats? (i.e. is below 160) They are not in the spreadsheet document and I can't believe that they should be hardcoded in the application.
Also, once the format string is somehow obtained, how to apply it to the cell value? So far I understand, the code should check the type of the cell value and if is Number - convert it to string using the format string.
I found this page which mentions using Microsoft.Office.Excel.Interop, but I would prefer to stay with OpenXML SDK only.
Overall, I'm very surprised that it's so difficult to find a definitive answer to this question on the Web as I thought that this would be something which many developers need in their daily work.
Men, this is a hard one... I will be adding here things that i found that could be worth..
First is to get the numbering format of the cell (once you have the CellFormat:
string format = excel.WorkbookPart.WorkbookStylesPart.Stylesheet.NumberingFormats.Elements<NumberingFormat>()
.Where(i => i.NumberFormatId.ToString() == cellFormat.NumberFormatId.ToString())
.First().FormatCode;
For more information about this you can go to: NumberingFormats
Im trying to find out how to apply this format to the cell.CellValue property... I think thats the way you have to go!
Ok, reading the ClosedXml code (its open source), seems to be easy to get the format.
Simply convert the value text to its type (int, double, etc) and call the ToString method passing the format. I was trying do that with the String.Format and didnt work. Ive tested the ToString and it works, but something still missing.
I recommend to you to look at this class and get the code from the method GetFormattedString() as #El G tell in his comment.
Bassicaly you will have to add something like this:
double d = double.Parse(cell.CellValue.InnerText);
string val = d.ToString(format);
Hope it helps you...
If you want to take cell value with applied formatting, same as displayed in Excel, use .Text property of Cell object. Like this:
String formattedValue = cell.Text

Categories

Resources