1. 程式人生 > 其它 >Java 獲取Excel中的表單控制元件

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—