Java 获取Excel中的表单控件

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: 

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

【代码示例】

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类的实例,加载Excel文档
        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<sheet.getRadioButtons().getCount();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;j<sheet.getSpinnerShapes().getCount();j++)
        {
            ISpinnerShape spinnerShape = sheet.getSpinnerShapes().get(j);
            String rangeAddress = spinnerShape.getLinkedCell().getRangeAddress();
            int currentValue = spinnerShape.getCurrentValue();
            System.out.println(rangeAddress + "\n" + currentValue);
        }

    }
}

★ 相关阅读推荐:Java 添加、删除Excel表单控件

—END—

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值