Šiame straipsnyje aprašomas ActiveX valdiklių naudojimas Excel su C#. Jame yra išsami informacija, skirta IDE nustatyti kūrimui, veiksmų, apibrėžiančių procesą, sąrašas ir pavyzdinis kodas, skirtas pridėti Excel Active X valdiklį naudojant C#. Išmoksite pridėti ActiveX valdiklį ir tada pasieksite jį, kad atnaujintumėte arba perskaitytumėte valdiklio reikšmę.
Veiksmai, kaip pridėti „ActiveX“ valdiklius programoje „Excel“ naudojant C#
- Nustatykite IDE, kad dirbdami su ActiveX valdikliais naudotų Aspose.Cells for .NET
- Sukurkite Excel failą naudodami Workbook klasę, kad pridėtumėte ActiveX valdiklį
- Pasiekite formų kolekciją iš lapo ir iškvieskite AddActiveXControl() metodą, kad pridėtumėte valdiklius
- Pasiekite ActiveX valdiklį ir išsiųskite susietą langelį
- Norėdami nustatyti konkrečią valdiklio ypatybę, įveskite ActiveX valdiklį ir nustatykite reikšmę
- Norėdami atnaujinti arba pasiekti valdiklį, patikrinkite jo valdymo tipą ir atnaujinkite norimą reikšmę
Šie veiksmai apibendrina kaip naudoti ActiveX valdiklius programoje Excel naudojant C#. Pradėkite procesą pasiekdami formų rinkinį iš darbaknygės lapo ir iškviesdami AddActiveXControl() metodą ShapeCollection klasėje. Nurodykite valdiklio tipą, tikslinį langelį ir ActiveX valdiklio dydį ir susiekite valdiklį su langeliu, kad galėtumėte manipuliuoti jo verte.
Kodas, skirtas pridėti ActiveX komandos mygtuką naudojant C#
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); | |
} | |
} |
Šis kodas parodo * Combo Box ActiveX valdiklio pridėjimą naudojant C#* Excel lape. Metodas AddActiveXControl() grąžina nuorodą į naujai pridėtą formą, kurią galite naudoti norėdami nustatyti susietą langelį ir kitas ypatybes, pvz., šriftą, spalvą, šešėlį ir teksto lygiavimą. Panašiai, jei įvesite formą į konkretų valdiklį, gausite daugybę savybių, skirtų valdikliui tinkinti.
Šis straipsnis išmokė mus įtraukti ir pasiekti ActiveX valdiklius Excel faile. Norėdami pridėti vaizdą į Excel failą, žr. straipsnį kaip pridėti vaizdą į Excel langelį naudojant C#.