本文介绍通过 Java 程序添加文本框到 Excel 的方法, 添加文本框时, 可以添加文本, 设置文本方向, 文本对齐方式, 设置文本框大小, 位置, 填充色 / 填充图片, 文本框旋转角度, 文本框名称, 可选文本, 文本框隐藏或显示等操作. 对已有文本框, 可实现读取文本框中的文本, 填充色, 填充图片, 文本框名称以及删除不需要的文本框等. 下面将分别通过示例演示具体实现方法.
使用工具: Free Spire.XLS for Java (免费版)
Jar 获取及导入: 可通过官网下载包, 解压并将 lib 文件夹下的 jar 导入 java 程序, 如下导入效果:
Java 代码示例
1. 添加文本框
- 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();
- }
- }
文本框添加效果:
2. 读取文本框
- 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"));
- }
- }
文本框读取结果:
3. 删除文本框
- 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();
- }
- }
文本框删除效果:
来源: https://www.cnblogs.com/Yesi/p/12502529.html