【GcExcel v5.2 新特性介绍】Excel 中表单控件的全面支持
本帖最后由 Winny 于 2022-9-8 19:56 编辑Excel表单控件提供了单选框、复选框等元素,简化用户填报流程。GCExcel V5.2之后,对Excel中的表单控件做了支撑,支持Excel中表单控件的创建及导入导出,下面会详细介绍表单控件的实现方式。
1. 下拉框
// 下拉框
IDropDown dropBox1 = ws.getControls().addDropDown(21.55, 38.4, 276.4, 30.19);
dropBox1.getItems().add(new DropDownItem("下拉选项1"));
dropBox1.getItems().add(new DropDownItem("下拉选项2"));
dropBox1.getItems().add(new DropDownItem("下拉选项3"));
dropBox1.setSelectedIndex(0);
ws.getRange("B12:B15").setValue(new Object[][] { { "下拉项" }, { "香蕉" }, { "苹果" }, { "梨" } });
ws.getRange("D12:D13").setValue(new Object[][] { { "Value" }, { 1d } });
IDropDown dropBox2 = ws.getControls().addDropDown(20.95, 123.8, 279.9, 26.50);
// 下拉数据来源于区域数据
dropBox2.setItemsSourceRange(ws.getRange("B13:B15"));
dropBox2.setLinkedCell(ws.getRange("D13"));
dropBox2.setSelectedIndex(0);导出Excel之后,展示如下:
2.分组框
// 分组框
IGroupBox groupBox2 = ws.getControls().addGroupBox(23.55, 153.6, 136.39, 116.4);
groupBox2.setText("Group 2");
IRange e12 = ws.getRange("E12");
e12.setValue(1d);
IOptionButton optionButton1 = ws.getControls().addOptionButton(42.75, 170.4, 77.7, 15.79);
optionButton1.setLinkedCell(e12);
optionButton1.setIsChecked(true);
IOptionButton optionButton2 = ws.getControls().addOptionButton(42.75, 201, 77.7, 17.69);
optionButton2.setLinkedCell(e12);
IOptionButton optionButton3 = ws.getControls().addOptionButton(42.75, 235.2, 77.7, 17);
optionButton3.setDisplay3DShading(true);
optionButton3.setLinkedCell(e12);分组框中可以包含很多选项按钮,导出Excel之后显示如下:
3.列表框
// 列表框
IListBox listBox1 = ws.getControls().addListBox(23.6, 32.8, 170, 80.9);
listBox1.getItems().add(new ListBoxItem("Unbound Item 1"));
listBox1.getItems().add(new ListBoxItem("Unbound Item 2"));
listBox1.getItems().add(new ListBoxItem("Unbound Item 3"));
listBox1.setSelectedIndex(0);
ws.getRange("F11:F14").setValue(new Object[][] { { "Items" }, { "Item 1" }, { "Item 2" }, { "Item 3" } });
ws.getRange("G11:G12").setValue(new Object[][] { { "Value" }, { 1d } });
// 从特定区域填充列表项
IListBox listBox2 = ws.getControls().addListBox(23.7, 153.7, 170, 80.30);
listBox2.setItemsSourceRange(ws.getRange("F12:F14"));
listBox2.setLinkedCell(ws.getRange("G12"));
listBox2.setSelectedIndex(0);
// 定义选择模式
IListBox listBox3 = ws.getControls().addListBox(24.1, 273.1, 170, 78.69);
listBox3.getItems().add(new ListBoxItem("Multi-select Item 1"));
listBox3.getItems().add(new ListBoxItem("Multi-select Item 2"));
listBox3.getItems().add(new ListBoxItem("Multi-select Item 3"));
listBox3.setSelectionMode(SelectionMode.Extended);
listBox3.getSelectedItems().add(listBox3.getItems().get(0));
listBox3.getSelectedItems().add(listBox3.getItems().get(2));
导出Excel之后显示如下:
4. 复选框
// 复选框
ICheckBox checkBox1 = ws.getControls().addCheckBox(23.1, 39.6, 88.5, 25.80);
checkBox1.setDisplay3DShading(true);
checkBox1.setIsChecked(false);
ICheckBox checkBox2 = ws.getControls().addCheckBox(23.1, 72, 88.5, 21);
checkBox2.setIsChecked(true);
checkBox2.setText("Checked");
ICheckBox checkBox3 = ws.getControls().addCheckBox(23.1, 101.4, 88.5, 20.39);
checkBox3.setIsChecked(null);
checkBox3.setText("Mixed");
// Data binding
ws.getRange("B11").setValue("It also supports data binding.");
ICheckBox checkBox4 = ws.getControls().addCheckBox(22.5, 175.8, 104.1, 23.39);
checkBox4.setLinkedCell(ws.getRange("D13"));
checkBox4.setIsChecked(true);
导出Excel之后显示如下:
5. 数值调节框
// 数值调节钮
ws.getRange("B9").setValue(6d);
ws.getRange("8:8").setRowHeight(7.8d);
ws.getRange("9:9").setRowHeight(25.8d);
ws.getRange("A:A").setColumnWidthInPixel(36d);
IRange b9 = ws.getRange("B9");
b9.setHorizontalAlignment(HorizontalAlignment.Left);
b9.setVerticalAlignment(VerticalAlignment.Center);
b9.setAddIndent(false);
ISpinner spinner2 = ws.getControls().addSpinner(48.5, 113.5, 25.5, b9.getHeight() - 2);
spinner2.setMax(10);
spinner2.setMin(1);
spinner2.setDisplay3DShading(false);
spinner2.setLinkedCell(b9);
spinner2.setValue(6);
导出Excel显示如下:
6. 按钮
// 按钮
IButton button = ws.getControls().addButton(50, 30, 120, 40);
button.setText("按钮");
button.setPrintObject(true);
button.setHorizontalTextAlignment(HorizontalAlignment.Center);
button.setVerticalTextAlignment(VerticalAlignment.Center);
导出Excel显示如下:
7. 标签
// 标签
ws.getControls().addLabel(25.8, 9.6, 27, 13.2).setText("这是一个label");
导出Excel显示如下:
8. 滚动条
// 滚动条
IScrollBar scrollBar1 = ws.getControls().addScrollBar(28.3, 39, 257.4, 24.20);
scrollBar1.setLargeChange(2);
// 水滚动条
scrollBar1.setOrientation(FormControlOrientation.Horizontal);
scrollBar1.setMax(10);
scrollBar1.setMin(1);
scrollBar1.setValue(3);
IScrollBar scrollBar2 = ws.getControls().addScrollBar(304.7, 23.7, 26.10, 136.10);
scrollBar2.setLargeChange(2);
// 垂直滚动条
scrollBar2.setOrientation(FormControlOrientation.Vertical);
scrollBar2.setMax(10);
scrollBar2.setMin(1);
scrollBar2.setSmallChange(1);
scrollBar2.setValue(1);
// Data binding
ws.getRange("B7").setValue("It can be linked to a cell to bind the value.");
ws.getRange("B9:C9").setValue(new Object[][] { { "Value", 4d } });
IScrollBar scrollBar3 = ws.getControls().addScrollBar(26.5, 140, 255, 21);
scrollBar3.setLargeChange(2);
scrollBar3.setOrientation(FormControlOrientation.Horizontal);
scrollBar3.setMax(10);
scrollBar3.setMin(1);
scrollBar3.setSmallChange(1);
scrollBar3.setLinkedCell(ws.getRange("C9"));
scrollBar3.setValue(1);
导出Excel显示如下:
以上表单控件支持导出PDF,执行WorkBook.save("test.pdf")即可。
页:
[1]