Search code examples
c#excelgembox-spreadsheet

how can i calculated values after Worksheet.Calculate()?


i tried Trial version of Gembox.SpreadSheet.

when i Get Cells[,].value by for() or Foreach(). so i think after Calculate() & get Cell[].value, but that way just take same time,too. it take re-Calculate when i Get Cell[].value.

workSheet.Calcuate(); <- after this, values are Calculated, am i right?
for( int i =0; i <worksheet.GetUsedCellRange(true).LastRowIndex+1;++i)
{
    ~~~~for Iteration~~~
     var value =  workSheet.Cells[i,j].Value; <- re-Calcuate value(?)
}

so here is a Question. Can i Get calculated values? or you guys know pre-Calculate function or Get more Speed?


Solution

  • Unfortunate, I'm not sure what exactly you're asking, can you please try reformulating your question a bit so that it's easier to understand it?

    Nevertheless, here is some information which I hope you'll find useful.
    To iterate through all cells, you should use one of the following:

    1.

    foreach (ExcelRow row in workSheet.Rows)
    {
        foreach (ExcelCell cell in row.AllocatedCells)
        {
            var value =  cell.Value;
            // ...
        }
    }
    

    2.

    for (CellRangeEnumerator enumerator = workSheet.Cells.GetReadEnumerator(); enumerator.MoveNext(); )
    {
        ExcelCell cell = enumerator.Current;
        var value = cell.Value;
        // ...
    }
    

    3.

    for (int r = 0, rCount = workSheet.Rows.Count; r < rCount; ++r)
    {
        for (int c = 0, cCount = workSheet.CalculateMaxUsedColumns(); c < cCount; ++c)
        {
            var value = workSheet.Cells[r, c].Value;
            // ...
        }
    }
    

    I believe all of them will have pretty much the same performances.
    However, depending on the spreadsheet's content this last one could end up a bit slower. This is because it does not exclusively iterate only through allocated cells.

    So for instance, let say you have a spreadsheet which has 2 rows. The first row is empty, it has no data, and the second row has 3 cells. Now if you use 1. or 2. approach then you will iterate only through those 3 cells in the second row, but if you use 3. approach then you will iterate through 3 cells in the first row (which previously were not allocated and now they are because we accessed them) and then through 3 cells in the second row.

    Now regarding the calculation, note that when you save the file with some Excel application it will save the last calculated formula values in it. In this case you don't have to call Calculate method because you already have the required values in cells.
    You should call Calculate method when you need to update, re-calculate the formulas in your spreadsheet, for instance after you have added or modified some cell values.

    Last, regarding your question again it is hard to understand it, but nevertheless:

    • Can i Get calculated values?
      Yes, that line of code var value = workSheet.Cells[i,j].Value; should give you the calculated value because you used Calculate method before it. However, if you have formulas that are currently not supported by GemBox.Spreadsheet's calculation engine then it will not be able to calculate the value. You can find a list of currently supported Excel formula functions here.

    • or you guys know pre-Calculate function or Get more Speed?
      I don't know what "pre-Calculate function" means and for speed please refer to first part of this answer.