Getting NumberFormat of Range using Excel interop in C# - c#

I am trying to use Microsoft.Office.Interop.Excel to read excel file in c#.
I want to read range of cells as it is way faster than reading cell one by one:
Range rbeg = (Range)sheet.Cells[1, i + 1];
Range rend = (Range)sheet.Cells[totalRowCount, i + 1];
Range range = sheet.get_Range(rbeg, rend);
column = (object[,])range.Value2;
The problem is when I want to get number format of cells by calling:
range.NumberFormat
I get System.DBNull. It works when I call it for single cell.
I want to distinguish between cells with numerical values and "%" values.
Any ideas?

See the documentation:
[NumberFormat] returns Null if all cells in the specified range
don't have the same number format.
What were you expecting it to do in this case?

Related

Write nullable integer values back to spreadsheet

I am using Excel as a reporting tool, for my desktop application. The app is written in C# (VS 2019).
The gist of my code is to obtain a range object from the worksheet, populate an array of null-able integers and write it back out to the same worksheet.
So far I have obtained a range of cells, where startRow is the first row highlighted in yellow:
Range startRow = (Range) input.Range["E" + row.ToString(), "K" + row.ToString()];
Initialise the local array as:
double?[,] startArray = new double?[1, 7];
Set the relevant values:
startArray[1,2] = 9.75;
And finally, write the data back:
startRow.Value2 = startArray;
When I write the array back, I get the error
The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
In this case, I assume that it is because I have some null-able values in the array, as if I do not use a null-able array and as a result its values are zero by default, this works OK, but wites zeros out into the spreadsheet.
I did try using:
string[,] startArray = new string[1, 7];
which does work, but any fields that does have a value, are not seen as numeric and causes Excel formulae not to work.
So to my question, can I get the array to write back to Excel with nulls, or do I have to revert to using the cell object and only update the cells which contain a value?
As it happens, just the fact of writing this post has enabled me to fix it.
The simple answer is to write directly to the range only the values which contain a value.
So something like this:
for (int i = 0; i < 7; i++)
{
if (startArray[0, i] != null) startRow[1, i + 1] = startArray[0, i];
}
As for EPPlus, it looks a fantastic product, but as I already pay for an Excel license would find this a little over kill as a paid license would also be needed for EPPlus.

How can I force Excel to display scientific notation via EPPlus?

Is there any way to control whether a number displays in scientific notation using the EPPlus library to generate an Excel file in C#?
worksheet.Cells[row, col].Value = someValue; //example value: 0.0000152691
worksheet.Cells[row, col].StyleName = RegularCenterAlign;
worksheet.Cells[row, col].Style.Numberformat.Format = "WHAT DO I PUT HERE???";
I tried "0.00e0" for the format and that causes excel to throw an error:
I didn't see it in the documentation but if you look in Excel at the formatting options, scientific notation is available, so you'd think EPPlus has a string code we could use for this.
What seems to be happening now is Excel is making its own determination about whether to format a given number in scientific notation. The same code inserted all of the numbers in the following screenshot. No format was specified but you can see that last one did not use scientific notation.
Use:
ws.Cells["A1"].Style.Numberformat.Format = "0E+00";
You can add the number of decimals with something like:
ws.Cells["A1"].Style.Numberformat.Format = "0.0000E+00";
Don't forget that your value should be numeric. This value will be formatted:
ws.Cells["A1"].Value = 0.0000152691;
this one won't:
ws.Cells["A1"].Value = "0.0000152691";

Maximum value for the absolute of Numbers in Excel and C# programatically

I am programming an application which uses data from excel file. in order to to get the maximum number in a range of column, I do the following:
xlRng = sheetReal.Range[sheetReal.Cells[11, i], sheetReal.Cells[41, i]];
sheetReal.Cells[UsedRow , i].Value = excelApp.WorksheetFunction.Max(xlRng);
which is fine. but now I want to have the maximum value for the absolutes of numbers. How can I get that?
Thank you
Change:
excelApp.WorksheetFunction.Max(xlRng);
To:
excelApp.WorksheetFunction.Max(-excelApp.WorksheetFunction.Min(xlRng), excelApp.WorksheetFunction.Max(xlRng));

Write to Excel very large numeric value

I am trying to write to Excel using Excel Interop. One of my column will have a very numeric value (44 digits).
After checking similar posts in the internet, I tried setting the NumberFormat.
worksheet.Cells[i, j].NumberFormat = "#"; and worksheet.Cells[i, j].NumberFormat = "#";
But it still shows the values in format- ######... so on.. I tried expanding the columns too but that did not work. However, this worked for some of my other column where I have numeric values in 14 digits.
Any help? Please let me know if I can provide more details.
There is a 15 digit limit in Excel unfortunately
for cells use cells["A:A"].numberFormat = "#";
for columns use cells[0, 2].EntireColumn.NumberFormat = "#";
If showing the data in text is ok , then set the cell format to TEXT.

conditional format excel epplus if (cell.value<0) fill red

I´m trying to apply conditional format to Excel using EPPLUS so that a range of cells is filled with red color if the value is negative.
I try this code that if the value of the cell is bigger that the value of the next cell, that cell is filled with red
ExcelAddress _formatRangeAddress = new ExcelAddress("J2:J"+(listaMargenes.Count+2));
string _statement="IF(OFFSET(J3,0,-1)-J3>0,1,0)";
var _cond4 = hoja.ConditionalFormatting.AddExpression(_formatRangeAddress);
_cond4.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond4.Style.Fill.BackgroundColor.Color = System.Drawing.Color.Red;
_cond4.Formula = _statement;
this Works fine, but if i change:
IF(OFFSET(J3,0,-1)-J3>0,1,0)
by this:
if(J3<0)
does not Works, when opening the Excel says there is corrupted data.
any idea of how to write the correct way tu put in red the cells with negative value?
An IF statement in excel does not allow for an optional value_if_true part anymore (I believe in older versions it did): MS IF Documentation
So change it to something like:
string _statement = "if(B3<0, 1)";

Categories

Resources