当前位置: 移动技术网 > IT编程>开发语言>Java > Java 添加、读取、删除Excel文本框

Java 添加、读取、删除Excel文本框

2020年03月16日  | 移动技术网IT编程  | 我要评论

本文介绍通过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();
    }
}

文本框删除效果:

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网