Extract Text from Textbox in Excel Using Java

Andrew Wilson
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

  1. Create a Workbook instance.
  2. Load a sample Excel file using Workbook.loadFromFile() method.
  3. Get a specified worksheet using Workbook.getWorksheets().get() method.
  4. Get a specified textbox using Worksheet.getTextBoxes().get() method.
  5. 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);
}
}

--

--

Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.