Excel sheet Pre Populated Formulas values not refreshed

白昼怎懂夜的黑 提交于 2019-12-24 21:05:14

问题


I have a requirement where excel file ( i will treat this is an a template) which has a pre populated formulas ( for close to 10k rows), when i populate the dependent column values the formulas calculations are not updated or refreshed. please help me with the same.

The formulas will be populated for 10k rows but while populating the data i may be populating the data just for 100 rows but user can add many more records later so we populate the formulas for 10k rows.

Code below:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.IT.Sales.RelationshipManagement.Segmentation.DataContracts;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {           
        static void Main(string[] args)
        {           
            PopulateExcel("E:\\Formula.xlsx");            
            Console.WriteLine("Hello World!");
        }

        private static void PopulateExcel(string Path)
        {   
            WorkbookPart WP;
            //WorksheetPart WSP;
            String rId;
            Worksheet WS;
            SheetData StD;
            Row R1;
            Row R2;
            Row R3;
            Cell C1;
            Cell C2;
            Cell C3;
            CellFormula CF;
            CalculationChainPart CP;
            CalculationChain CC;
            CalculationCell CCell;
            Workbook Workbook;
            Sheet ss;
            Sheet Sheet;

            using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(Path, true))
            {
                // Get the SharedStringTablePart. If it does not exist, create a new one.
                WP = spreadSheet.WorkbookPart;
                var wsp = spreadSheet.WorkbookPart.WorksheetParts.FirstOrDefault(); //.ElementAt(sheetnumber);
                SheetData sheetData = wsp.Worksheet.Descendants<SheetData>().LastOrDefault();

                R1 = GetRow(sheetData, 2);
                C1 = new Cell();
                SetCell(C1, "A1", 1);
                sheetData.Append(R1);

                R1.Append(C1);

                C2 = new Cell();
                SetCell(C2, "B1", 2);

                R1.Append(C2);    

                spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
                spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
                wsp.Worksheet.Save();

                // Close the document.
                spreadSheet.Close();
            }   
        }

        private static Row GetRow(SheetData wsData, UInt32 rowIndex)
        {
            var row = wsData.Elements<Row>().
            Where(r => r.RowIndex.Value == rowIndex).FirstOrDefault();
            if (row == null)
            {
                row = new Row();
                row.RowIndex = rowIndex;
                wsData.Append(row);
            }
            return row;
        }
        private static void SetCell(Cell cell, string Reference, int Value)
        {
            cell.CellReference = Reference;
            cell.DataType = CellValues.Number;
            cell.CellValue = new CellValue();
            cell.CellValue.Text = Value.ToString();

        }    
    }
}

Please help me with the code.

来源:https://stackoverflow.com/questions/48134346/excel-sheet-pre-populated-formulas-values-not-refreshed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!