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.
Related
I'm using NPOI to programatically create an Excel file. One of the requirements is that it needs to be able to change the background of cells based on values - green for good numbers, red for bad, etc. I have everything working perfectly and can create formulas...but I cannot for the life of me find a formula that shows how to change background color. No matter how I try to google for the answer, everything just wants to show how to open Excel and use the conditional formatting wizard. I'm overlooking something? Is there a way I can see the formula that the conditional formatting wizard created and just copy and paste it into my code?
Below is a sample I set up to change the field to Pass/Fail...but my peeps like shiny colors to go along with it...
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sh = (XSSFSheet)wb.CreateSheet("ACT");
string cf = "IF(" + engCell + (detailRow.RowNum + 1) + #">17,""Pass :)"", ""Fail :("")";
detailRow.CreateCell(detailIdx);
detailRow.GetCell(detailIdx).SetCellType(CellType.Formula);
detailRow.GetCell(detailIdx++).SetCellFormula(cf);
I figured it out!!! I hope this may help others who are using NPOI XSSF for conditional formatting:
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sh = (XSSFSheet)wb.CreateSheet("ACT");
sh.CreateRow(0).CreateCell(0).SetCellValue(14);
sh.CreateRow(1).CreateCell(0).SetCellValue(20);
sh.CreateRow(2).CreateCell(0).SetCellValue(10);
sh.CreateRow(3).CreateCell(0).SetCellValue(23);
sh.CreateRow(4).CreateCell(0).SetCellValue(19);
sh.CreateRow(5).CreateCell(0).SetCellValue(14);
XSSFSheetConditionalFormatting sCF = (XSSFSheetConditionalFormatting)sh.SheetConditionalFormatting;
//Fill Green if Passing Score
XSSFConditionalFormattingRule cfGreen =
(XSSFConditionalFormattingRule)sCF.CreateConditionalFormattingRule(ComparisonOperator.GreaterThanOrEqual, "19");
XSSFPatternFormatting fillGreen = (XSSFPatternFormatting)cfGreen.CreatePatternFormatting();
fillGreen.FillBackgroundColor = IndexedColors.LightGreen.Index;
fillGreen.FillPattern = FillPattern.SolidForeground;
//Fill Red if Passing Score
XSSFConditionalFormattingRule cfRed =
(XSSFConditionalFormattingRule)sCF.CreateConditionalFormattingRule(ComparisonOperator.LessThan, "19");
XSSFPatternFormatting fillRed = (XSSFPatternFormatting)cfRed.CreatePatternFormatting();
fillRed.FillBackgroundColor = IndexedColors.Red.Index;
fillRed.FillPattern = FillPattern.SolidForeground;
CellRangeAddress[] cfRange = { CellRangeAddress.ValueOf("A1:A6") };
sCF.AddConditionalFormatting(cfRange, cfGreen, cfRed);
I'm having an issue when I try to change the format of an entire column.
My issue is that I don't get the XLCellValues operand. I am able to choose XLDataType.Text but that does not work. This is what I have:
eventSheet.Column("A").CellsUsed().SetDataType(XLDataType.Text);
I have also tried replacing the A with 1. No luck that way either. Could it be a newer revision of the ClosedXML package? Thank you!
The IXLCell.SetDataType method does not change the display format. It just changes the type of the underlying value. To change the display format, use IXLCell.Style.NumberFormat
Example:
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Style NumberFormat");
var co = 2;
var ro = 1;
// Using a custom format
ws.Cell(++ro, co).Value = "123456.789";
ws.Cell(ro, co).Style.NumberFormat.Format = "$ #,##0.00";
ws.Cell(++ro, co).Value = "12.345";
ws.Cell(ro, co).Style.NumberFormat.Format = "0000";
// Using a OpenXML's predefined formats
ws.Cell(++ro, co).Value = "12.345";
ws.Cell(ro, co).Style.NumberFormat.NumberFormatId = 3;
ws.Column(co).AdjustToContents();
workbook.SaveAs("StylesNumberFormat.xlsx");
You can find more information on the ClosedXML wiki, e.g. at https://github.com/ClosedXML/ClosedXML/wiki/Styles-NumberFormat
I am using Aspose Cells and I need to convert a particular column to text format, as the column consists of both numbers and Text, by default the column is taken as number format. I have used
Aspose.Cells.Style style = worksheet.Cells["A3"].GetStyle();
style.Number = 49;
worksheet.Cells["A3"].SetStyle(style);
the above code works only for particular Cell but I need to Set Text format entire column, I have tried using this
Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
style.Number = 49; //Sets the Text format.
StyleFlag flag = new StyleFlag();
worksheet.Cells.ApplyColumnStyle(0, style, flag);
worksheet.Cells.ApplyColumnStyle(1, style, flag);
the above code not working. Is there any other way to fix this?
Thanks in Advance
I got solution for this,
var workbook = new Workbook();
var worksheet = workbook.Worksheets[0];
Aspose.Cells.Style TextStyle = workbook.CreateStyle();
TextStyle.Number = 49;
StyleFlag TextFlag = new StyleFlag();
TextFlag.NumberFormat = true;
worksheet.Cells.Columns[0].ApplyStyle(TextStyle, TextFlag);
worksheet.Cells.Columns[1].ApplyStyle(TextStyle, TextFlag);
I have written an application which writes content into an Excelsheet. So far so good. But in some cases the autoformat function from Excel changed my values.
For example, I write 'true'/'false' to a cell, but Excel interprets it and translate it.
Second example, I write a telephonenumber to Excel and Excel cuts the leading '0', so '012345' changed to '12345'.
How can I say, that a cell is just a text/string cell?
This is a samplecode, how I write to Excel:
ws.get_Range("A1", "A1").Value = "true";
ws.get_Range("A1", "A1").Cells.WrapText = true;
ws.get_Range("A1", "A1").Rows.AutoFit();
best regards
Another way to make Excel treat values as text is to add ' in front of the values:
ws.get_Range("A1").Value = "'0123";
The ' will be visible only in the cell formula but not in it's value/text.
You can also set multiple cells at once:
object[,] values = { { "true", "0123" }, { "false", "0125" } };
var range = ws.get_Range("A1:B2")
range.NumberFormat = "#"; // Text format to treat the values as Text
range.Value = values
or
Clipboard.SetText("'true\t'01234\n'false\t'01235");
ws.get_Range("A1").PasteSpecial(); // or just ws.Paste();
Try this for leading zero
ws.get_Range("A1", "A1").NumberFormat = "#";
ws.get_Range("A1", "A1").Value = "012345";
For boolean value
ws.get_Range("B1", "B1").Value = "True/False";
I would like to add a data validation for a cell in an excel to allow ONLY numeric values.
My code does the following,
SpreadSheetGearHelper hlpr = new SpreadSheetGearHelper(excelFilePath);
cells = workbook.Worksheets[0].Cells;
hlpr.WorkSheet(0).Cells[string.Format("{0}:{0}", colName)].Validation.Add(SpreadsheetGear.ValidationType.WholeNumber, ValidationAlertStyle.Stop,
ValidationOperator.Between, "-9999999", "9999999");
hlpr.WorkSheet(0).Cells[string.Format("{0}:{0}", colName)].NumberFormat = "#";
hlpr.WorkSheet(0).Cells[string.Format("{0}:{0}", colName)].Validation.ErrorMessage = "Please enter a number";
But when I enter valid number within the range in excel it still says "Please enter a number".
Can someone please help me out with this
You're currently using the ValidationType.WholeNumber, which will only allow whole numbers such as 1, 2, 3 and not decimals such as 1.23. If you need to allow all numeric values and not just whole numbers you need to specify ValidationType.Decimal. Example:
using SpreadsheetGear;
...
IWorkbook workbook = Factory.GetWorkbook();
IWorksheet worksheet = workbook.ActiveWorksheet;
IRange cells = worksheet.Cells;
cells["A1"].Validation.Add(ValidationType.Decimal, ValidationAlertStyle.Stop,
ValidationOperator.Between, "-9999999", "9999999");
Take a look at this: Apply-Data-Validation-to-Excel-Cells-in-Csharp - CodeProject
sheet.Range["C9"].DataValidation.AllowType = CellDataType.Decimal;
sheet.Range["C9"].DataValidation.Formula1 = "-9999999";
sheet.Range["C9"].DataValidation.Formula2 = "9999999";
sheet.Range["C9"].DataValidation.CompareOperator = ValidationComparisonOperator.Between;
sheet.Range["C9"].DataValidation.InputMessage = "Type a number between -9999999-9999999 in this cell.";
I am not familar with SpreadsheetGear but the solution in this article works fine at my side.
Fixed it.
The Number Format of the cell was set to text, and that was why it was giving me an error every time I had entered a number (Even though the data validation was set correctly). Hence I added the below line of code to change it to "General"
hlpr.WorkSheet(0).Cells[string.Format("{0}:{0}", colName)].NumberFormat = "General";
Thank you guys for your time and responses.