ClosedXML does not give me option to format column - c#

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

Related

How to set up conditional formatting icon sets in Excel using C# NPOI? (C#, NPOI, Excel, MultistateFormatting, IconSet, IconSet Thresholds)

Since I could not find any helpful posts or guides as to how to set up an Excel icon set with conditional formatting in NPOI, I would like to post a solution inspired by this answer.
You can format IconSets in NPOI by using this code (I'm using NPOI 2.5.6):
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.OpenXmlFormats.Spreadsheet;
var cf = oSheet.GetCTWorksheet().AddNewConditionalFormatting(); //oSheet is an XSSFSheet.
cf.sqref = "C1"; //Or "C1:C2"
var rule = cf.AddNewCfRule();
rule.type = ST_CfType.iconSet;
rule.priority = 1;
var IconSet = rule.AddNewIconSet();
IconSet.iconSet = ST_IconSetType.Item3Signs; //You can choose another IconSet type here.
IconSet.showValue = true;
IconSet.reverse = true; //Reverses the default order of icons.
var value_1 = IconSet.AddNewCfvo();
value_1.type = ST_CfvoType.num; //Depending on what you need, you can choose precentages or other types. I found that using numbers when defining thresholds works better.
value_1.val = "0"; //Why you should start with a zero is further below.
var value_2 = IconSet.AddNewCfvo();
value_2.type = ST_CfvoType.num;
value_2.val = "10";
var value_3 = IconSet.AddNewCfvo();
value_3.type = ST_CfvoType.num;
value_3.val = "20";
Why would you need to add a zero to your thresholds?
I found out that a correct icon set is defined in the XML-file for the Excel sheet as follows:
<conditionalFormatting sqref="C1">
<cfRule type="iconSet" priority="1">
<iconSet iconSet="3Signs" reverse="1">
<cfvo type="num" val="0" />
<cfvo type="num" val="10" />
<cfvo type="num" val="20" />
</iconSet>
</cfRule>
</conditionalFormatting>
A note on the threshold values:
Since threshold is a string value you should be careful when casting numerical values as strings. For example, floating point numbers when cast to string can receive scientific notation or commas.
In order to correctly cast a floating point number you should get rid of scientific notation and replace comma with dot since Excel would not accept scientific notation and commas.
This helped me overcome this problem.

c# excel cell format to hh:mm:ss

I am having a problem while converting text into the format [hh:mm:ss].
When I double click, formatting is fixed and cell value is automatically aligned to the right.
I am using [SpreadsheetStreams.net] NuGet package to create an excel file.
Here what I have already tried:
1.
writer.AddCell(TimeSpan.Parse(columnNames[i].ToString()))
2.
Style test = new Style();
NumberFormat format = new NumberFormat(NumberFormatType.Custom);
format.Custom = "hh:mm";
test.NumberFormat = format;
writer.AddCell(TimeSpan.Parse(columnNames[i].ToString()), test) OR
writer.AddCell(columnNames[i].ToString(), test)
Please refer to this image:
How can I format the cell to [hh:mm:ss]?
After trying many solutions I found a way :
Create a Style object having a custom type like below :
Style timeStyle = new Style();
NumberFormat format = new NumberFormat(NumberFormatType.Custom);
format.Custom = "hh:mm";
timeStyle.NumberFormat = format;
Then pass this as the second parameter and first parameter as a day (in double)
writer.AddCell(TimeSpan.Parse(columnNames[i]).TotalSeconds / 86400, timeStyle );

NPOI Excel / C# Need formula to programatically change background for conditional formatting

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);

How to set Text format to a particular column in Excel using Aspose Cells

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);

Formula conditional EPPlus

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.

Categories

Resources