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

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

文本框删除效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Apache POI 库来操作 Excel 表格并添加水印,具体步骤如下: 1. 导入 Apache POI 库。 2. 创建一个 Excel 文件对象并读取添加水印的 Excel 表格。 ``` FileInputStream fis = new FileInputStream("test.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(fis); ``` 3. 获取当前工作表对象。 ``` XSSFSheet sheet = workbook.getSheetAt(0); ``` 4. 创建一个 Drawing 对象并将其添加到当前工作表中。 ``` XSSFDrawing drawing = sheet.createDrawingPatriarch(); ``` 5. 创建一个文本框并设置其位置、大小、内容和样式。 ``` XSSFSimpleShape shape = drawing.createSimpleShape(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 1, 1)); shape.setShapeType(ShapeTypes.TEXT_BOX); shape.setFillColor(255, 255, 255); shape.setLineStyle(0); shape.setLineStyleColor(255, 255, 255); shape.setLineWidth(0); shape.setNoFill(true); shape.setAnchor(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 10, 10)); shape.setText("水印内容"); XSSFRichTextString rts = shape.getRichText(); XSSFFont font = workbook.createFont(); font.setColor(IndexedColors.GREY_50_PERCENT.getIndex()); font.setItalic(true); font.setFontHeightInPoints((short) 20); rts.applyFont(font); ``` 6. 保存 Excel 文件。 ``` FileOutputStream fos = new FileOutputStream("test.xlsx"); workbook.write(fos); workbook.close(); fis.close(); fos.close(); ``` 完整代码如下: ``` import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFDrawing; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; public class AddWatermarkToExcel { public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream("test.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(fis); XSSFSheet sheet = workbook.getSheetAt(0); XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFSimpleShape shape = drawing.createSimpleShape(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 1, 1)); shape.setShapeType(ShapeTypes.TEXT_BOX); shape.setFillColor(255, 255, 255); shape.setLineStyle(0); shape.setLineStyleColor(255, 255, 255); shape.setLineWidth(0); shape.setNoFill(true); shape.setAnchor(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 10, 10)); shape.setText("水印内容"); XSSFRichTextString rts = shape.getRichText(); XSSFFont font = workbook.createFont(); font.setColor(IndexedColors.GREY_50_PERCENT.getIndex()); font.setItalic(true); font.setFontHeightInPoints((short) 20); rts.applyFont(font); FileOutputStream fos = new FileOutputStream("test.xlsx"); workbook.write(fos); workbook.close(); fis.close(); fos.close(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值