Hello,
I saw many demo code to create controls but none to read it.
I want to open a excel and get a collection of all available labels and/or textboxes.
Then I want to read their values and also update them.
My approach is that someone create a excel and place controls where some information will be set from a workflow automatically added to the document.
So the control are there as placeholder and I want to read if they are exists (unique name) and then write their values.
Is there any demo code for this?
Thanks in advance, Yves
—
ADD:
So far I found soem code to load a worksheet and collect the text boxes.
But with Aspose.Cells.Drawing.TextBoxCollection I only the FormFields textboxes which can not be renamed inside Excel (for some very stupid reason I do not know).
So how can I access the ActiveX label and textboxes the same way??
Hi Yves,
var sheet = book.Worksheets[0];
var shapes = sheet.Shapes;
foreach (Shape shape in shapes)
{
if (shape.ControlData != null)
{
Console.WriteLine(shape.Id + " " + shape.Name + " is ActiveX control");
}
else
{
Console.WriteLine(shape.Id + " " + shape.Name + " is Form control");
if (shape.MsoDrawingType == MsoDrawingType.CheckBox)
{
((CheckBox)shape).CheckedValue = CheckValueType.Checked;
}
else if (shape.MsoDrawingType == MsoDrawingType.Button)
{
((Button)shape).Text = “Button Text”;
}
else if (shape.MsoDrawingType == MsoDrawingType.ListBox)
{
((ListBox)shape).SelectionType = SelectionType.Multi;
((ListBox)shape).SelectedItem(0, true);
((ListBox)shape).SelectedItem(1, true);
}
else if (shape.MsoDrawingType == MsoDrawingType.RadioButton)
{
((RadioButton)shape).IsChecked = true;
}
}
}
Hello,
thank you for your answer.
FormFields are “ok” to use for me, but in fact your sample does not include the most important one: TextField.
My requirement is to replace placeholder textbox values like in a “header like” environment, like author, title, etc.
I thought it would be way easier to simple set some textboxes, select them by name and the update their values…
Hi again,
var sheet = book.Worksheets[0];
var shapes = sheet.Shapes;
var textBox = shapes[“TextBox 1”];
((TextBox)textBox).Text = “Some Text”;
I ran into a bug I guess:
var oldName = “Textfield 1”;
var newName = “Field4711”;
var workbookSheets = _workbook.Worksheets;
foreach (Worksheet sheet in workbookSheets)
{
var boxes = sheet.TextBoxes;
foreach (var box in boxes)
{
if (box.Name == oldName) box.Name = newName;
}
}
If I save the document and open it via Excel it loads, but try to save it in Excel popup a error message that the file is corrupted!
This seems to work:
var shapes = sheet.Shapes;
foreach (var shape in shapes)
{
if (shape.MsoDrawingType == MsoDrawingType.TextBox && shape.Name == oldName)
shape.Name = newName;
}
Yes, exactly. This is what I want to address. I will work on this.
For some reason I do not understand the TextField shape name is not available to set in Excel itself (only via VB) which makes it very hard to use for a normal user…
But it seems I can not either use ActiveX-Textfield controls as you mentioned they are only read-able.
Add, to give feedback.
Works:
var workbookSheets = _workbook.Worksheets;
foreach (Worksheet sheet in workbookSheets)
{
var shapes = sheet.Shapes;
foreach (var shape in shapes)
{
if (shape.MsoDrawingType == MsoDrawingType.TextBox && shape.Name == name)
shape.Text = value;
}
}
The only thing now I need to find out is a solution to proper set the names of the fields…
And you might check on the bug I reported.
Thanks for help.
rausch:I ran into a bug I guess:
var oldName = "Textfield 1";
var newName = "Field4711";
var workbookSheets = _workbook.Worksheets;
foreach (Worksheet sheet in workbookSheets)
{
var boxes = sheet.TextBoxes;
foreach (var box in boxes)
{
if (box.Name == oldName) box.Name = newName;
}
}
If I save the document and open it via Excel it loads, but try to save it in Excel popup a error message that the file is corrupted!
This seems to work:
var shapes = sheet.Shapes;
foreach (var shape in shapes)
{
if (shape.MsoDrawingType == MsoDrawingType.TextBox && shape.Name == oldName)
shape.Name = newName;
}
Attached you find an example.
There is a “Textfeld 4” field. Use box.Name and save the document. You will be able to load it and on save click you get an error message.
Hi Yves,
var oldName = “Textfeld 4”;
var newName = “Field4711”;
var workbookSheets = book.Worksheets;
foreach (Worksheet sheet in workbookSheets)
{
var boxes = sheet.TextBoxes;
foreach (TextBox box in boxes)
{
if (box.Name == oldName) box.Name = newName;
}
}
book.Save(“C:/temp/output.xlsx”);
Hello,
I retried the same code and it do not produce the error now. Using the same version. So I do not know why it is working now, sorry for any circumstances
It seems that something else caused the effect…
Hi Yves,