Java实现获取Excel中的表单控件(java读取excel数据)

网友投稿 508 2022-07-28


目录引入jar包代码示例

Excel中可通过【开发工具】菜单栏下插入表单控件,如文本框、单选按钮、复选框、组合框等等,插入后的控件可执行设置控件格式,如大小、是否锁定、位置、可选文字、数据源区域、单元格链接等。当Excel中已插入上述控件,需要读取时,也可以使用本文中的方法来读取。下面,将通过java代码示例展示如何来获取Excel文档中的表单控件。以下是读取的方法及步骤,供参考。

引入jar包

按照如下方法来引用Spire.Xls.jar 版本:5.1.0

方法1

将 Free Spire.XLS for Java 包 下载 到本地,解压,找到lib文件夹下的Spire.Xls.jar文件。然后在IDEA中打开“Project Structure”界面,然后执行如图步骤来手动导入本地路径下的jar文件:

方法2:通过 Maven仓库 下载导入,如下配置pom.xml:

com.e-iceblue

e-iceblue

https://repo.e-iceblue.cn/repository/maven-public/

e-iceblue

spire.xls.free

5.1.0

代码示例

Java

import com.spire.xls.*;

import com.spire.xls.core.ICheckBox;

import com.spire.xls.core.IRadioButton;

import com.spire.xls.core.ISpinnerShape;

public class GetFormControl {

public static void main(String[] args) {

//创建Workbook类的实例,加载ExceYPbIRxqDl文档

Workbook wb = new Workbook();

wb.loadFromFile("AddControls.xlsx");

//获取第1张工作表

Worksheet sheet = wb.getWorksheets().get(0);

//获取TextBox

String textbox = sheet.getTextBoxes().get(0).getText();

System.out.println(textbox);

//获取Radio Button

for(int i = 0; i

{

IRadioButton radioButton = sheet.getRadioButtons().get(i);

String name = radioButton.getCheckState().name();

String text = radioButton.getText();

boolean islocked = radioButton.isLocked();

System.out.println(name + text + " 是否锁定:"+ islocked);

}

//获取Combo Box控件中的选中的值(注:非列表中所有选项值)

String value = sheet.getComboBoxes().get(0).getSelectedValue();

System.out.println(value);

//获取Checkbox

for(int z = 0;z< sheet.getCheckBoxes().getCount();z++)

{

ICheckBox checkBox = sheet.getCheckBoxes().get(z);

String text = checkBox.getText();

String name = checkBox.getCheckState().name();

String alternativetext = checkBox.getAlternativeText();

System.out.println(text + name + alternativetext);

}

//获取SpinnerShape

for(int j = 0;jhttp://

{

ISpinnerShape spinnerShape = sheet.getSpinnerShapes().get(j);

String rangeAddress = spinnerShape.getLinkedCell().getRangeAddress();

int currentValue = spinnerShape.getCurrentValue();

System.out.println(rangeAddress + "\n" + currentValue);

}

}

}

获取效果如图所示:

{

IRadioButton radioButton = sheet.getRadioButtons().get(i);

String name = radioButton.getCheckState().name();

String text = radioButton.getText();

boolean islocked = radioButton.isLocked();

System.out.println(name + text + " 是否锁定:"+ islocked);

}

//获取Combo Box控件中的选中的值(注:非列表中所有选项值)

String value = sheet.getComboBoxes().get(0).getSelectedValue();

System.out.println(value);

//获取Checkbox

for(int z = 0;z< sheet.getCheckBoxes().getCount();z++)

{

ICheckBox checkBox = sheet.getCheckBoxes().get(z);

String text = checkBox.getText();

String name = checkBox.getCheckState().name();

String alternativetext = checkBox.getAlternativeText();

System.out.println(text + name + alternativetext);

}

//获取SpinnerShape

for(int j = 0;jhttp://

{

ISpinnerShape spinnerShape = sheet.getSpinnerShapes().get(j);

String rangeAddress = spinnerShape.getLinkedCell().getRangeAddress();

int currentValue = spinnerShape.getCurrentValue();

System.out.println(rangeAddress + "\n" + currentValue);

}

}

}

获取效果如图所示:

{

ISpinnerShape spinnerShape = sheet.getSpinnerShapes().get(j);

String rangeAddress = spinnerShape.getLinkedCell().getRangeAddress();

int currentValue = spinnerShape.getCurrentValue();

System.out.println(rangeAddress + "\n" + currentValue);

}

}

}

获取效果如图所示:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java实现ATM机操作系统(java实现atm柜员机模拟程序)
下一篇:mybatis plus乐观锁及实现详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~