2014-10-27 194 views
1

我想從c#運行excel間接函數。我用下面的代碼,但有例外異常來自HRESULT:0x800A03EC INDIRECT

「的類型 ‘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; 
} 
+0

確實A2包含對不啓動'='另一個單元格的引用? – pnuts 2014-10-27 11:42:48

+0

不,當我從硬代碼值中獲取數據時,它也能正常工作。當我從數據庫中獲得數據時,會發生問題 – 2014-10-27 11:53:56

+0

那麼A2包含什麼? – pnuts 2014-10-27 11:55:48

回答

0

你需要把周圍的公式中的單元格引用雙引號: vehicle_makes_dropdown.Formula = "=indirect(""A2"")";

-1
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

+0

請正確格式化和詳細說明您的代碼。添加信息,瞭解您的代碼是如何工作的以及如何解決問題以改進未來讀者的幫助。 – jotasi 2016-09-19 15:19:05

+0

看起來,當'Operator'是xlBetween時,'Formula2'是必需的。由於你的函數調用缺少右括號,我想你只是忘了粘貼其餘的。而且,正如jotasi所說,請詳細說明。 https://msdn.microsoft.com/en-us/library/office/ff840078.aspx – djv 2016-09-19 15:55:55

+0

請參閱http://user.qzone.qq.com/26149705/blog/1474296940 – 2016-09-20 11:15:33

相關問題