I'm currently using the bytescout library to generate a PDF document. Here is some pseudo code of me setting the cell in question:
cell.Value = 20;
cell.NumberFormatString = "0.00%";
When the Excel document is generated the Cell displays as 2000.00%
Does anyone have experience with this library?
Simple answer is to divide by 100. Seems Excel expects the % decimal value. The lightbulb eventually came on.
Related
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.
Background
I am trying to read a 22 x 22 matrix from a Excel Worksheet. The matrix holds percent values and the values of each row must have a sum of 100% (or 1 when dealing with the numbers behind the percent value). When I open such a Excel worksheet and build the sum on each row, it is always 100% (1). Perfect.
But when I read the worksheet and sum up the (double) values read from the sheet I get a significant distance to 1 on most of the rows (significant means more than 0.00000000001 absolute distance to 1).
Investigation
I modified the matrix in excel to display me the numbers behind the percent values and the compared it to what I've read using EPPlus. For example I had
99.86% (Excel with percent)
0.998610811163197 (Excel as number)
0.9986108111631975 (read with EPPlus)
I renamed my Excel document to a ZIP archived, unpacked it and opened the according sheet in Visual Studio. The value stored was exact the value I got with EPPlus - which wasn't really surprising.
Solution?
I decided to operate as excel does, at least I thought excel does it so. I tried to round the values after 15 digits. But funny enough, the result wasn't the same as in excel, even worse, after looking at some other values I had:
0.00 % (Excel with percent)
0.00000330942432252678 (Excel as number)
3.3094243225267778E-6 (stored in the XML, read via EPPlus)
So, the question is: is there a way to round or read the values from Excel as Excel displays them?
Here is my code for reading the excel:
using (ExcelPackage excel = new ExcelPackage())
{
excel.Load(File.OpenRead("data.xlsx"));
var a1 = excel.Workbook.Worksheets.First().Cells["A1"].Value;
var a2 = excel.Workbook.Worksheets.First().Cells["A2"].Value;
}
Apologies, I am not able to upload the excel file at the moment from my workplace to dropbox or something else, I'll attach it later.
Edit: here is the excel document.
If i understand your question, you have problem with display double value, right?
You can use correct format for displaying double values. For example:
double val = 99.8610811163198;
Console.WriteLine(val.ToString("P", CultureInfo.InvariantCulture));
About this read MS article: https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx
I have an ETL that's saving data to an Excel file. The issue is that the decimals are not being written out for integers. Example:
14.00
is being written out as
14
My code for writing out that line is
loWorksheet.Cells[liRowNum, 5] = lcAmount.ToString("0.00");
When I step through the code, it shows as 14.00, but on the Excel file it is not retaining the decimal places. Is this something that can be fixed in my code or is this an Excel issue? Any suggestions?
I'm quite sure you have to set format for your cells. I can't check right now, but it will be something like
xlYourRange.NumberFormat = "0.00";
You can check this question Set data type like number, text and date in excel column using Microsoft.Office.Interop.Excel in c#
If you really want the data to be displayed literally the way it is in the source file, you have to deal with trade-offs. The simplest way is to format the data as text. You can do this a cell at a time or for entire columns:
loWorksheet.Columns["A:E"].NumberFormat = "#";
The trade-off is it's just text at this point. You can't add, sum, average, whatever.
On the other hands, if your data looks like this:
4.0
4.00
4.000
You can't really keep it as numbers and expect to retain the original format without doing some funny business.
If it's consistently two decimal places, and you know it's going to be, then I agree with #RenatZamaletdinov's solution.
And you might want to consider other strings and what Excel might to do them
0000123 becomes 123
10/23 will probably render as a date, depending on your localization
12345678901234567890 will render as scientific notation probably
These are all avoided if you make the numeric format text (#), but again without knowing what you plan to do with the data, it's hard to say if this is the correct approach.
Wrap lcAmount.ToString("0.00");in a pair of quotes and put an equal sign in front of it. This will prevent excel from overriding the format.
loWorksheet.Cells[liRowNum, 5] = "=" + '"' lcAmount.ToString("0.00") + '"';
I use NPOI .NET third party library to export datas to *.xlsx file.
I have got a time value which represented in milliseconds.
For example, 2 minutes 12 seconds 3 milliseconds represented as 132003 milliseconds.
I would like to display "132003" as "02m 12.003s" in Excel.
So if i click a cell in excel which contains this value, i would like to see "02m 12.003s" in the cell, and "132003" in the formula editor.
How can I solve it?
Thanks in advance.
Consider the code below:
var t = TimeSpan.FromMilliseconds(521516);
var formatted = String.Format("{0:D2}m:{1:D2}s:{2:D3}ms", t.Minutes, t.Seconds, t.Milliseconds);
System.Diagnostic.Debug.WriteLine(formatted);
This will output:
08m:41s:516ms
You can use that code to retrive the formatted text and then add it to excel.
I am using Excel Library - http://code.google.com/p/excellibrary/ - To generate an excel 2003 spreadsheet. Everything works fine except when some big values are used.
These are some reference numbers that are used by a client and I simply need to present them as integer values in the spreadsheet.
int val = 1420007117;
worksheet.Celss[row, col] = new Cell(val); // Displays - 352108063
This results in the value 352108063 being displayed in the spreadsheet. If the value is lower, then it displays fine.
Anyone know what the issue might be, or how to work around this problem. Outputting the value as string is not possible as it leaves a green Number stored as Text error.
I would say that excel doesn't support 64-bit integers and excellibrary doesn't care about it.
For such big numbers you better use floating point. This is how Excel handles big numbers.