Java add, read, delete Excel text box

This article by adding Java program text box to the Excel method, when adding a text box, may be added to the text, the text direction, text alignment, the text box size, position, fill color / fill images, text boxes rotation angle textbox name, optional text, such as a text box to hide or display operation. Existing text box, enabling the reading of the text in the text box, fill color, filled with pictures, text boxes, name and delete unwanted text box and so on. The following will demonstrate the specific method by way of example.
use tools

  • Free Spire.XLS for Java (free version)

Jar and Get introduced: by downloading the official website , extract lib folder and introducing the jar java program, import following effects:

Java add, read, delete Excel text box

Java code examples

1. Add a 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) {
        //创建实例
        Workbook wb = new Workbook();

        //获取工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //添加文本框1
        ITextBox textBox1 = sheet.getTextBoxes().addTextBox(3,3,150,300);//指定文本框位置、大小
        textBox1.setText("添加文本到文本框");//添加文本到文本框
        ((ITextBoxLinkShape) textBox1).getFill().setFillType(ShapeFillType.SolidColor);//设置文本框填充类型
        ((ITextBoxLinkShape) textBox1).getFill().setForeColor(new Color(255,218,155));//设置填充色
        textBox1.setHAlignment(CommentHAlignType.Center);//设置文本对齐方式
        textBox1.setVAlignment(CommentVAlignType.Center);
        textBox1.setTextRotation(TextRotationType.TopToBottom);//设置文本方向
        ((ITextBoxLinkShape) textBox1).setVisible(true);//设置文本框可见
        ((ITextBoxLinkShape) textBox1).setName("文本框1");//设置文本框名称

        //添加文本框2
        ITextBox textBox2 = sheet.getTextBoxes().addTextBox(7,10,120,300);//指定文本框位置、大小
        textBox2.setText("添加图片填充文本框2");//添加文本内容到文本框

        ((ITextBoxLinkShape) textBox2).getFill().customPicture("tp.png");//添加图片填充文本框
        ((ITextBoxLinkShape) textBox2).setRotation(30);//设置文本框旋转30度
        ((ITextBoxLinkShape) textBox2).setName("文本框2");//设置文本框名称
        ((ITextBoxLinkShape) textBox2).setAlternativeText("可选文本");//设置可选文本

        //保存文档
        wb.saveToFile("AddTextBox.xlsx",ExcelVersion.Version2013);
        wb.dispose();
    }
}

The effect of adding a text box:
Java add, read, delete Excel text box

2. Read the 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 {
        //创建实例,并加载测试文档
        Workbook wb = new Workbook();
        wb.loadFromFile("AddTextBox.xlsx");

        //获取工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //获取第一个文本框,读取文本及填充色
        XlsTextBoxShape textBoxShape1 = (XlsTextBoxShape) sheet.getTextBoxes().get(0);
        String  text = textBoxShape1.getText();
        Color color = textBoxShape1.getFillColor();
        String  name = textBoxShape1.getName();
        System.out.println("文本内容:"+ text + " 填充色:" + color + " 名称:"+ name);

        //获取第一个文本框,读取填充图片
        XlsTextBoxShape textBoxShape2 = (XlsTextBoxShape) sheet.getTextBoxes().get(1);
        BufferedImage image = textBoxShape2.getFill().getPicture();
        ImageIO.write(image,"png", new File("ExtractedImg.png"));
    }
}

Text box read results:
Java add, read, delete Excel text box

3. Delete the text box

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

public class RemoveTextBox {
    public static void main(String[] args) {
        //加载测试文档
        Workbook wb = new Workbook();
        wb.loadFromFile("AddTextBox.xlsx");

        //获取工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //获取文本框,删除
        XlsTextBoxShape textBoxShape = (XlsTextBoxShape) sheet.getTextBoxes().get(0);
        textBoxShape.remove();

        //保存文档
        wb.saveToFile("RemoveTextBox.xlsx",FileFormat.Version2013);
        wb.dispose();
    }
}

The effect of deleting the text box:
Java add, read, delete Excel text box

(This article End)

Guess you like

Origin blog.51cto.com/eiceblue/2475455