Extract Text from Textbox in Excel Using Java
1 min readMar 16, 2022
Textbox in Excel can add additional information to the existing table, and my previous post has once introduced how to programmatically add a textbox with text to a Excel document. In this article, you will learn how to extract text from textbox within a worksheet using the same free Java API (Free Spire.XLS for Java).
Installation (2 Method)
1# Download the free API and unzip it, then add the Spire.Xls.jar file to your project as dependency.
2# Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
Steps and Sample Code
- Create a Workbook instance.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Get a specified textbox using Worksheet.getTextBoxes().get() method.
- Get the text in the specified textbox using XlsTextBoxShape.getText() method.
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.core.spreadsheet.shapes.XlsTextBoxShape;
public class ExtractTextFromTextbox {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.loadFromFile("input.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Get the first textbox
XlsTextBoxShape shape = (XlsTextBoxShape)sheet.getTextBoxes().get(0);
//Get the text inside the textbox
String text = shape.getText();
System.out.print(text);
}
}