Epplus: - 引用其他工作表中的单元格,然后如何从.xlsm文件中获取计算值

时间:2017-05-06 08:03:34

标签: c# .net epplus xlsm epplus-4

我正在使用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;
}

0 个答案:

没有答案