Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding formula to Excel with C# - making the formula shown

I wanted to add formulas to an Excel workSheet.
I managed to do so with the Formula property.

The problem is that when I open the worksheet in Excel, I can see that the formula works - but I can only see the result in the cell. I can't see the formula that was calculated in the Formula Bar at the top of Excel.

Obviously if I enter a formula in Excel itself I can see the result in the cell and the formula in the Formula Bar.

Some of my code:

for (int i = 0; i < nOfColumns / 3; i++)
{
    Range cells = workSheet.Range[workSheet.Cells[2, i * 3 + 3], workSheet.Cells[lastRowNumber, i * 3 + 3]];
    cells.FormulaR1C1 = "=IF(EXACT(RC[-2],RC[-1]),TRUE,ABS(RC[-2]/RC[-1]-1))";
}

below is a test code. even after I save the workbook - the FormulaHidden is false and I can successfully retrieve the formula insterted. really frustrated

        Microsoft.Office.Interop.Excel.Application excelApp = null;
        Workbooks workBooks = null;
        Workbook workBook = null;
        Worksheet workSheet;

        try
        {

            excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.DisplayAlerts = false;

            workBooks = excelApp.Workbooks;
            workBook = workBooks.Open(filePath, AddToMru: false);
            workSheet = workBook.Worksheets.get_Item(1);

            int nOfColumns = workSheet.UsedRange.Columns.Count;
            int lastRowNumber = workSheet.UsedRange.Rows.Count;

            Range rng = workSheet.Range["C1"];
            rng.Formula = "=SUM(B2:B4)";
            String formula = rng.Formula; //retrieve the formula successfully

            rng.FormulaHidden = false;
            workSheet.Unprotect();

            workBook.SaveAs(filePath, AccessMode: XlSaveAsAccessMode.xlExclusive);

            formula = rng.Formula;  //retrieve the formula successfully
            bool hidden = rng.FormulaHidden;

        }
        catch (Exception e)
        {
            throw;
        }
        finally
        {
            if (workBook != null)
            {
                workBook.Close();
                workBook = null;
            }
            if (workBooks != null)
            {
                workBooks.Close();
                workBooks = null;
            }
            if (excelApp != null)
            {
                excelApp.Quit();
                excelApp = null;
            }
        }
    }

Anyone know how to make the formula shown, when adding the formulas programatically ?

like image 288
Amico Avatar asked Jan 16 '12 10:01

Amico


1 Answers

finally !!! figured it out. this behavior is caused by the SaveAs flags. changed

workBook.SaveAs(filePath, AccessMode: XlSaveAsAccessMode.xlExclusive);

to

workBook.SaveAs(filePath, AccessMode: XlSaveAsAccessMode.xlShared);

now the only thing left is to understand what exactly is the different between the two flags. :)

like image 97
Amico Avatar answered Sep 21 '22 14:09

Amico