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项目。

java删除表中全部数据 java删除excel列_System

java删除表中全部数据 java删除excel列_excel_02

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

java删除表中全部数据 java删除excel列_经验分享_03

java删除表中全部数据 java删除excel列_经验分享_04

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

java删除表中全部数据 java删除excel列_excel_05

java删除表中全部数据 java删除excel列_maven_06

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);
    }
}

java删除表中全部数据 java删除excel列_经验分享_07

读取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());
    }
}

java删除表中全部数据 java删除excel列_excel_08

删除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文档:

java删除表中全部数据 java删除excel列_excel_09

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);
    }
}

结果:

java删除表中全部数据 java删除excel列_excel_10

官网地址: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

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

java删除表中全部数据 java删除excel列_java删除表中全部数据_11