EPPlus Formula Evaluation - c#

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.

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

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

How to read a cell value from Excel that contains a function in C#

I am writing a C# application that reads data from an Excel file. Everything was running smoothly until I attempted to read from a cell that used a formula.
I am pulling data from the sheet and trying to add the cumulative quantity, so in a loop, I'm using:
cntr = Cell(row, column);
NOTE: I'm paraphrasing rather than copy my actual code.
Anyways, if the actual cell value contains a number, this works, but if the cell contains a function, it returns the string
"=SUM(A1:A5)"
and I'm not sure how I can execute this in my C# code to retrieve the actual value of that cell.
Try
Cell(a,b).Value
instead of just Cell(a,b).
Also, the following approach should work
Excel.Range objRange = (Excel.Range)objSheet.Cells[rowN,colN];
variableName = objRange.get_Value(System.Missing.Type).ToString();
You may modify it for your datatype

How to copy format of one row to another row in Excel with c#

I am inserting data to Excel using C#. Whenever I add a new row to Excel using C# I want the same format as above row i.e, color, font and background color everything by programmatically.
It's an OLEDB insert.
Post insert, I want to apply the format of first row to the second row. With format painter from UI it's a straightforward job, I can't find a way to do the same with C#.
1) First you Need to get the Range you want to copy for e.g. RngToCopy
2) Then Set the Range where you want to insert.
3) use the below mentioned code snippet.
Range RngToCopy = ws.get_Range(StartCell, EndCell).EntireRow;
Range RngToInsert = ws.get_Range(StartCell, Type.Missing).EntireRow;
oRngToInsert.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown, oRngToCopy.Copy(Type.Missing));
//ws is the worksheet object, set StartCell and EndCell as per your requirement

Is there a simple way to get the "A1" style coordinates for a Range using the Excel COM interfaces?

I'm trying to get my program to dump out data into an Excel spreadsheet. At the moment, it simply puts the information into the proper cell calculating any calculated values as needed within my code. What I would really like to do is instead of writing out the calculated values is to write out the Excel formulas to the cells that contain calculated values. That way, if I give the Excel document to someone, they can change the data in it and the calculated values will update as expected.
The problem is that I don't know how to get the "A1" style coordinates which Excel would expect. Basically, I'm hoping that there's something like this available:
calculatedCell.Value = string.Format("=SUM({0})", range.Coordinates);
I know I can get the row and column index for the range, and using those I could evaluate the coordinates myself, I was just hoping there was a pre-packaged way of doing it.
There should be an Address property on Range.
Try this:
calculatedCell.Value = string.Format("=SUM({0})", range.get_Address(true, true,
XlReferenceStyle.xlA1,
false, null));

Categories