Java add, read and delete excel text box

Time:2020-7-28

This paper introduces the method of adding text box to excel through Java program. When adding text box, you can add text, set text direction, text alignment, set text box size, position, fill color / fill picture, text box rotation angle, text box name, optional text, text box hiding or displaying. For existing text box, it can read text, fill color, fill picture, text box name and delete unnecessary text box. The following will demonstrate the specific implementation method through examples.

Tools used:Free Spire.XLS For Java (free version)

Jar acquisition and import:Can be passed throughDownload from official websitePackage, unzip and import the jar under the Lib folder into the Java program. The import effect is as follows:

 

 

Java Code example

1. Add text box

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

import java.awt.*;

public class AddTextBox {
    public static void main(String[] args) {
        //Create instance
        Workbook wb = new Workbook();

        //Get worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Add text box 1
        ITextBox textBox1 =  sheet.getTextBoxes (). Addtextbox (3,31530300); // specifies the location and size of the text box
        Textbox1.settext ("add text to text box"); // add text to text box
        ((ITextBoxLinkShape) textBox1).getFill().setFillType( ShapeFillType.SolidColor ); // sets the text box fill type
        ((itextboxlinkshape) textbox1). Getfill(). Setforecolor (new color (255218155)); // sets the fill color
        textBox1.setHAlignment( CommentHAlignType.Center ); // set text alignment
        textBox1.setVAlignment(CommentVAlignType.Center);
        textBox1.setTextRotation( TextRotationType.TopToBottom ); // set text direction
        ((itextboxlinkshape) textbox1). Setvisible (true); // sets the text box visible
        ((itextboxlinkshape) textbox1). Setname ("textbox 1"); // sets the name of the text box


        //Add text box 2
        ITextBox textBox2 =  sheet.getTextBoxes (). Addtextbox (7,10120300); // specifies the location and size of the text box
        Textbox2. Settext ("add picture to fill text box 2"); // add text content to text box

        ((ITextBoxLinkShape) textBox2).getFill().customPicture(" tp.png "); // add a picture to fill the text box
        ((itextboxlinkshape) textbox2). SetRotation (30); // sets the text box to rotate 30 degrees
        ((itextboxlinkshape) textbox2). Setname ("textbox 2"); // sets the name of the text box
        ((itextboxlinkshape) textbox2). Setalternative text ("optional text"); // sets the optional text

        //Save the document
        wb.saveToFile("AddTextBox.xlsx",ExcelVersion.Version2013);
        wb.dispose();
    }
}

Text box add effect:

 

2. Read text box

import com.spire.xls.*;
import com.spire.xls.core.spreadsheet.shapes.XlsTextBoxShape;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ReadTextBox {
    public static void main(String[] args) throws IOException {
        //Create an instance and load the test document
        Workbook wb = new Workbook();
        wb.loadFromFile("AddTextBox.xlsx");

        //Get worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Get the first text box, read the text and fill color
        XlsTextBoxShape textBoxShape1 = (XlsTextBoxShape) sheet.getTextBoxes().get(0);
        String  text = textBoxShape1.getText();
        Color color = textBoxShape1.getFillColor();
        String  name = textBoxShape1.getName();
        System.out.println ("text content: + text +" fill color: + Color + "Name: + name)";

        //Get the first text box and read the fill image
        XlsTextBoxShape textBoxShape2 = (XlsTextBoxShape) sheet.getTextBoxes().get(1);
        BufferedImage image = textBoxShape2.getFill().getPicture();
        ImageIO.write(image,"png", new File("ExtractedImg.png"));
    }
}

Text box read results:

 

3. Delete text box

import com.spire.xls.*;
import com.spire.xls.core.spreadsheet.shapes.XlsTextBoxShape;

public class RemoveTextBox {
    public static void main(String[] args) {
        //Load test document
        Workbook wb = new Workbook();
        wb.loadFromFile("AddTextBox.xlsx");

        //Get worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Get text box, delete
        XlsTextBoxShape textBoxShape = (XlsTextBoxShape) sheet.getTextBoxes().get(0);
        textBoxShape.remove();

        //Save the document
        wb.saveToFile("RemoveTextBox.xlsx",FileFormat.Version2013);
        wb.dispose();
    }
}

Text box deletion effect: