Formula cell doesn't update in template using excelpackage

Mar 11, 2011 at 3:10 AM

Dear all,

I'm using ExcelPackage to export excel file based on template here is my template:

A Price 1 2 2 5

Here is my C# code to export excel file:

using System;
using System.IO;
using System.Reflection;
using OfficeOpenXml;

namespace ExcelDemo
{
    class Program
    {
        //HOW TO:
        //http://excelpackage.codeplex.com/license

        static void Main(string[] args)
        {
            string templateDir = String.Format("{0}{1}", Assembly.GetExecutingAssembly().Location, @"..\..\..\..\..\template\");
            FileInfo newFile = new FileInfo(templateDir + "Book1.xlsx");
            FileInfo template = new FileInfo(templateDir + "template.xlsx");
            using (ExcelPackage xlPackage = new ExcelPackage(newFile, template))
            {
                ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["Sheet1"];
                ExcelCell cell;
                const int startRow = 2;
                int row = startRow;

                for (int r = 0; r  startRow) worksheet.InsertRow(row);
                    worksheet.Cell(row, 1).Value = "$10";
                    row++;
                }

                //worksheet.CreateSharedFormula(worksheet.Cell(startRow, 1), worksheet.Cell(row, 1));

                xlPackage.Save();                                
            }
        }
    }
}

And here is my excel output:

A1: Price 10 10 10 10 10 10 10 10 10 10 2 2 5

So strange! because I just loop and push 10 records into spreadsheet but I got 2 and 2 between number 5 (contain SUM formula) and 10.

Please correct me, Thanks

 

Mar 24, 2011 at 7:42 PM

Hi,

I think you have simply pushed the 2, 2 and 5 down. If i understand your description of your template, you have that 2, 2 and 5 in there already, so it isn't being put there by your method, just moved.

chrs