
Kami dapat dengan mudah menghapus baris dan kolom kosong dari lembar kerja Excel secara terprogram. Pada artikel ini, kita akan mempelajari cara menghapus baris dan kolom kosong di Excel menggunakan Java. Topik-topik berikut akan dibahas dalam artikel ini:
- Java API untuk Menghapus Baris dan Kolom Kosong di Excel
- Hapus Baris Kosong di Excel menggunakan Java
- Hapus Kolom Kosong di Excel menggunakan Java
- Perbarui Referensi Secara Otomatis saat Menghapus Baris dan Kolom Kosong
Java API untuk Menghapus Baris dan Kolom Kosong di Excel
Untuk menghapus baris dan kolom kosong dari file XLSX, kita akan menggunakan Aspose.Cells for Java API. Harap unduh JAR API atau cukup tambahkan konfigurasi pom.xml berikut dalam aplikasi Java berbasis Maven.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>21.11</version>
</dependency>
Hapus Baris Kosong di Excel menggunakan Java
Kami dapat menghapus baris kosong dari lembar kerja Excel secara terprogram dengan mengikuti langkah-langkah di bawah ini:
- Pertama, muat file Excel menggunakan kelas Workbook.
- Selanjutnya, panggil metode Workbook.getWorksheets() dan dapatkan lembar kerja di objek WorksheetCollection.
- Kemudian, akses lembar kerja yang memiliki baris kosong baik dengan indeksnya (berbasis nol) atau dengan nama.
- Setelah itu, panggil metode Cells.deleteBlankRows() untuk menghapus baris kosong dari lembar kerja yang diakses.
- Terakhir, panggil metode save() dengan jalur file keluaran untuk menyimpan file keluaran.
Kode contoh berikut menunjukkan cara menghapus baris kosong dari Excel menggunakan Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Load an existing excel file. Workbook workbook = new Workbook("C:\\Files\\Cells\\sample_rows_cols.xlsx"); // Get the worksheets collection in the spreadsheet. WorksheetCollection sheets = workbook.getWorksheets(); // Get first Worksheet from WorksheetCollection by index. Worksheet sheet = sheets.get(0); // Or by name. // Worksheet sheet = sheets.get("Sheet1"); // Delete the Blank Rows from the worksheet. sheet.getCells().deleteBlankRows(); // Save the updated excel file. workbook.save("C:\\Files\\Cells\\output.xlsx"); Hapus Baris Kosong di Excel menggunakan Java.
Demikian pula, kami dapat menghapus baris kosong dari semua lembar kerja dalam dokumen Excel. Kami hanya akan mengulangi WorksheetCollection dan memanggil metode deleteBlankRows() pada setiap lembar kerja seperti yang ditunjukkan dalam contoh kode berikut:
// Open an existing excel file. | |
Workbook workbook = new Workbook("C:\\Files\\Cells\\sample_rows_cols.xlsx"); | |
// Get worksheets collection | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Iterate over the worksheets. | |
for (int i=0; i<worksheets.getCount(); i++) | |
{ | |
// Access worksheets one by one | |
Worksheet sheet = worksheets.get(i); | |
// Delete the Blank Rows from the worksheet. | |
sheet.getCells().deleteBlankRows(); | |
} | |
// Save the updated excel file. | |
workbook.save("C:\\Files\\Cells\\output.xlsx"); |
Hapus Kolom Kosong di Excel menggunakan Java
Kami dapat menghapus kolom kosong dari lembar kerja Excel secara terprogram dengan mengikuti langkah-langkah di bawah ini:
- Pertama, muat file Excel menggunakan kelas Workbook.
- Selanjutnya, panggil metode Workbook.getWorksheets() dan dapatkan lembar kerja di objek WorksheetCollection.
- Kemudian, akses lembar kerja yang memiliki kolom kosong baik dengan indeksnya (berbasis nol) atau dengan nama.
- Setelah itu, panggil metode Cells.deleteBlankColumns() untuk menghapus kolom kosong dari lembar kerja yang diakses.
- Terakhir, panggil metode save() dengan jalur file keluaran untuk menyimpan file keluaran.
Kode contoh berikut menunjukkan cara menghapus kolom kosong dari Excel menggunakan Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Open an existing excel file. Workbook workbook = new Workbook("C:\\Files\\Cells\\sample_rows_cols.xlsx"); // Get the worksheets collection in the spreadsheet. WorksheetCollection sheets = workbook.getWorksheets(); // Get first Worksheet from WorksheetCollection by index. Worksheet sheet = sheets.get(2); // Delete the blank columns. sheet.getCells().deleteBlankColumns(options); // Save the updated excel file. workbook.save("C:\\Files\\Cells\\output_DeleteBlankColumns.xlsx"); Hapus Kolom Kosong di Excel menggunakan Java.
Perbarui Referensi Secara Otomatis saat Menghapus Baris dan Kolom Kosong
Menghapus baris atau kolom kosong dapat merusak referensi dalam rumus, bagan, dan tabel. Misalnya, sel A1 di Sheet2 memiliki rumus =Sheet1!C7 yang merujuk ke sel C7 dari lembar kerja pertama seperti yang ditunjukkan pada gambar berikut.

Sel A1 di Sheet2 mengacu pada nilai sel C7 di Sheet1.
Jika kami menghapus baris kosong di Sheet1, nilai C7 (650000) akan dipindahkan ke sel C6. Tetapi rumus (=Sheet1!C7) tidak akan diperbarui dan sel A1 akan menampilkan nilai C7 yang akan menjadi 550000 dalam kasus ini. Kita dapat menghindari masalah ini dengan menyetel DeleteOptions.setUpdateReference ke true. Ini akan memastikan bahwa referensi diperbarui saat menghapus baris kosong. Kami dapat memperbarui referensi secara otomatis saat menghapus baris kosong dari lembar kerja Excel secara terprogram dengan mengikuti langkah-langkah berikut:
- Pertama, muat file Excel menggunakan kelas Workbook.
- Selanjutnya, panggil metode Workbook.getWorksheets() dan dapatkan lembar kerja di objek WorksheetCollection.
- Kemudian, akses lembar kerja dengan indeks (berbasis nol) atau dengan nama yang memiliki baris kosong.
- Selanjutnya, buat instance dari kelas DeleteOptions.
- Lalu, panggil setUpdateReferences() ke true. Ini akan memperbarui referensi di lembar lain sambil menghapus baris kosong.
- Setelah itu, panggil metode Cells.deleteBlankRows() dengan objek DeleteOptions sebagai argumen untuk menghapus baris kosong dari lembar kerja yang diakses.
- Terakhir, panggil metode save() dengan jalur file keluaran untuk menyimpan file keluaran.
Kode contoh berikut menunjukkan cara memperbarui referensi sambil menghapus baris kosong di Excel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Open an existing excel file. Workbook workbook = new Workbook("C:\\Files\\Cells\\sample_rows_cols.xlsx"); // Get the worksheets collection in the spreadsheet. WorksheetCollection sheets = workbook.getWorksheets(); // Get first Worksheet from WorksheetCollection by index. Worksheet sheet = sheets.get(0); // This option will ensure the references (in formulas, charts) // are updated while deleting blank rows. DeleteOptions options = new DeleteOptions(); options.setUpdateReference(true); // Delete the Blank Rows from the worksheet. sheet.getCells().deleteBlankRows(options); // Save the updated excel file. workbook.save("C:\\Files\\Cells\\output_UpdateReferencesAutomatically.xlsx");
Dapatkan Lisensi Gratis
Silakan coba API tanpa batasan evaluasi dengan meminta lisensi sementara gratis.
Kesimpulan
Pada artikel ini, kita telah mempelajari cara menghapus baris dan kolom di Excel menggunakan Java. Kita juga telah melihat cara memperbarui referensi sambil menghapus baris dan kolom secara terprogram. Selain itu, Anda dapat mempelajari lebih lanjut tentang Aspose.Cells untuk Java API menggunakan dokumentasi. Jika ada ambiguitas, jangan ragu untuk menghubungi kami di forum.