EPPlus: Cell displayed emty but return 0 - c#

There is an Excel file with a pivot table. In the file, I see empty cells, but I programmatically get the value "0"
The contents of the cell I get in the standard way:
var text = ws.Cells[r, c].Text;
Is it possible to somehow get an empty string instead of "0" using EPPlus?

Related

How can I set color of specific characters in a cell value of excel for epplus?

If my cell value is "REGION AND COUNTRY" and I want to only set font color of "COUNTRY" or "TRY" to red in it then how can do that using epplus?
Ex : Worksheet.Cells[1,2].Style.Font.Color.SetColor(Color.Red);
This changes the font color of complete cell value.
Thanks
You'll want to use rich text formatting for that.
This example hardcodes the text without looking at the original contents of the cell, but you could read the cell and split it into runs with individual formatting.
var cell = Worksheet.Cells[1, 2];
cell.RichText.Clear(); //Incase you're running this multiple times and don't want duplicate text.
cell.RichText.Add("REGION AND ", false);
var counRun = cell.RichText.Add("COUN", false);
var tryRun = cell.RichText.Add("TRY", false);
counRun.Color = System.Drawing.Color.Red;
tryRun.Color = System.Drawing.Color.Red;

Read Excel Formulas

I need to get all the excel formulas from the worksheet.I am using Range.SpecialCells and iterating through the each cell to get formula and it is really slow.Is there any better solution to get only formulas as list of strings?
Sample code:
Excel.Worksheet worksheet = Workbook.Worksheets["Sheet1"];
Range range = worksheet.UsedRange.SpecialCells(xlCellTypeFormulas);
foreach(Cell cell in range.Cells)
{
string formula = cell.formula.ToString();
//some code
}
You can try to use the range.formula property.
For example:
In cell A3, the cell formula is '=DATE(D3,C3,1)'
In cell B3, the cell formula is '=A3'
With the code:
Range r = activeWorksheet.get_Range("A3","B3");
var c = r.Formula;
The variable c will contain sort of like a list of the cells specified in that range and their respective formula.
Output of C:
[1,1] = "=DATE(D3,C3,1)"
[1,2] = "=A3"
This is a totally different way to do it, but it might help you,
Just press CRTL + ` (backquote), this will make all the formula visible in the excel sheet. You could then search what you want or copy paste in a txt file to make searches and modifications.
If you are just searching for some strings within a formula, you can use CTRL + F , select look in formulas and search the strings

Get formula result from Excel cell using Spire.xls

I'm trying to read data in a xlsx file (in C# WPF with spire xls) but when the cell contains a formula I can only get the formula and I don't know how to get the result
I've tried this :
wb.LoadFromFile("...");
wb.CalculateAllValue();
colsheet = wb.Worksheets[6];
SwitchName = colsheet.Range["B1"].Value;
and with that I get : "=IF(OR(I215=0,J215=0),"",I215+J215-1)" but I want the result and not the formula. How can I do that?
You should use Object ob = colsheet.Range["B1"].FormulaValue; instead of this line SwitchName = colsheet.Range["B1"].Value;

Keep excel cell format as text with "date like" data

This seems silly, but I haven't been able to get my values in the format of #/#### to write as the literal string rather than becoming formatted as a date within excel.
I'm using ClosedXML to write to excel, and using the following:
// snip
IXLRangeRow tableRow = tableRowRange.Row(1);
tableRow.Cell(1).DataType = XLCellValues.Text;
tableRow.Cell(1).Value = "2/1997";
// snip
Looking at the output excel sheet I get in the cell 2/1/1997 - even though I'm setting the format as text in code, I'm getting it as a "Date" in the excel sheet - I checked this by right clicking the cell, format cell, seeing "date" as the format.
If I change things up to:
// snip
IXLRangeRow tableRow = tableRowRange.Row(1);
tableRow.Cell(1).Value = "2/1997";
tableRow.Cell(1).DataType = XLCellValues.Text;
// snip
I instead get 35462 as my output.
I just want my literal value of 2/1997 to be displayed on the worksheet. Please advise on how to correct.
try this
ws.Cell(rowCounter, colCounter).SetValue<string>(Convert.ToString(fieldValue));
Not sure about from ClosedXML, but maybe try Range.NumberFormat (MSDN Link)
For example...
Range("A1").NumberFormat = "#"
Or
Selection.NumberFormat = "#/####"
Consider:
tableRow.Cell(1).Value = "'2/1997";
Note the single quote.
ws.Cell(rowCounter, colCounter).Value="'"+Convert.ToString(fieldValue));
Formatting has to be done before you write values to the cells.
I had following mechanism, run after I make worksheet, right before I save it:
private void SetColumnFormatToText(IXLWorksheet worksheet)
{
var wholeSheet = worksheet.Range(FirstDataRowIndexInExcel, StartCellIndex, RowCount, HeaderCount);
wholeSheet.Style.NumberFormat.Format = "#";
}
which didn't do squat.
Doing it before I write values to the cells in a row did it.
worksheet.Range(RowIndex, StartCellIndex, RowIndex, EndCellIndex).Style.NumberFormat.Format = "#";
with cell value assignments following immediately after.

converting into integer in excel using EPPlus (asp.net)

I am loading data into excel from datatable using LoadFromDataTable method then changed cell format to integer still it is showing error "The number in this cell is formatted as text or preceded by apostrophe".
cell was showing to right side only and number format only on cell property.
still I am not understanding why I am getting this error??.
Dim wsManufacturing As ExcelWorksheet = pck.Workbook.Worksheets.Add("Manufacturing")
wsManufacturing.Cells("A1").LoadFromDataTable(dtManufacturing, True)
Using col As ExcelRange = wsManufacturing.Cells(2, 2, 2 + dtManufacturing.Rows.Count, 2)
col.Style.Numberformat.Format = "#,##0"
col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right
End Using
You can do like this:
//strValue="98.5%";
double realValue=double.Parse(strValue.Replace("%", string.Empty));
Worksheet.Cells[row + 1, col].Style.Numberformat.Format = "#0\\.00%";
Worksheet.Cells[row + 1, col].Value = realValue;
Changing the format from Text to Number does not change the nature of the entry that was in the cell prior to the format change.
To change entries that were originally textual representations of numbers
Change the cell format to Number
Enter a 1 in some cell
Edit/Copy
Select your cell(s) with the text numbers
Paste Special / Multiply
Delete the original 1

Categories