Java實現(xiàn)獲取Excel中的表單控件
Excel中可通過【開發(fā)工具】菜單欄下插入表單控件,如文本框、單選按鈕、復(fù)選框、組合框等等,插入后的控件可執(zhí)行設(shè)置控件格式,如大小、是否鎖定、位置、可選文字、數(shù)據(jù)源區(qū)域、單元格鏈接等。當Excel中已插入上述控件,需要讀取時,也可以使用本文中的方法來讀取。下面,將通過Java代碼示例展示如何來獲取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文件:
方法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); } } }
獲取效果如圖所示:
到此這篇關(guān)于Java實現(xiàn)獲取Excel中的表單控件的文章就介紹到這了,更多相關(guān)Java獲取Excel表單控件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java內(nèi)存模型與JVM運行時數(shù)據(jù)區(qū)的區(qū)別詳解
這篇文章主要介紹了Java內(nèi)存模型與JVM運行時數(shù)據(jù)區(qū)的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10seata-1.4.0安裝及在springcloud中使用詳解
這篇文章主要介紹了seata-1.4.0安裝及在springcloud中使用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12Mybatis Integer類型參數(shù)值為0時得到為空的解決方法
這篇文章主要介紹了Mybatis Integer類型參數(shù)值為0時得到為空的解決方法,有需要的朋友們可以學習下。2019-08-08關(guān)于SpringGateway調(diào)用服務(wù) 接受不到參數(shù)問題
這篇文章主要介紹了關(guān)于SpringGateway調(diào)用服務(wù)接受不到參數(shù)問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12