0

我正在使用EPPlus库来根据我们的需要获取所有要求。我没有得到如何获取在同一.xlsm文件中引用另一个工作表的单元格的计算值(启用宏)。有人可以帮忙吗?

点击后Calculate(),它被计算出来但我没有得到正确的值,打开文件后,在那个临时时间启用宏需要时间我可以看到错误的值,无论我得到什么....但是在完全打开文件后,这些值是变化,这是正确的计算,它是一个沉重的文件,许多公式与另一个工作表相互链接。

public ExcelResultModel CreateExcelInput(string path, ExcelInputModel model)
{
        //Old excel
        //FileInfo templateFile = new FileInfo(path + @"\Calculator15-zoetis5_Embryo_FDCv1.xlsm");


        FileInfo templateFile = new FileInfo(path + @"\myfile1.xlsm");
        string CalculateValues = string.Empty;
        ExcelResultModel result = new Model.ExcelResultModel();

        //using (FileStream templateDocumentStream = File.OpenWrite(templateFile.ToString()))
        //{
            using (ExcelPackage package = new ExcelPackage(templateFile))
            {
                //Open worksheet 1

                ExcelWorksheet worksheet = package.Workbook.Worksheets[2];
                package.Workbook.CalcMode = ExcelCalcMode.Automatic;

                worksheet.Cells["I15"].Style.Numberformat.Format = "#0\\%";
                worksheet.Cells["I16"].Style.Numberformat.Format = "#0\\%";
                worksheet.Cells["I17"].Style.Numberformat.Format = "#0\\%";
                worksheet.Cells["I18"].Style.Numberformat.Format = "#0\\%";
                worksheet.Cells["I19"].Style.Numberformat.Format = "#0\\%";

                //worksheet.Cells["F14"].Style.Numberformat.Format = "#0\\.00%";


                worksheet.Workbook.FullCalcOnLoad = true;
                worksheet.Cells["I15"].Value = model.val1;
                worksheet.Cells["I16"].Value = model.val2;
                worksheet.Cells["I19"].Value = model.val3;

                //worksheet.Cells["I17"].Value = model.val4;
                //worksheet.Cells["I18"].Value = model.val5;
                //worksheet.Cells["I19"].Value = model.val6;

                worksheet.Cells["T19"].Calculate();

                //worksheet.Cells["T15"].Calculate();
                //worksheet.Cells["T16"].Calculate();
                //worksheet.Cells["T17"].Calculate();
                //worksheet.Cells["T18"].Calculate();
                //worksheet.Cells["T19"].Calculate();

                // save our new workbook and we are done!
                //package.Workbook.CalcMode = ExcelCalcMode.Manual;
                //worksheet.Cells["F14"].Calculate();

                //worksheet["mysheet"].Cells["A1"].Calculate();

                package.Save();
                //System.Diagnostics.Process.Start(templateFile.ToString());

                var calculateOptions = new ExcelCalculationOption();
                calculateOptions.AllowCirculareReferences = true;

                bool isFormulaCircularReference = false;
                try
                {
                    //package.Workbook.Calculate();
                    package.Workbook.Worksheets["Long"].Calculate();
                    package.Workbook.Worksheets["genetics"].Calculate();
                }
                catch (CircularReferenceException ex)
                {
                    //If there is a circular reference this exception will be thrown
                    isFormulaCircularReference = true;
                }


                package.Save();

                if (worksheet.Cells["T15"].Address == "T15")
                {
                    object calcval = worksheet.Cells["T15"].Value;
                    result.DonorResult = Convert.ToString(calcval);

                }

        return result;
}
4

0 回答 0