-
Java 添加、删除Excel表单控件
通过表单控件,用户可以快速地将数据填写到模板文档中,轻松引用单元格数据并与其进行交互。本文通过Java代码示例介绍如何在Excel表格中添加表单控件,包括文本框、单选按钮、复选框、组合框、微调按钮等;以及如何删除Excel中的指定表单控件。
程序运行环境:Java、IDEA、jdk1.8.0、无需安装Microsoft Excel
使用工具:Free Spire.XLS for Java (免费版)
jar获取及导入:官网下载jar包 https://www.e-iceblue.cn/Downloads/Free-Spire-XLS-JAVA.html,并解压,将lib文件夹下的jar文件导入java程序。或者可通过maven仓库下载导入 https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html到Maven项目。如下导入效果:
【Java 示例1】添加表单控件
import com.spire.xls.*; import com.spire.xls.core.*; public class AddFormControl { public static void main(String[] args) { //创建工作簿,获取第一个工作表 Workbook wb = new Workbook(); Worksheet sheet = wb.getWorksheets().get(0); sheet.getCellRange("A2").setText("姓名: "); //添加文本框 ITextBoxShape textbox = sheet.getTextBoxes().addTextBox(2, 2, 18, 65); textbox.setText("李宏"); textbox.setHAlignment(CommentHAlignType.Center); textbox.setVAlignment(CommentVAlignType.Center); sheet.getCellRange("A4").setText("性别: "); //添加单选按钮1 IRadioButton radiobutton1 = sheet.getRadioButtons().add(4, 2, 18, 65); radiobutton1.setText("男"); radiobutton1.setCheckState(CheckState.Checked); //添加单选按钮2 IRadioButton radiobutton2 = sheet.getRadioButtons().add(4, 4, 18, 65); radiobutton2.setText("女"); sheet.getCellRange("A6").setText("爱好:"); //添加复选框1 ICheckBox checkbox1 = sheet.getCheckBoxes().addCheckBox(6, 2, 18, 65); checkbox1.setCheckState(CheckState.Checked); checkbox1.setText("摄影"); //添加复选框2 ICheckBox checkbox2 = sheet.getCheckBoxes().addCheckBox(6, 4, 18, 65); checkbox2.setCheckState(CheckState.Checked); checkbox2.setText("围棋"); sheet.getCellRange("A8").setText("职业:"); sheet.getCellRange("A20").setText("学生"); sheet.getCellRange("A21").setText("教师"); sheet.getCellRange("A22").setText("医生"); //添加组合框 IComboBoxShape combobox = sheet.getComboBoxes().addComboBox(8, 2, 18, 65); combobox.setListFillRange(sheet.getCellRange("A20:A22")); combobox.setSelectedIndex(2); sheet.getCellRange("A10").setText("行政级别:"); //添加微调按钮 ISpinnerShape spinnerShape = sheet.getSpinnerShapes().addSpinner(10,2,18,30); spinnerShape.setCurrentValue(1); spinnerShape.setDisplay3DShading(true); spinnerShape.setLinkedCell(sheet.getCellRange("B10")); spinnerShape.setMin(1); spinnerShape.setMax(5); //保存文档 wb.saveToFile("AddControls.xlsx", ExcelVersion.Version2013); wb.dispose(); } }
表单控件添加效果:
【Java 示例2】删除表单控件
import com.spire.xls.*; public class RemoveFormControl { public static void main(String[] args) { //加载Excel工作簿 Workbook wb = new Workbook(); wb.loadFromFile("AddControls.xlsx"); //获取第一个工作表 Worksheet sheet = wb.getWorksheets().get(0); //删除工作表中的所有单选按钮 for(int j = 0; j < sheet.getRadioButtons().getCount(); j ++){ sheet.getRadioButtons().get(j).remove(); } //保存文档 wb.saveToFile("RemoveFormControl.xlsx",ExcelVersion.Version2013); wb.dispose(); } }
表单控件删除效果:
(本文完)
出 处:https://www.cnblogs.com/Yesi/p/12610905.html
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比