How can I get hold of the displayed format of a cell in excel interop 2007. I have a cell where the font's boldness is depending on the value in another cell. No matter if the condition is met or not, the cell.Font.Bold and cell.Style.Font.Bold properties are always false. (cell is of type Range). So is there a way to query the cell's style as the user would see it in Excel?
workbook = application.Workbooks.Open(fileName);
var worksheet = (Worksheet)workbook.Worksheets["Test"];
var cell = (Range)worksheet.Cells[8, 3];
var style = (Style)cell.Style;
strb.AppendLine("Bold: " + cell.Font.Bold); // -> False
strb.AppendLine("Bold: " + style.Font.Bold);// -> False
I also tried using the FormatConditions, but there I haven't found a way to know whether the conditions are met.
Cheers
Wullie
Unfortunately Excel doesn't give you functions 'out of the box' to tell you which conditions have been met. The best code I've seen that will tell you which conditions are active is Chuck Pearson's ActiveCondition code. You'll have to translate it from VBA to C#.
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.
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)";
I found some code which uses Style property of Microsoft.Office.Interop.Excel.Worksheet.Cells[x,y] but it is treated as an object in my Visual Studo code editor:
Workbook wb = new Application.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = wb.Sheets[1];
ws.Cells[x,y] is simply treated as an object so how can I use its Style property?
I'm using Microsoft Excel 15.0 Objects Library (goes with Microsoft Office 2013). Does that matter?
Could you please explain this to me? Thank you.
you have to cast the object as a Range.
The Range interface/object contains all the style and value information for the cell or range that you are specifying.
some examples:
((Excel.Range)ws.Cells[r, c]).NumberFormat = format;
((Excel.Range)ws.Cells[r, c]).Value2 = cellVal;
((Excel.Range)ws.Cells[r, c]).Interior.Color = ColorTranslator.ToOle(Color.Red);
((Excel.Range)ws.Cells[r, c]).Style.Name = "Normal"
etc etc and so on.
Have a link:
https://learn.microsoft.com/en-us/office/vba/api/Excel.Range.Style
You might also like to check out stackoverflow.com/questions/15366340/ which includes cell formatting while exporting to excel.
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?
I'm attempting to format a cell in excel to the currency format. So I go into excel, record a macro of me converting an ordinary cell to a currency format, take a look at the vb script and see that it outputs the following:
NumberFormat = "$ #,##0.00"
So i take that format and paste it into my code, it works to the extent that im getting the currency character before the values in my excel sheet. However, the format of the cell is still a number and excel places a little green triangle at the bottom left of the cell informing me that the format is incorrect (which it is, cos its supposed to be currency, but its set to number) is there any way in c# to actually set the cell to a "Currency" format?
thanks
The range object has a "Style" property... the intelisense metadata says that it is "Returns an object you can use" only but you can also just set the Style with that property. To get the built-in "Currency" style, use the "Styles" property of (for instance) a Workbook object.
Example:
using Excel = Microsoft.Office.Interop.Excel;
...
var excel = new Excel.Application();
var wb = excel.Workbooks.Add();
var sheet = (Excel.Worksheet)wb.Sheets[1];
((Excel.Range)sheet.Cells[3, 4]).Style = wb.Styles["Currency"];
In fact you can just set it to the string "Currency" which may be what Charles was suggesting.
((Excel.Range)sheet.Cells[3, 4]).Style = "Currency";
Ok i tried many that codes and i found this works better than other on any language/regional settings.
numberFormat = #"_-[$$-409]* #,##0.00_ ;_-[$$-409]* -#,##0.00 ;_-[$$-409]* ""-""??_ ;_-#_ ";
Not sure I understand your question:In Excel all numeric values including currency, integers and date/time are held as floating-point doubles. The Format of a value only controls how it is presented in the rendering (visible) layer. The green triangle tests do not include tests for formatting. Is the problem that you are creating a text value rather than a numeric value?