Excel, EPPlus, Formulas not properly evaluated - c#

I am trying to insert a formula into an excel column using EPPlus and evaluate it in Excel. To be clear: no, I don't need the result at the runtime of the program.
This is my code:
using (ExcelRange range = worksheet.Cells[1, 1, rowCounter - 1, worksheet.Dimension.End.Column])
{
ExcelTable table = worksheet.Tables.Add(range, $"someName");
table.ShowHeader = true;
// insert calculated column
ExcelTableColumn column = table.Columns[0];
column.Name = "Remaining Runtime";
column.CalculatedColumnFormula = "=DAYS([DateOfRepayment],TODAY())";
}
The range includes a column with the header DateOfRepayment, the formula works if I select a field and then deselect it or when I just select the formula in the cell in Excel and hit enter. Before these steps, I have an Invalid Name Error referencing the [DateOfRepayment]. However, using just =[DateOfRepayment] as the formula works fine.
How can I insert this formula in EPPlus so that it will work in Excel?
(I am using EPPlus version 4.5.1)

At the moment of this answer, the formula you are referring "=DAYS()" is not supported by EPPlus. I know you are not looking for the result at the runtime of the program. But you can either replace that formula by the one that is supported by EPPlus or generate result at runtime.
https://github.com/JanKallman/EPPlus/wiki/Supported-Functions

Related

C# EPPlus multiple cell formatting not applying

I am using EPPlus to generate excel file from data table. i have only two rows. i am applying % formatting on first row and $ formatting on second row but my two row has getting same % formatting for first two row which is wrong. i am not being able to capture the reason why this is happening. why second formatting not being applied on second row which is $ formatting.
See this line where i use range to apply formatting.
ws.Cells["C0:P0"].Style.Numberformat.Format = "#,###,##0.0%;(#,###,##0.0%)";
ws.Cells["C1:P1"].Style.Numberformat.Format = "$##,##0.0;($##,##0.0)";
in the above code i mention cell range with formatting but my two row getting only first formatting and second formatting not consider...not clear why this is happening?
Sample Code
using (OfficeOpenXml.ExcelPackage obj = new OfficeOpenXml.ExcelPackage(FileLoc))
{
// creating work sheet object
OfficeOpenXml.ExcelWorksheet ws = obj.Workbook.Worksheets.Add("Vertical");
// freezing work sheet columns and rows
ws.View.FreezePanes(2, 3);
// exporting data to excel
ws.Cells["A1"].LoadFromDataTable(selected, true);
// setting calumns as autofit
ws.Cells[ws.Dimension.Address].AutoFitColumns();
//fixing height of column
ws.Row(1).Height = 16;
ws.Row(1).Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Row(1).Style.Fill.BackgroundColor.SetColor(Color.LightGray);
obj.Save();
ws.Cells["C0:P0"].Style.Numberformat.Format = "#,###,##0.0%;(#,###,##0.0%)";
ws.Cells["C1:P1"].Style.Numberformat.Format = "$##,##0.0;($##,##0.0)";
}
screen shot of excel data. see first two line in picture and definitely understand #,###,##0.0%;(#,###,##0.0%) this format is applying on first two row but in my code i have given different format for second records.
please help me to find the wrong things in my code. thanks
Well, there are a couple of errors. First, you're saving before setting the formatting, so it's not being applied.
Second, Excel addresses are base 1, it doesn't exist "C0" and "P0". Also note that in the first row is the columns titles, so you probably want rows 2 and 3. Try the following:
ws.Cells["C2:P2"].Style.Numberformat.Format = "#,###,##0.0%;(#,###,##0.0%)";
ws.Cells["C3:P3"].Style.Numberformat.Format = "$##,##0.0;($##,##0.0)";
obj.Save();

Colorize Entire Row of Cells Having a Text/Value on a Column in Excel using EPPlus

I need to format entire row of cells having a value on a column using EPPlus.
For example, colorize rows having text of "yes" on its 'H' column.
In order to achieve this I used excel conditional formatting rules(EPPlus) but I could only format cells, not entire row. How can I accomplish this?
Given that worksheet is an ExcelWorksheet and rowNumber is... that:
var rangeAddress = $"{rowNumber}:{rowNumber}";
var expression = worksheet.ConditionalFormatting
.AddExpression(new ExcelAddress(rangeAddress));
expression.Style.NumberFormat.Format = "0.00";
// not trying to be lazy here - you already have your formula.
expression.Formula = "IF(something)";

How to set the formula to the sum of a column in c# interop

How to set the formula to the sum of a column in c# interop ?
I tried
sheet.Range["O4"].Formula = string.Format("=SUM({0})", table.ListColumns["MONTANT"].DataBodyRange.Address);
But when i add a row to the table the sum is not correct, It does not take the new line.
When you sum a column in Excel, say, AX, you write SUM(AX:AX). Therefore, the format should be SUM({0}:{0}):
sheet.Range["O4"].Formula = string.Format("=SUM({0}:{0})", table.ListColumns["MONTANT"].DataBodyRange.Address);

In ClosedXML, how to sort a worksheet by a column?

In C# using asp/MVC the app generates an Excel .xlsx file based on data thats filled in to the specific columns, works great.
But the goal is to provide additional worksheets that use the same columns but sort on specific colums, such as Column "J"
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Proj Info");
var ws2 = wb.Worksheets.Add("Sort By Dates");
The worksheet ws has values filled in by variables or formulas, the data is correct, but cannot make it sort on a column
ws.AutoFilter.Column("J"); //no, nothing changes
ws.Column("J").Sort(); -> this shifts all the columns up but does not sort
ws.Column("J").Sort(XLSortOrder.Ascending); ->same, doesnt sort only shifts
Update: ws.Sort(9); worked in sorting, but the problem is that Column 10 has a Formula, and I need to sort on that Column.
ws.Cell("J" + c).FormulaR1C1 = "=C$2-F" + c;
With this? it WILL NOT SORT. The ws.Sort(10); works when the cell contains a final value, but when its got the Formula? Is there any workaround to force the Excel page to sort after its implemented the formula's in each cell?
You are not sorting the table, but the values in column J.
Try this:
ws.Sort("Column10");

EPPlus Formula Evaluation

When using EPPlus, I am trying to insert the result of a formula into a cell
ie:
Worksheet.Cells["A4"].Value = 3
Worksheet.Cells["A5"].Value = 4
Worksheet.Cells["A6"].Formula = "=SUM(A3:A4)"
Worksheet.Cells["A6"].Calculate()
In the worksheet i will see the formula in the Formula bar, but what I would like is to evaluate the formula in EPPlus and insert the value into the cell. So when clicking into the cell all i see is 7 and not =SUM(A3:A4)
The reason for this, is because I have large worksheet (for business reasons) and having the formulas calculate when opening means the sheet takes about 20 seconds to load
just to illustrate swmal answer :
If you want to calculate and remove the actual formula before you send the workbook to the client you should set the Formula property to string.Empty after you have called Calculate(). The calculated value is stored in the Value property of the cell.

Categories