Spire.XLS for Java读写删Excel

java读取、删除、写入操作excel

前言

​ 最近在频繁对接excel等文档方面的工作,操作也不麻烦但是数据量有点大,就想着用java去写一个脚本。然后也研究了POI,EasyExcel,之前一直使用POI的方式在进行读写,代码冗长,不易维护。无意中发现了Spire.XLS for Java 这个java操作excel的组件。它可以很方便的操作EXCEL文件,所以最终还是选择了spire

该组件包可以通过maven仓库下载:

只需简单配置,您就可以轻松将 Java 产品的 JAR 包通过 Maven 仓库安装到的 Maven 项目中。我们所有 Java 产品均可通过 Maven 安装。下面以 Spire.PDF for Java 为例,详细说明如何在 Maven 程序中添加对 JAR 包的依赖。

首先,在pom.xml文件中配置Maven仓库路径。

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>

然后,在pom.xml文件中指定Spire.PDF for Java的Maven依赖。

<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.pdf</artifactId>
        <version>3.11.6</version>
    </dependency>
</dependencies>

配置完成后,在IDEA中,您只需点击”Import Changes”即可导入JAR包;在Eclipse中,您需要点击”Save”按钮, JAR包才会自动下载。至此,您已经成功在Maven项目中添加了Spire.PDF JAR包依赖。

: Free Spire.PDF for Java的artifactId为spire.pdf.free

<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>

IDEA中的详细安装流程

第一步:创建Maven项目。

通过 Maven 仓库安装 Spire.PDF for Java

通过 Maven 仓库安装 Spire.PDF for Java

第二步:设置任意GroupId和ArtifactId。

通过 Maven 仓库安装 Spire.PDF for Java

通过 Maven 仓库安装 Spire.PDF for Java

第三步:配置pom.xml文件,然后点击”Import Changes”。

通过 Maven 仓库安装 Spire.PDF for Java

通过 Maven 仓库安装 Spire.PDF for Java

Java 创建 Excel 文件

import com.spire.xls.*;

import java.awt.*;

public class CreateExcel {
    public static void main(String[] args){
        //创建Workbook实例 
        Workbook workbook = new Workbook();

        //获取第一张工作表(新建的Workbook默认包含3张工作表)
        Worksheet sheet = workbook.getWorksheets().get(0);
        //为第一张工作表设置名称
        sheet.setName("Data Sheet");

        //创建列头单元格样式
        CellStyle style1 = workbook.getStyles().addStyle("Header Style");
        style1.getFont().setSize(12f);
        style1.getFont().setColor(Color.BLACK);
        style1.getFont().isBold(true);
        style1.setHorizontalAlignment(HorizontalAlignType.Center);
        style1.setVerticalAlignment(VerticalAlignType.Center);

        //创建数据单元格样式
        CellStyle style2 = workbook.getStyles().addStyle("Data Style");
        style2.getFont().setSize(10f);
        style2.getFont().setColor(Color.BLACK);

        //为列头单元格添加数据并应用样式
        for (int column=1; column<5; column++)
        {
            CellRange header =sheet.getCellRange(1,column);
            header.setValue("Column " + column );
            header.setStyle(style1);
            header.setColumnWidth(15f);
        }

        //为数据单元格添加数据并应用样式
        for (int row=2; row<11; row++)
        {
            for (int column=1; column<5; column++)
            {
                CellRange cell = sheet.getCellRange(row, column);
                cell.setValue("Data " + row + ", " + column);
                cell.setStyle(style2);
            }
        }

        //保存结果文件
        workbook.saveToFile("CreateExcel.xlsx", FileFormat.Version2013);
    }
}

读取Excel文档属性

import com.spire.xls.*;

public class ReadProperties {
    public static void main(String[] args) {
        //加载Excel文档
        Workbook wb = new Workbook();
        wb.loadFromFile("AddProperties.xlsx");

        //读取Excel内置文档属性
        System.out.println("标题: " + wb.getDocumentProperties().getTitle());
        System.out.println("主题: " + wb.getDocumentProperties().getSubject());
        System.out.println("作者: " + wb.getDocumentProperties().getAuthor());
        System.out.println("单位: " + wb.getDocumentProperties().getCompany());
        System.out.println("主管: " + wb.getDocumentProperties().getManager());
        System.out.println("类别: " + wb.getDocumentProperties().getCategory());
        System.out.println("关键字: " + wb.getDocumentProperties().getKeywords());

        //获取Excel自定义文档属性
        DocumentProperty property = (DocumentProperty) wb.getCustomDocumentProperties().get(0);
        //读取第一个自定义文档属性的名称和值
        System.out.println("名称: " + property.getName());
        System.out.println("值: " + property.getValue());
    }
}

在这里插入图片描述

删除Excel文档属性

import com.spire.xls.*;

public class RemoveProperties {
    public static void main(String[] args) {
        //加载Excel文档
        Workbook wb = new Workbook();
        wb.loadFromFile("AddProperties.xlsx");

        //通过将对应文档属性的值设置为空来删除该内置属性
        wb.getDocumentProperties().setTitle("");
        wb.getDocumentProperties().setSubject("");
        wb.getDocumentProperties().setAuthor("");
        wb.getDocumentProperties().setCompany("");
        wb.getDocumentProperties().setManager("");
        wb.getDocumentProperties().setCategory("");
        wb.getDocumentProperties().setKeywords("");
        wb.getDocumentProperties().setComments("");

        //根据自定义文档属性的名称来移除该自定义文档属性
        wb.getCustomDocumentProperties().remove("编辑");
        wb.getCustomDocumentProperties().remove("联系电话");

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

Java 查找和替换 Excel 数据

原Excel文档:

在这里插入图片描述

import com.spire.xls.CellRange;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class ReplaceData {
    public static void main(String[] args){

        //创建Workbook实例
        Workbook workbook = new Workbook();
        //加载Excel文档
        workbook.loadFromFile("Test.xlsx");

        //获取第一个工作表
        Worksheet worksheet = workbook.getWorksheets().get(0);

        //查找工作表中的指定文字
        CellRange[] ranges = worksheet.findAllString("合计", true, true);

        for (CellRange range : ranges)
        {
            //替换为新文字
            range.setText("替换");
        }

        //保存结果文档
        workbook.saveToFile("ReplaceData.xlsx", ExcelVersion.Version2013);
    }
}

结果:

在这里插入图片描述

官网地址:https://www.e-iceblue.cn/Downloads/Free-Spire-XLS-JAVA.html

也有很详细的文档教程,有兴趣的小伙伴可以了解一下!!

workbook.saveToFile(“ReplaceData.xlsx”, ExcelVersion.Version2013);
}
}
官网地址:https://www.e-iceblue.cn/Downloads/Free-Spire-XLS-JAVA.html

也有很详细的文档教程,有兴趣的小伙伴可以了解一下!!
在这里插入图片描述

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spire.XLS for Java 库中,可以使用以下代码将图片插入到 Excel 文档中并让图片在单元格中居中显示: ```java //加载 Excel 文档 Workbook workbook = new Workbook(); workbook.loadFromFile("sample.xlsx"); //获取要插入图片的单元格 Worksheet sheet = workbook.getWorksheets().get(0); CellRange range = sheet.getCellRange("B2"); Cell cell = range.getCell(0, 0); //加载图片 String imagePath = "image.png"; InputStream inputStream = new FileInputStream(imagePath); byte[] imageBytes = IOUtils.toByteArray(inputStream); Picture picture = sheet.getPictures().add(cell.getRow(), cell.getColumn(), imageBytes); //设置图片位置和大小 picture.setTop(cell.getTop()); picture.setLeft(cell.getLeft()); picture.setWidth(cell.getColumnWidth()); picture.setHeight(cell.getRowHeight()); //设置图片在单元格中居中显示 picture.setSizeMode(PictureSizeMode.STRETCH); picture.getPictureFormat().setStretch(STRETCH_CENTER); //保存 Excel 文档 workbook.saveToFile("output.xlsx", ExcelVersion.Version2013); ``` 这段代码中,我们首先加载了一个 Excel 文档,然后获取了要插入图片的单元格,并加载了一张图片。接着,我们设置了图片的位置和大小,然后将图片在单元格中居中显示,并最后将 Excel 文档保存到了一个新的文件中。 需要注意的是,这里使用了 `PictureSizeMode.STRETCH` 和 `picture.getPictureFormat().setStretch(STRETCH_CENTER)` 两个方法来实现图片在单元格中居中显示的效果。其中,`PictureSizeMode.STRETCH` 表示将图片拉伸至单元格的大小,而 `picture.getPictureFormat().setStretch(STRETCH_CENTER)` 则表示在水平和垂直方向上都居中显示图片。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值