Java insert textbox to Excel worksheet

This article will demonstrate how to add textbox into Excel worksheet with Spire.XLS for Java. We could fill in the textbox with text and image.

import com.spire.xls.*;
import com.spire.xls.core.ITextBox;
import com.spire.xls.core.ITextBoxShape;

public class ExcelTextbox {
    public static void main(String[] args) throws Exception {

         //Create a workbook
        Workbook workbook = new Workbook();

        //Get the first sheet
        Worksheet sheet = workbook.getWorksheets().get(0);        
        
        //Insert the textbox with text
        ITextBox textBox = sheet.getTextBoxes().addTextBox(5, 3, 128, 196);
        textBox.setText("Insert TextBox in Excel");
        textBox.setHAlignment(CommentHAlignType.Center);
        textBox.setVAlignment(CommentVAlignType.Center);

        //Insert the textbox with picture
        ITextBoxShape shape = sheet.getTextBoxes().addTextBox(5, 8, 128, 196);
        shape.getFill().customPicture("logo.png");
        shape.getFill().setFillType(ShapeFillType.Picture);

        //Save the Excel file
        workbook.saveToFile("output/TextBox.xlsx", ExcelVersion.Version2010);
    }
}

Effective screenshot after adding textbox with text and picture in Excel worksheet:

Java insert textbox to Excel worksheet