本文介绍通过Java程序添加文本框到Excel的方法,添加文本框时,可以添加文本、设置文本方向、文本对齐方式、设置文本框大小、位置、填充色/填充图片、文本框旋转角度、文本框名称、可选文本、文本框隐藏或显示等操作。对已有文本框,可实现读取文本框中的文本、填充色、填充图片、文本框名称以及删除不需要的文本框等。下面将分别通过示例演示具体实现方法。
使用工具: Free Spire.XLS for Java (免费版)
Jar获取及导入:可通过
Java代码示例
1.添加文本框
import com.spire.xls.*;importcom.spire.xls.core.ITextBox;importcom.spire.xls.core.ITextBoxLinkShape;import java.awt.*;public classAddTextBox {public static voidmain(String[] args) {//创建实例
Workbook wb = newWorkbook();//获取工作表
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.*;importcom.spire.xls.core.spreadsheet.shapes.XlsTextBoxShape;importjavax.imageio.ImageIO;import java.awt.*;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;public classReadTextBox {public static void main(String[] args) throwsIOException {//创建实例,并加载测试文档
Workbook wb = newWorkbook();
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.*;importcom.spire.xls.core.spreadsheet.shapes.XlsTextBoxShape;public classRemoveTextBox {public static voidmain(String[] args) {//加载测试文档
Workbook wb = newWorkbook();
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();
}
}
文本框删除效果:
文章来源: www.cnblogs.com,作者:E-iceblue,版权归原作者所有,如需转载,请联系作者。
原文链接:https://www.cnblogs.com/Yesi/p/12502529.html