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倉庫路徑。

1
2
3
4
5
6
<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依賴。

1
2
3
4
5
6
7
<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

1
2
3
4
5
6
7
<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 檔案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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);
    }
}

[外鏈圖像轉存失敗,源站可能有防盜鏈機制,建議將圖像儲存下來直接上傳(img-6LnQHGca-1616054990271)(C:UsersHWAppDataRoamingTypora ypora-user-imagesimage-20210318143627128.png)]

讀取Excel檔案屬性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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());
    }
}

[外鏈圖像轉存失敗,源站可能有防盜鏈機制,建議將圖像儲存下來直接上傳(img-DCNqr0cA-1616054990276)(C:UsersHWAppDataRoamingTypora ypora-user-imagesimage-20210318143319357.png)]

刪除Excel檔案屬性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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檔案:

[外鏈圖像轉存失敗,源站可能有防盜鏈機制,建議將圖像儲存下來直接上傳(img-6W1l1Van-1616054990277)(C:UsersHWAppDataRoamingTypora ypora-user-imagesimage-20210318143750428.png)]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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);
    }
}

結果:

[外鏈圖像轉存失敗,源站可能有防盜鏈機制,建議將圖像儲存下來直接上傳(img-9kuYmPP1-1616054990277)(C:UsersHWAppDataRoamingTypora ypora-user-imagesimage-20210318143821215.png)]

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

也有很詳細的檔案教學,有興趣的小夥伴可以了解一下!!