Java insert textbox to Excel worksheet

Monday, 02 March 2020 10:04 Written by  jie zou
Rate this item
(0 votes)

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

Additional Info

  • tutorial_title:
Last modified on Wednesday, 01 September 2021 02:29