【问题标题】:Exception from HRESULT: 0x800A03EC INDIRECTHRESULT 异常:0x800A03EC 间接
【发布时间】:2014-10-27 11:31:46
【问题描述】:

我正在尝试从 c# 运行 excel 间接函数。我使用了以下代码,但得到了异常

“类型的第一次机会异常 'System.Runtime.InteropServices.COMException' 发生在 System.Dynamic.dll"

附加信息:HRESULT 异常:0x800A03EC

Excel.Range vehicle_makes_dropdown = xlWorkSheet1.get_Range("B2", "B101");
vehicle_makes_dropdown.Formula = "=indirect(A2)";
vehicle_makes_dropdown.Validation.Add(Excel.XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertInformation, XlFormatConditionOperator.xlEqual, vehicle_makes_dropdown.Formula, misValue);

vehicle_makes_dropdown.Validation.IgnoreBlank = true;
vehicle_makes_dropdown.Validation.InCellDropdown = true;

更新的代码是

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wlb = app.Workbooks.Open(@"D:\Templates2.xlsx");
Microsoft.Office.Interop.Excel.Worksheet MySheet = (Microsoft.Office.Interop.Excel.Worksheet)wlb.Sheets[1];


Microsoft.Office.Interop.Excel.Range oRange;

//  Get Subject 



// string subjetcs = string.Empty;
//List<Topics> lstsubject = PickSubject(out subjetcs);
AdminQuestionCommonModel objAdminQuestioncommonModel = new AdminQuestionCommonModel();
// _Question.BindQuestionDropDowns(objAdminQuestioncommonModel);
objAdminQuestioncommonModel.ExcelTopics = _Question.GetTopics(subjectId);

List<SubjectBO> lstTopics = GlobalService.GetAllTopicsandSubtopics(5, subjectId, 0); // get all topics of Subjects
//if (lstsubject.Count > 0)
if (lstTopics.Count > 0)
{
    Microsoft.Office.Interop.Excel.Worksheet IDSheet = (Microsoft.Office.Interop.Excel.Worksheet)wlb.Sheets[2];


    int rows = 1;
    int subrows = 1;
    IDSheet.Unprotect("123#");


    //foreach (var item in lstsubject)
    foreach (var Topic in lstTopics)
    {

        var TopicName = Topic.Topic_SubTopic.Trim();
        TopicName = TopicName.Replace(".", "_").Replace("&", "_").Replace(" ", "_").Replace("__", "_");

        // Add the header the first time through  
        IDSheet.Cells[rows, 1] = Topic.SubjectTopicId.ToString();
        IDSheet.Cells[rows, 2] = TopicName;

        //   List<SubTopics> lst = PickSubTopic(item.TopicName, item.TopicID);
        List<SubjectBO> lstsubtopics = _Question.GetSubTopics(subjectId, Topic.SubjectTopicId);

        int startindex = subrows;
        foreach (var subtopics in lstsubtopics)
        {
            IDSheet.Cells[subrows, 4] = subtopics.Topic_SubTopic;
            IDSheet.Cells[subrows, 5] = subtopics.SubjectTopicId.ToString();
            IDSheet.Cells[subrows, 6] = TopicName;
            IDSheet.Cells[subrows, 7] = Topic.SubjectTopicId.ToString(); 
            subrows++;
        }
        if (lstTopics.Count > 0)
        {
            wlb.Names.Add(TopicName, IDSheet.get_Range("D" + startindex + ":D" + (subrows - 1)));
        }
        subrows++;
        rows++;

    }


    wlb.Names.Add("Topics", IDSheet.get_Range("B1:B" + (rows - 1)));

    Microsoft.Office.Interop.Excel.Range Range = MySheet.get_Range("B2", "B800");

    Range.Validation.Delete();

    Range.NumberFormat = "Text";
    //Range.Cells.Value = lstsubject[0].TopicName.ToString();
    Range.Cells.Value = "Text";

    Range.Validation.Add(Microsoft.Office.Interop.Excel.XlDVType.xlValidateList, Microsoft.Office.Interop.Excel.XlDVAlertStyle.xlValidAlertStop
    , Microsoft.Office.Interop.Excel.XlFormatConditionOperator.xlBetween
    , Formula1: "=Topics"
    );

    Range.Validation.InCellDropdown = true;
    Range.Validation.IgnoreBlank = true;



    oRange = MySheet.get_Range("c2","c3");

    oRange.Validation.Delete();

    oRange.NumberFormat = "Text";



    oRange.Validation.Add(Microsoft.Office.Interop.Excel.XlDVType.xlValidateList, Microsoft.Office.Interop.Excel.XlDVAlertStyle.xlValidAlertStop
    , Microsoft.Office.Interop.Excel.XlFormatConditionOperator.xlBetween
    , Formula1: "=INDIRECT(B2)"
     );

    oRange.Validation.InCellDropdown = true;
    oRange.Validation.IgnoreBlank = true;

    IDSheet.Protect("123#");
    //Range.Validation.InCellDropdown = true;
    //Range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(255, 217, 217, 0));



    app.Visible = true;
}

【问题讨论】:

  • 不,当我从硬代码值中获取数据时它也可以工作。当我从数据库中获取数据时会出现问题
  • 对不起...A2 包含来自另一张纸的项目列表..
  • 谷歌“Excel 错误 1004”。无数次命中,你不需要另一个。关闭自动重新计算应该有很长的路要走。

标签: c# excel


【解决方案1】:

您需要在公式中的单元格引用周围加上双引号: vehicle_makes_dropdown.Formula = "=indirect(""A2"")";

【讨论】:

    【解决方案2】:
    oRange.Validation.Add(Microsoft.Office.Interop.Excel.XlDVType.xlValidateList,
        Microsoft.Office.Interop.Excel.XlDVAlertStyle.xlValidAlertStop,
        Microsoft.Office.Interop.Excel.XlFormatConditionOperator.xlBetween,
        Formula1: "=INDIRECT(if(B2=\"\",A5000,B2))"
    

    // 另见http://user.qzone.qq.com/26149705/blog/1474296940

    【讨论】:

    • 请正确格式化并详细说明您的代码。添加信息您的代码如何工作以及它如何解决问题以提高未来读者的帮助。
    • 似乎当Operator 是xlBetween 时,Formula2 是必需的。由于您的函数调用缺少右括号,我想您只是忘记粘贴其余部分。而且,正如jotasi所说,请详细说明。 msdn.microsoft.com/en-us/library/office/ff840078.aspx
    • 另见 user.qzone.qq.com/26149705/blog/1474296940
    猜你喜欢
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多