2017-11-20 9 views
0

내 의도는 작업 시트 개체 속성에 문자열/숫자 값을 저장하는 것입니다.이 문자열은 사용자가 읽고 쓸 수 없어야합니다.엑셀 작업 시트 개체 속성에 문자열 또는 숫자 값 저장

아래처럼 시도했지만 예외가 발생했습니다.

Excel.Worksheet ws = Globals.ThisWorkbook.Application.ActiveWorkbook.ActiveSheet; 
ws._CodeName = "abc"; 

워크 시트 객체

+1

정확히 어떤 예외가 있습니까? 워크 시트에서 열을 잠그고 숨기는 것을 고려 했습니까? https://www.extendoffice.com/documents/excel/1280-excel-protect-hidden-columns.html – PaulF

+0

시트에 있어야하는 이유가 있습니까? ? – QHarr

+0

@QHarr : 각 시트에 대해 사용자에게 표시되지 않는 요청 ID를 저장해야합니다. –

답변

1

:

using System; 
using Excel = Microsoft.Office.Interop.Excel; 

class Startup 
{ 
    static void Main() 
    { 
     Excel.Application excelApp = new Excel.Application(); 
     Excel.Workbook wkbReport = Open(excelApp, @"C:\Testing.xlsx"); 
     Excel.Worksheet ws = wkbReport.Worksheets[1]; 
     Console.WriteLine(ws._CodeName); 
     ws.Name = "ABC"; 
     Console.WriteLine("END"); 
     excelApp.Visible = true;   
    } 

    public static Excel.Workbook Open(Excel.Application excelInstance, string fileName, bool readOnly = false, bool editable = true, bool updateLinks = true) 
    { 
     Excel.Workbook book = excelInstance.Workbooks.Open(
      fileName, updateLinks, readOnly, 
      Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
      Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing, 
      Type.Missing, Type.Missing); 
     return book; 
    } 
} 

이 그것과 같을 것이다 방법입니다 : 워크 시트의 이름을 변경하려는 경우

그러나, 이것은 가능한 시나리오는 c#이면 VBA 태그도 표시되므로 워크 시트에 customproperty를 추가하는 VBA 루틴이 있습니다.

Option Explicit 
Sub foo() 
    Dim WS As Worksheet 
    Dim sCPName As String 
    Dim sCPValue As String 
    Dim I As Long 

Set WS = Worksheets("sheet1") 
    sCPName = "Request ID" 
    sCPValue = 12345 

With WS.CustomProperties 
    For I = 1 To .Count 
     If .Item(I).Name = sCPName Then Exit For 
    Next I 

    If I > .Count Then 
     .Add sCPName, sCPValue 
    Else 
     .Item(I).Value = sCPValue 
    End If 

End With 

End Sub 
+0

너무 감사합니다 @Ron Rosenfeld –

0

ws._CodeName에 사용자 정의 값을 저장할 수있는 가능성이는 ReadOnly 속성 (MSDN Reference)입니다. 따라서, 그것을 읽을 수만 있고 그것을 덮어 쓸 수는 없습니다. 코드 비록

enter image description here