简体   繁体   English

C# 使用 Microsoft.Office.Interop.Excel 错误设置范围 .Formula 到范围 .Value2

[英]C# Using Microsoft.Office.Interop.Excel Error Setting Range .Formula to Range .Value2

I have C# code writing to Excel.我有 C# 代码写入 Excel。 The following code works:以下代码有效:

var probForExcel = new string[profileSize, 1];

Range range3 = worksheet.Range["C5", "C" + (startRow + profileSize - 1)];

range3.Value = probForExcel;

range3.NumberFormat = "0.00000000000E+0";

range3.Formula = range3.Value2; //NEEDED FOR ACTUAL FORMULAS otherwise writes in xls as string

Now I am trying to use a List of Type Range:现在我正在尝试使用类型范围列表:

 var formulaForExcel = new List<string[,]>();
            var formularange1 = new List<Range>();

            for (var i = 0; i < profileSize; i++)
            {
                var count = formulas[i].Count();
                formularange1.Add(worksheet.Range[worksheet.Cells[rowTracker, 2], worksheet.Cells[rowTracker, count]]);
                formulaForExcel.Add(new string[1, count]);
                var j = 0;
                foreach (var formula in formulas[i])
                {
                    formulaForExcel[i][0, j] = formula;
                    j++;
                }

                formularange1[i].Value = formulaForExcel[i];
                formularange1[i].Formula = formularange1[i].Value2;//CRASHES HERE
                rowTracker++;
            }

The program runs without the following line of code:该程序在没有以下代码行的情况下运行:

formularange1[i].Formula = formularange1[i].Value2;//CRASHES HERE

System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC' System.Runtime.InteropServices.COMException: '来自 HRESULT 的异常:0x800A03EC'

But it prints the formula as a string without that line of code and I have to go into each cell and hit enter for it to evaluate as a formula.但是它将公式打印为没有那行代码的字符串,我必须进入每个单元格并按 Enter 键才能将其作为公式进行评估。 Why does the syntax work, no compile errors.为什么语法有效,没有编译错误。 And it runs in the first part of the code, where it's not in a list.它运行在代码的第一部分,它不在列表中。 I put Range in a list and it crashes.我把 Range 放在一个列表中,它崩溃了。 Unsure if the .Formula is causing the crash or the .Value2.不确定 .Formula 是导致崩溃还是 .Value2。

I get the same error if I do this如果我这样做,我会得到同样的错误

Range temp = formularange1[i];

temp.Formula = temp.Value2; //CRASHES HERE

Found another post about Excel that helped me find my bug.找到了另一篇关于 Excel 的帖子,帮助我找到了我的错误。 Because my excel formulas were automatically being built and very large, I didn't realize some of my formulas ended in a + sign.因为我的 excel 公式是自动构建的并且非常大,所以我没有意识到我的一些公式以 + 号结尾。 Because I was adding things and checking length, some ended in +.因为我在添加东西并检查长度,所以有些以 + 结尾。 So I was doing a find and replace "=" with "=" and that would cause all the formulas to evaluate.所以我正在做一个查找并将“=”替换为“=”,这将导致所有公式进行评估。 Except for the ones with a + at the end, excel threw an error.除了末尾带有 + 的那些,excel 抛出了错误。 Once I found this and updated my formula builder, now this works:一旦我找到了这个并更新了我的公式生成器,现在这有效:

        for (var i = 0; i < profileSize; i++)
        {
            var count = formulas[i].Count();

            sumrangeForExcel[i, 0] = "=sum(C" + rowTracker + ":c"     + rowTracker + ")";

            formularange1.Add(worksheet.Range[worksheet.Cells[rowTracker, 3], worksheet.Cells[rowTracker,3+ count-1]]);
            formulaForExcel.Add(new string[1, count]);
            var j = 0;
            foreach (var formula in formulas[i])
            {
                formulaForExcel[i][0, j] = formula;
                //formularange1[i][0, j].Formula = formularange1[i][0, j].Value2; Erased my formulas
                j++;
            }

            formularange1[i].Value = formulaForExcel[i];
            formularange1[i].Formula = formularange1[i].Value2;//NO CRASHING

            formularange1[i].NumberFormat = "###,###,##0";
            rowTracker++;
        }

So, using interop is not easy and does not have the best error checking.因此,使用互操作并不容易,并且没有最好的错误检查。 The error I had was unrelated to my actual problem.我的错误与我的实际问题无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM