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;
Related
C#, NPOI
Good evening, i'm trying to fill in an empty template with values(numbers, but in string variable), that needs to be in cells.
So, when it came to formatting, i've stuck with styling. That means, when i wrote the value inside(in pre-formatted cell) i get just the string value, that was in array.
But, when i open file, click "edit cell", then apply without any changes, then cell is formatted, with the format, which was in a template
I tried to apply previous style in cell(basically just copy style in var, before setting a value, then re-apply style), but, it didnt help.
Here is the video of my doings
var cr = new CellReference(cellAndValue[i, 0]);
var row = sheet?.GetRow(cr.Row);
var cell = row?.GetCell(cr.Col);
var prevtype = cell.CellType;
var prevstyle = cell.CellStyle;
var dataformat = cell.CellStyle.DataFormat;
CellType type = cell.CellType;
cell.SetCellValue(cellAndValue[i, 0]);
How to resolve that?)
It was required to convert incoming data to int, before inserting it XD
int n;
if (Int32.TryParse(cellAndValue[i, 1], out n))
{
cell.SetCellValue(n);
}
else
{
cell.SetCellValue(cellAndValue[i, 1]);
}
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?
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
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.
I am trying to create double and number format cells in excel using NPOI library. I used code like
Dim cell As HSSFCell = row.CreateCell(j)
cell.SetCellValue(Double.Parse(dr(col).ToString))
In excel numbers are aligning right but when I check format it is showing in "General"
then I changed my code to like below
Dim cell As HSSFCell = row.CreateCell(j)
cell.SetCellValue(Double.Parse(dr(col).ToString))
Dim cellStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,#0.0")
cell.CellStyle = cellStyle
Then While opening file it is giving error and also taking so long to open. But Excel format showing in "Number"
error showing is like below.
How to fix this?
Take a look at this, are you creating a cellStyle object for each cell? If so don't. Try creating just a couple of styles before creating your cells and then apply these pre-defined styles to the cells you create.
To fix the too many different cell styles declare all styles outside of any loop you may be running.
I'm presumeing you 'j' would be the enumerator so i'll drop what you had in a corrected format for you.
Dim cellStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,#0.0")
For col = 0 To ColoumCounter
For j = 0 To Counter
Dim cell As HSSFCell = row.CreateCell(j)
cell.SetCellValue(Double.Parse(dr(col).ToString))
cell.CellStyle = cellStyle
Next
Next
This should work a bit better, by limiting the number of "New" styles.
Hare is a simple way to create double format in Excel Document USING NPOI.
//make NUMERIC Format in Excel Document // Author: Akavrelishvili
var eRow = sheet.CreateRow(rowIndex); //create new Row , rowIndex - it's integer, like : 1,2,3
eRow.CreateCell(0).SetCellValue(row["ProvidName"].ToString()); //create cell and set string value
double Amount = Convert.ToDouble(row["Amount"].ToString()); //convert string to double
eRow.CreateCell(1).SetCellValue(Amount); // create cell and set double value.
This is working version, I have used it a lots of projects.
Very Hard is to insert DateTime format in Excel, There no good example in Internet and I think it helps people to do it right way.
I show you code example:
//make Date Time Format in Excel Document // Author: Akavrelishvili
var eRow = sheet.CreateRow(rowIndex); //create new Row // rowIndex - it's integer, like : 1,2,3
ICellStyle cellDateStyle = workBook.CreateCellStyle(); //create custom style
cellDateStyle.DataFormat = workBook.CreateDataFormat().GetFormat("dd/mm/yyyy"); //set day time Format
eRow.CreateCell(3).SetCellValue(Convert.ToDateTime(row["Date"])); //set DateTime value to cell
eRow.GetCell(6).CellStyle = cellDateStyle; // Restyle cell using "cellDateStyle"
I hope it helps
Create a style then but this style for the column
ICellStyle _TextCellStyle = wb1.CreateCellStyle();
_TextCellStyle.DataFormat = wb1.CreateDataFormat().GetFormat("#");
sheet.SetDefaultColumnStyle(2, _TextCellStyle);