이 문서에서는 C#을 사용하여 Excel에서 ActiveX 컨트롤을 사용하는 방법을 안내합니다. 개발용 IDE를 설정하는 세부 사항, 프로세스를 정의하는 단계 목록, C#을 사용하여 Excel Active X 컨트롤을 추가하기 위한 샘플 코드가 포함되어 있습니다. ActiveX 컨트롤을 추가한 다음 이에 액세스하여 컨트롤 값을 업데이트하거나 읽는 방법을 배웁니다.
C#을 사용하여 Excel에 ActiveX 컨트롤을 추가하는 단계
- ActiveX 컨트롤 작업에 Aspose.Cells for .NET을 사용하도록 IDE를 설정합니다.
- ActiveX 컨트롤을 추가하기 위해 Workbook 클래스를 사용하여 Excel 파일을 만듭니다.
- 시트에서 도형 컬렉션에 액세스하고 AddActiveXControl() 메서드를 호출하여 컨트롤을 추가합니다.
- ActiveX 컨트롤에 액세스하고 연결된 셀 보내기
- 컨트롤의 특정 속성을 설정하려면 ActiveX 컨트롤을 타입캐스트하고 값을 설정하세요.
- 컨트롤을 업데이트하거나 액세스하려면 해당 컨트롤 유형을 확인하고 원하는 값을 업데이트하세요.
이 단계에서는 C#을 사용하여 Excel에서 ActiveX 컨트롤을 사용하는 방법을 요약합니다. 통합 문서의 시트에서 모양 컬렉션에 액세스하고 ShapeCollection 클래스의 AddActiveXControl() 메서드를 호출하여 프로세스를 시작합니다. ActiveX 컨트롤의 컨트롤 유형, 대상 셀 및 크기를 제공하고 해당 값을 조작하기 위해 컨트롤을 셀과 연결합니다.
C#을 사용하여 ActiveX 명령 단추를 추가하는 코드
using Aspose.Cells; | |
using Aspose.Cells.Drawing; | |
using Aspose.Cells.Drawing.ActiveXControls; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
new License().SetLicense("License.lic"); | |
// Create workbook object | |
Workbook wb = new Workbook(); | |
// Access first worksheet | |
Worksheet sheet = wb.Worksheets[0]; | |
// Add Command Button ActiveX Control inside the Shape Collection | |
Shape s = sheet.Shapes.AddActiveXControl(ControlType.CommandButton, 4, 0, 4, 0, 100, 30); | |
// Access the ActiveX control object and set its linked cell property | |
ActiveXControl c = s.ActiveXControl; | |
c.LinkedCell = "A1"; | |
// Add Toggle Button ActiveX Control inside the Shape Collection | |
Shape s1 = sheet.Shapes.AddActiveXControl(ControlType.ComboBox, 16, 0, 4, 0, 100, 30); | |
// Access the ActiveX control object and set its linked cell property | |
ActiveXControl c1 = s1.ActiveXControl; | |
c1.LinkedCell = "A4"; | |
ComboBoxActiveXControl comboControl = (ComboBoxActiveXControl)c1; | |
comboControl.Value = "A sample value for the ComboBox"; | |
// Save the workbook | |
wb.Save("Combo box with original value.xlsx"); | |
foreach (var shape in sheet.Shapes) | |
{ | |
// Access specific ActiveX Control and set its value | |
if (shape.ActiveXControl != null) | |
{ | |
// Access Shape ActiveX Control | |
ActiveXControl control = shape.ActiveXControl; | |
// Check for the target type | |
if (control.Type == ControlType.ComboBox) | |
{ | |
// Type cast ActiveXControl into ComboBoxActiveXControl and change its value | |
ComboBoxActiveXControl comboBoxActiveX = (ComboBoxActiveXControl)control; | |
comboBoxActiveX.Value = "A new value for the ComboBox"; | |
} | |
} | |
} | |
// Save the workbook in xlsx format | |
wb.Save("AddActiveXControls_out.xlsx", SaveFormat.Xlsx); | |
} | |
} |
이 코드는 Excel 시트에서 C#*을 사용하여 *Combo Box ActiveX 컨트롤을 추가하는 방법을 보여줍니다. AddActiveXControl() 메서드는 연결된 셀과 글꼴, 색상, 그림자, 텍스트 정렬 등의 기타 속성을 설정하는 데 사용할 수 있는 새로 추가된 도형에 대한 참조를 반환합니다. 마찬가지로, 모양을 특정 컨트롤로 타입캐스트하는 경우 컨트롤을 사용자 정의하기 위한 광범위한 속성을 얻게 됩니다.
이 문서에서는 Excel 파일에 ActiveX 컨트롤을 추가하고 액세스하는 방법을 설명했습니다. Excel 파일에 이미지를 추가하는 방법은 C#을 사용하여 Excel 셀에 이미지를 추가하는 방법의 문서를 참조하세요.