I am trying to apply conditional formatting on range. I am trying to do that if cell value is not empty and cell value is more than 2.5 then cell color will be green. if cell value is not empty and cell value is less than 2.5 then cell color will be red.
I tried this way but I am not getting expected result.
How to refer current cell is not blank in formula?
ExcelAddress formatRangeAddress = new ExcelAddress(_StartPeriod + ":" + _EndPeriod);
var cond1 = ws.ConditionalFormatting.AddLessThan(formatRangeAddress);
cond1.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
cond1.Style.Fill.BackgroundColor.Color = System.Drawing.ColorTranslator.FromHtml("#FF0000");
cond1.Formula = "2.5";
formatRangeAddress = new ExcelAddress(_StartPeriod + ":" + _EndPeriod);
var cond2 = ws.ConditionalFormatting.AddGreaterThan(formatRangeAddress);
cond2.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
cond1.Style.Fill.BackgroundColor.Color = System.Drawing.ColorTranslator.FromHtml("#92D050");
cond2.Formula = "2.5";
I am sure i am not able to write exact formula for my scenario. So anyone can help to share formula which should work for my scenario and range of value will have right background color.
Related
I want to set a number format of the lengends on a chart with the next format:
300.000 kWh
Actually i get 300000.
How can i do this.
I tried do it manually with Excel setting the format of the cell to personalized number format in the dialog cell Format adding this format # "kWh".
With this trick, I can do. But I need to do this when I generate the excel, and not when the excel is generated...
cells["kwhcell"].Style.NumberFormat = "#.##0,00 kWh";
You can't directly. You'll have to loop all the cells and do the formatting yourself.
for (int i = 1; i <= excelWorksheet.Dimension.End.Row; i++)
{
string cellValue = string.Format("{0:N2}", Convert.ToDecimal(excelWorksheet.Cells[i, 1].Value)) + " kWh";
excelWorksheet.Cells[i, 1].Value = cellValue;
}
I sloved my Problem.
This is the correct code to answer my question
wsData.Cells[2, (z + 1)].Style.Numberformat.Format = "#0,0 \"kwh\"";
and
wsData.Cells[2, (z + 1)].Style.Numberformat.Format = "#0,0 \"ºC\"";
I'm trying to add to my file a column with conditional formula...
But the cell stay blank or the formula show as a string.
I saw two possibilities, using _formatRangeAddress or Get each data of a cell... But my file can have 150 000+ rows...
var _formatRangeAddress = new ExcelAddress("C1:Cxxxx,D1:Dxxxx");
string _statement = "SI(C2=D2;Point(D2 C2);LineString(D2 C2,D3 C3)";
var _cond4 = worksheet.ConditionalFormatting.AddExpression(_formatRangeAddress);
_cond4.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond4.Formula = _statement;
Any ideas how can I make this formula works?
Thanks.
i´m trying to aply a format to cells that contains the "SUBTOTAL " Word, this is my code:
ExcelAddress _formatRangeAddress = new ExcelAddress(2,1,tam,40);
string _statement = "$A2=\"SUBTOTAL \"";
var _cond1 = hoja.ConditionalFormatting.AddExpression(_formatRangeAddress);
_cond1.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond1.Style.Fill.BackgroundColor.Color = System.Drawing.Color.LightSalmon;
_cond1.Style.Font.Color.Color = System.Drawing.Color.White;
_cond1.Formula = _statement;
And Works fine if the cell only contains this Word, but in all cases(except 1) the value of the cell will be "SUBTOTAL " + more text.
So i wold like to know if it´s posible to use wildcards or something similar to an SQL like statement.
Thanks.
=ISNUMBER(FIND("SUBTOTAL ", $A2))
if case sensitive
=ISNUMBER(SEARCH("SUBTOTAL ", $A2))
if case insensitive.
The functions Search and Find returns the index of first occurrence of the searched word, of #VALUE!, if nothing found. IsNumber decides, if it is number or #VALUE!
I'm having a problem formatting cells in an Excel sheet. For some reason my code seems to be changing the style of all cells when I just want to change the style of a few specified, or a specified range.
Here's some of the code that I am using:
app = new Microsoft.Office.Interop.Excel.Application();
workbook = app.Workbooks.Add(1);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
//Change all cells' alignment to center
worksheet.Cells.Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
//But then this line changes every cell style back to left alignment
worksheet.Cells[y + 1, x + 2].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
Why would it change the style of multiple cells when I set it to just work on one? Is it not supposed to work how I want it to? Is there another way of doing this?
This works good
worksheet.get_Range("A1","A14").Cells.HorizontalAlignment =
Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
Based on this comment from the OP, "I found the problem. apparentlyworksheet.Cells[y + 1, x + 1].HorizontalAlignment", I believe the real explanation is that all the cells start off sharing the same Style object. So if you change that style object, it changes all the cells that use it. But if you just change the cell's alignment property directly, only that cell is affected.
Maybe declaring a range might workout better for you.
// fill in the starting and ending range programmatically this is just an example.
string startRange = "A1";
string endRange = "A1";
Excel.Range currentRange = (Excel.Range)excelWorksheet.get_Range(startRange , endRange );
currentRange.Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
Don't use "Style:
worksheet.Cells[y,x].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
Modifying styles directly in range or cells did not work for me. But the idea to:
create a separate style
apply all the necessary style property values
set the style's name to the Style property of the range
, given in MSDN How to: Programmatically Apply Styles to Ranges in Workbooks did the job.
For example:
var range = worksheet.Range[string.Format("A{0}:C{0}", rowIndex++)];
range.Merge();
range.Value = "some value";
var style = workbook.AddStyle();
style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
range.Style = style.Name;
ExcelApp.Sheets[1].Range[ExcelApp.Sheets[1].Cells[1, 1], ExcelApp.Sheets[1].Cells[70, 15]].Cells.HorizontalAlignment =
Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
This works fine for me.
Something that works for me. Enjoy.
Excel.Application excelApplication = new Excel.Application() // start excel and turn off msg boxes
{
DisplayAlerts = false,
Visible = false
};
Excel.Workbook workBook = excelApplication.Workbooks.Open(targetFile);
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];
var rDT = workSheet.Range(workSheet.Cells[monthYearNameRow, monthYearNameCol], workSheet.Cells[monthYearNameRow, maxTableColumnIndex]);
rDT.Merge();
rDT.Value = monthName + " " + year;
var reportDateRowStyle = workBook.Styles.Add("ReportDateRowStyle");
reportDateRowStyle.HorizontalAlignment = XlHAlign.xlHAlignCenter;
reportDateRowStyle.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
reportDateRowStyle.Font.Bold = true;
reportDateRowStyle.Font.Size = 14;
rDT.Style = reportDateRowStyle;
Manikandan's answer is good. For SpreadSheetGear users (a C# framework for interacting easier with spreadsheets) try this:
workbook.Worksheets[0].Cells["B1:B4"].HorizontalAlignment = HAlign.Center;.
This will align all the cells in your sheet from B1 to B4 (column 2- row 1 through 4).
I am currently writing an application (C#) to generate reports in a excel file, based on other reports.
The problem is that, once get an number from a certain sheet, and copy to another one, the destination cell is not formated correctly, and the number does no display correctly.
E.g :
Number from source : 14.34
Number on destination : 14.345661
How do i format the destination cell to force the number formating to be the same as the source cell.
Thanks !
The format of a given cell/range in excel can be set via code.
public void SetCustomFormat(string format, int r, int c)
{
((Excel.Range)xlWks.Cells[r, c]).NumberFormat = format;
}
In your specific case, I believe you would need to use the format "#.00" or "#.##"
Here's a snippet from some code I've got in use, to show you the general pattern for formatting cells. Obviously, there are some variables declared, but it should show you what you need.
sheet.get_Range("A" + CurrentRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).Font.Bold = true;
sheet.get_Range("A" + CurrentRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).Interior.Color = Color.Silver.ToArgb();
sheet.get_Range("A" + CurrentRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
sheet.get_Range("A" + CompetencyStartRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
That first line, assuming CurrentRowIndex = 1 and ColPrefix = "B", replacing the variables with the resulting values would translate into
sheet.get_Range("A1", "B1").Font.Bold = true;
At any rate, you want to set the numberformat. (Coming..)
sheet.Cells[Row, Column].NumberFormat = "0.00"