2017-09-11 1 views
1

내 콘솔 응용 프로그램에서 뭔가를 쓰고 Excel 워크 시트의 CustomProperties에서 다시 읽으려고합니다. Microsoft.Office.Interop.Excel v14 어셈블리에 대한 참조가 있습니다.콘솔 응용 프로그램에서 Worksheet.CustomProperties에 쓰거나 읽을 수 없습니다.

firstWorksheet.CustomProperties.Add 메서드를 호출하는 줄에서 HRESULT 0x800A03EC 예외가 발생합니다. 다음은

코드의 관련 비트입니다 : 내가 얻을

{"Exception from HRESULT: 0x800A03EC"} 
    Data: {System.Collections.ListDictionaryInternal} 
    ErrorCode: -2146827284 
    HResult: -2146827284 
    HelpLink: null 
    IPForWatsonBuckets: 0x7177fe49 
    InnerException: null 
    IsTransient: false 
    Message: "Exception from HRESULT: 0x800A03EC" 
    RemoteStackTrace: null 
    Source: "" 
    StackTrace: " at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)\r\n at Microsoft.Office.Interop.Excel.CustomProperties.Add(String Name, Object Value)\r\n at CustomDocumentProperties.Program.WriteToExcelCustomDocumentProperties(String excelFile, String outputFolder, String propertyName, Object propertyValue) in C:\\Sathyaish\\DotNet\\CustomDocumentProperties\\CustomDocumentProperties\\Program.cs:line 61" 
    TargetSite: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)} 
    WatsonBuckets: null 
    _HResult: -2146827284 
    _className: null 
    _data: {System.Collections.ListDictionaryInternal} 
    _dynamicMethods: null 
    _exceptionMethod: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)} 
    _exceptionMethodString: null 
    _helpURL: null 
    _innerException: null 
    _ipForWatsonBuckets: 0x7177fe49 
    _message: "Exception from HRESULT: 0x800A03EC" 
    _remoteStackIndex: 0 
    _remoteStackTraceString: null 
    _safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager} 
    _source: "" 
    _stackTrace: {sbyte[96]} 
    _stackTraceString: null 
    _watsonBuckets: null 
    _xcode: -532462766 
    _xptrs: 0x00000000 

을 내가 코드를 사용하여 정보를 읽으려고하면 다음과 같습니다 : 아래

static void WriteToExcelCustomDocumentProperties(
    string excelFile, 
    string outputFolder, 
    string propertyName, 
    object propertyValue) 
{ 
    excel::Application excel = null; 
    Workbook workbook = null; 
    Worksheet firstWorksheet = null; 

    try 
    { 
     excel = new excel::Application(); 

     workbook = excel.Workbooks.Open(excelFile); 

     firstWorksheet = workbook.Worksheets[1] as Worksheet; 

     firstWorksheet.CustomProperties.Add(propertyName, propertyValue); 

     var outputFilePath = GetOutputFilePath(excelFile, outputFolder); 

     workbook.SaveAs(outputFilePath); 
    } 
    catch(Exception ex) 
    { 
     Console.WriteLine("\nERROR:"); 
     Console.WriteLine($"{excelFile}!{firstWorksheet.Name}"); 
     Console.WriteLine($"{ex.Message}\n"); 
    } 
    finally 
    { 
     if (workbook != null) 
      workbook.Close(); 

     if (excel != null) 
      excel.Quit(); 
    } 
} 

을 그리고 나는 오류받을 수있다 코드 목록 뒤에 나오는 예외.

static object ReadFromExcelCustomDocumentProperties(
    string excelFile, 
    string propertyName) 
{ 
    excel::Application excel = null; 
    Workbook workbook = null; 
    Worksheet firstWorksheet = null; 
    object value = null; 

    try 
    { 
     excel = new excel::Application(); 

     workbook = excel.Workbooks.Open(excelFile); 

     firstWorksheet = workbook.Worksheets[1] as Worksheet; 

     value = firstWorksheet.CustomProperties[(object)propertyName].Value; 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine($"\nERROR in {nameof(ReadFromExcelCustomDocumentProperties)}:"); 
     Console.WriteLine($"{excelFile}!{firstWorksheet.Name}"); 
     Console.WriteLine($"{ex.Message}\n"); 
    } 
    finally 
    { 
     if (workbook != null) 
      workbook.Close(); 

     if (excel != null) 
      excel.Quit(); 
    } 

    return value; 
} 

는 나에게 다음과 같은 오류를 제공합니다 :

enter image description here

아래는 예외 클래스 개체의 덤프입니다.

{"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"} 
    Data: {System.Collections.ListDictionaryInternal} 
    ErrorCode: -2147352571 
    HResult: -2147352571 
    HelpLink: null 
    IPForWatsonBuckets: 0x7177fe49 
    InnerException: null 
    IsTransient: false 
    Message: "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))" 
    RemoteStackTrace: null 
    Source: "" 
    StackTrace: " at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)\r\n at Microsoft.Office.Interop.Excel.CustomProperties.get__Default(Object Index)\r\n at CustomDocumentProperties.Program.ReadFromExcelCustomDocumentProperties(String excelFile, String propertyName) in C:\\Sathyaish\\DotNet\\CustomDocumentProperties\\CustomDocumentProperties\\Program.cs:line 131" 
    TargetSite: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)} 
    WatsonBuckets: null 
    _HResult: -2147352571 
    _className: null 
    _data: {System.Collections.ListDictionaryInternal} 
    _dynamicMethods: null 
    _exceptionMethod: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)} 
    _exceptionMethodString: null 
    _helpURL: null 
    _innerException: null 
    _ipForWatsonBuckets: 0x7177fe49 
    _message: "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))" 
    _remoteStackIndex: 0 
    _remoteStackTraceString: null 
    _safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager} 
    _source: "" 
    _stackTrace: {sbyte[96]} 
    _stackTraceString: null 
    _watsonBuckets: null 
    _xcode: -532462766 
    _xptrs: 0x00000000 

은 또한 상기 관측 된 거동이 상호 운용성 어셈블리의 버그에 기인 할 수 this answer에서 나타난다.

그러나 this answer은 포스터가 코드를 성공적으로 실행할 수 있었음을 나타냅니다.

코드를 성공적으로 실행할 수 있습니까? 이 오류를 보았고 수정 사항을 알고 있습니까?

+0

'CustomProperty'가 주어진'Name' 속성으로 이미 존재하지 않으면'CustomProperties.Add'를 성공적으로 호출 할 수 있어야합니다. 'Name'에 의한 검색은 Excel VBA 환경 에서조차'WorkSheet.CustomProperties'에 대해 결코 작동하지 않았습니다. 이것은 CustomProperties의 워크 시트 구현이있는 결함입니다. SmartTag에서 CustomProperties 컬렉션을 가져온 경우 이름별로 컬렉션을 인덱싱 할 수 있습니다. 링크 된 대답은 열거 루프를 사용하여 이름별로 조회하는 방법을 보여줍니다. 그 기술을 시험해 보셨습니까? – TnTinMn

답변

0

그것은 당신이 사용자 정의 속성에 대해 무엇을 의미하는지 모르는 가능하지만, Excel에서 파일 -> 정보 섹션에서 등록 정보를 참조하는 경우 : 그처럼

enter image description here

이 올 것입니다 Sharepoint에서 Workbook 개체의 ContentTypeProperties 컬렉션을 사용하여 액세스합니다. 나는 완전히 배를 놓칠 수 있으므로 다시 한 Worksheet 개체에서 뭔가를 보여

// Excel.Workbook wb; 

string dmdRegion = wb.ContentTypeProperties["Demand Region"].Value.ToString(); 
wb.ContentTypeProperties["Demand Region"].Value = "EMEA"; 

귀하의 예 : 여기

내가 위의 이미지에 액세스하는 방법의 예입니다.

관련 문제