Epplus formula error - c#

I'm using Epplus to put a formula into a cell. If I put this formula manually into the Excel cell it works:
=SUM(E4;G4)
But when I put in code with Epplus it doesn't work:
xls.ActiveSheet.Cells(8, 4).Formula = "SUM(E4;G4)"
Is there something special needed when I SUM two cells?
If I do the same with a range of cells it works, but with specific cells not.
This works (Range):
xls.ActiveSheet.Cells(8, 4).Formula = "SUM(E4:G4)"

Try changing this
xls.ActiveSheet.Cells(8, 4).Formula = "SUM(E4;G4)";
into
xls.ActiveSheet.Cells(8, 4).Formula = "SUM(E4,G4)";
Somehow using ; in a formula messes things up. Could be a bug...

Related

Using EPPlus library to replace formulas by their values in Excel

I've read that Microsoft.Office.Interop.Excel would be the easiest way to replace formulas by their values in Excel but it requires to have Office installed. Since I will need to deploy on a Windows Server (2008 or 2012), I am looking for the best and/or simplest way to accomplish that using EPPlus.
Adding formulas is well documented, e.g.
currentWorksheet.Cells["C4"].Formula = "SUM(C2:C3)";
But I cannot find any example of replacing entire worksheets of formulas by their equivalent values. Basically the Copy followed by the Paste Special option in Excel.
I dont think there is any kind of function built into Epplus that will do that for you en masse. But you can take advantage of the fact that the Cells collection of the Worksheet only contains entries for cells with content. So something like this should not be too painful performance-wise:
currentWorksheet.Cells["C2"].Value = 5;
currentWorksheet.Cells["C3"].Value = 15;
currentWorksheet.Cells["C4"].Formula = "SUM(C2:C3)";
currentWorksheet.Cells["D2"].Value = 15;
currentWorksheet.Cells["D3"].Value = 25;
currentWorksheet.Cells["D4"].Formula = "SUM(D2:D3)";
//Calculate the formulas and the overwrite them with their values
currentWorksheet.Cells.Calculate();
foreach (var cell in currentWorksheet.Cells.Where(cell => cell.Formula != null))
cell.Value = cell.Value;
I know this is 3 years old, but if you're reading this now, EPPlus now has the function .ClearFormulas() that does just that, simply do
yourSheetHere.Calculate();
yourSheetHere.ClearFormulas();
and you're good to go.

How to color excel column ranges between AC to AE in c# code

I'm trying to color a particular range of cells in exsisting excel using C# code.
Below is the code I tried but it throws an exception.Let me know how to proceed.
excelWorkSheet4.get_Range("AC", "AE").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
I got the answer for this one below code works for any particular range after "Z"
excelWorkSheet4.get_Range("AC:AC", "AE:AE").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);

how to add conditional formatting on cells with values greater than a specific constant value using epplus

I have made an excel sheet programmatically. and now I want to add conditional formatting on a specific cell range.
The formatting type is all the cells with values greater than 0 (>0)
How to go about doing it?
In excel I can do it using an inbuilt formula of Cell Values Greater Than. But how to embed it in excel using C# and epplus?
i coudnt find an exact solution to this problem. so adding my own solution that works
var cellAddress = new ExcelAddress(
<startingRow>,
<startingcolumn>,
<endingRow>,
<endingColumn>);
var cf = ws.ConditionalFormatting.AddGreaterThan(cellAddress);
cf.Formula = "0";
cf.Style.Fill.BackgroundColor.Color = Color.LightGreen;

Edit Range NumberFormat in existing document

I can't change column format in an existing Excel document (xlsx). Columns content are numbers actually but shown as text and therefore green triangle appear telling that cells shown as text.
So I open this document in C# app and do the following thing:
sheet_.Range[sheet_.Cells[1, 2], sheet_.Cells[rowNum, 2]].EntireColumn.NumberFormat = "0";
But it doesn't change column to appear content as numbers (they remain aligned by left side)
I know this is an old post, but I've been dealing with the same problem. I receive .xlsx files that already have green triangles denoting "Number as Text" errors. I couldn't find a way to programmatically run the Excel error-checking command "Convert to Number" that you can do by clicking in Excel, and changing the NumberFormat on cells with these errors didn't work for me, but I was able to "refresh" the cell format by using the TextToColumns method.
int lastCol = sheet.UsedRange.Columns.Count;
if(lastCol > 1)
{
for (int i = 1; i <= lastCol; i++)
{
sheet.Columns[i].TextToColumns(Type.Missing, XlTextParsingType.xlDelimited, XlTextQualifier.xlTextQualifierNone);
}
And from there you can change the NumberFormat. I happened to have long integers that were getting put into scientific notation, so I used this to make them regular integers again:
sheet.Cells.NumberFormat = "#";
(PS, if anyone finds a definitive guide on the symbols to use for customized NumberFormats, I'm still trying to find one!)
Try to access to cell value over get_Range method! for example and for what number format you want, lets say that you have in your excel cell this number : 1546,65
sheet_.get_Range("P10", "Q10").NumberFormat = "0"; // returns 1546
sheet_.get_Range("P10", "Q10").NumberFormat = "0,00"; // returns 1546,65
sheet_.get_Range("P10", "Q10").NumberFormat = "#.##0,00"; // returns 1.546,65
And you can play with these number formats!
Hope it helps you
I didn't find ideal solution to this issue and ended with the following:
sheet_.get_Range("A1", "A100").NumberFormat = "0";
for(int i = 1; i <= 100; i++)
{
sheet_.Cells[1, i].Value = sheet_.Cells[1, i].Value;
}
I know this is a old post but I stumbled over this and have a solution for this. Have you tried to not assign any NumberFormat? by default excel decides based on the cell content so you wouldnt get green triangle if you have numbers which are stored as text. If you want read values based on data type then refer this post
http://www.codeproject.com/Articles/335589/Export-Multiple-Datasets-to-Multiple-Excel-sheets
For me using the Style and the NumberFormatLocal solved the problem:
sheet_.get_Range("A1", "A100").Style.NumberFormatLocal = "0";

Some Excel cells stay unpopulated, with C#

I'm writing an application that opens an existing .xlsx file and writes to certain cells.
Some cells write correctly, where others just stay blank?
Any ideas?
This is a snippet of code
The same code for the cells that are and arent working, except that the index's changed
oSheet.Cells[3, 15] = "1"; // this doesnt write to the cell
oSheet.Cells[7, 7] = "1"; // this writes to the cell
All that i could think is that there is a formatting issue in the Excel file?
I've been working in Excel for years and find quirks like this all the time. If you are in .NET 4.0 try this:
using Excel = Microsoft.Office.Interop.Excel
//Other Class code
var range = oSheet.Cells[3, 15];
range.Value2 = "1";
Otherwise, try this:
using Excel = Microsoft.Office.Interop.Excel
//Other Class code
Excel.Range range = (Excel.Rang)oSheet.Cells[3, 15];
range.Value2 = "1";
Value2 seems to work more consistently, so I generally recommend using it.
Cheers!
Anthony was right, I had my columns and rows switched.

Categories