溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java如何實現(xiàn)獲取Excel中的表單控件

發(fā)布時間:2022-05-27 13:48:38 來源:億速云 閱讀:206 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Java如何實現(xiàn)獲取Excel中的表單控件”,在日常操作中,相信很多人在Java如何實現(xiàn)獲取Excel中的表單控件問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java如何實現(xiàn)獲取Excel中的表單控件”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

Excel中可通過【開發(fā)工具】菜單欄下插入表單控件,如文本框、單選按鈕、復(fù)選框、組合框等等,插入后的控件可執(zhí)行設(shè)置控件格式,如大小、是否鎖定、位置、可選文字、數(shù)據(jù)源區(qū)域、單元格鏈接等。當(dāng)Excel中已插入上述控件,需要讀取時,也可以使用本文中的方法來讀取。

引入jar包

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

方法1

將 Free Spire.XLS for Java 包 下載 到本地,解壓,找到lib文件夾下的Spire.Xls.jar文件。然后在IDEA中打開“Project Structure”界面,然后執(zhí)行如圖步驟來手動導(dǎo)入本地路徑下的jar文件:

Java如何實現(xiàn)獲取Excel中的表單控件

方法2:通過 Maven倉庫 下載導(dǎo)入,如下配置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) {
        //創(chuàng)建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如何實現(xiàn)獲取Excel中的表單控件

到此,關(guān)于“Java如何實現(xiàn)獲取Excel中的表單控件”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI