
Sebagai pengembang C#, Anda dapat dengan mudah menyisipkan atau menghapus baris dan kolom di lembar kerja Excel secara terprogram. Pada artikel ini, Anda akan mempelajari cara menyisipkan atau menghapus baris dan kolom dalam lembar Excel menggunakan C#. Topik-topik berikut dibahas/dibahas dalam artikel ini:
- [C# API untuk Menyisipkan atau Menghapus Baris dan Kolom][2]
- [Sisipkan Baris di Lembar Kerja Excel menggunakan C#][3]
- [Sisipkan Baris dengan Pemformatan di Lembar Kerja Excel menggunakan C#][4]
- [Hapus Baris dari Lembar Kerja Excel menggunakan C#][5]
- [Sisipkan Kolom di Lembar Kerja Excel menggunakan C#][6]
- [Hapus Kolom dari Lembar Kerja Excel menggunakan C#][7]
C# API untuk Menyisipkan atau Menghapus Baris dan Kolom
Untuk menyisipkan atau menghapus baris dan kolom dalam lembar [Excel][8], saya akan menggunakan [Aspose.Cells for .NET API][9]. Ini adalah API manipulasi spreadsheet terkenal yang memungkinkan Anda membuat dan memproses file Excel dari dalam aplikasi .NET Anda. API memungkinkan Anda untuk memasukkan satu atau beberapa baris dan kolom dalam file Excel. Ini juga memungkinkan Anda untuk menghapus baris dan kolom secara terprogram. Anda dapat [mengunduh][10] DLL API atau menginstalnya menggunakan [NuGet][11].
Install-Package Aspose.Cells
Sisipkan Baris di Lembar Kerja Excel menggunakan C#
Anda dapat menyisipkan baris di lembar Excel secara terprogram dengan mengikuti langkah-langkah yang disebutkan di bawah ini:
- Buat instance kelas [Buku Kerja][12] dengan jalur file input.
- Buat instance dari kelas [Lembar Kerja][13].
- Akses lembar kerja dari koleksi [Lembar Kerja][14] berdasarkan indeksnya.
- Sisipkan baris dengan memanggil metode [InsertRows()][15] dan teruskan indeks baris untuk memulai dan total baris untuk disisipkan.
- Panggil metode [Save()][16] dengan jalur file keluaran.
Contoh kode berikut menunjukkan cara menyisipkan banyak baris dalam lembar Excel menggunakan C#.
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
// Instantiating a Workbook object // Opening the Excel file through the file stream Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx"); // Accessing the first worksheet in the Excel file Worksheet worksheet = workbook.Worksheets[0]; // Inserting 10 rows into the worksheet starting from 3rd row worksheet.Cells.InsertRows(2, 10); // Saving the modified Excel file workbook.Save(@"C:\Files\output.xlsx"); Sisipkan Banyak Baris di Lembar Kerja Excel menggunakan C#.
Demikian pula, Anda dapat menyisipkan satu baris dalam lembar Excel menggunakan contoh kode berikut.
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Inserting a row into the worksheet at 3rd position | |
worksheet.Cells.InsertRow(2); | |
// Saving the modified Excel file | |
workbook.Save(@"C:\Files\output.xlsx"); |

Sisipkan satu Baris di Lembar Kerja Excel menggunakan C#
Kelas [Buku Kerja][12] API mewakili buku kerja Excel. Anda bisa mendapatkan koleksi semua lembar kerja yang tersedia di dalam buku kerja menggunakan properti [Lembar Kerja][14] dari kelas ini. Lembar kerja apa pun dari buku kerja Excel dapat diakses dari koleksi Lembar Kerja dengan menggunakan indeksnya. Kelas [Lembar Kerja][13] mewakili satu lembar kerja. Itu memperlihatkan beberapa properti dan metode untuk melakukan berbagai operasi pada lembar kerja. Properti [Sel][19] kelas ini mewakili kumpulan sel yang tersedia di lembar kerja. Kelas [Sel][20] mewakili sel individual di dalam lembar kerja. Metode [InsertRow()][21] dari kelas [Cells][20] memungkinkan penyisipan satu baris pada indeks yang ditentukan. Kelas Cells juga menyediakan metode [InsertRows()][15] untuk menyisipkan lebih dari satu baris sekaligus. Dibutuhkan indeks baris dari mana mulai memasukkan baris dan jumlah total baris baru untuk disisipkan sebagai parameter input. Metode [Save()][16] dari kelas Buku Kerja menyimpan buku kerja di jalur file tertentu yang ditentukan sebagai parameter input.
Sisipkan Baris dengan Pemformatan di Lembar Kerja Excel menggunakan C#
Anda dapat menyisipkan baris dengan pemformatan di lembar Excel secara terprogram dengan mengikuti langkah-langkah yang disebutkan di bawah ini:
- Buat instance kelas [Buku Kerja][12] dengan jalur file input.
- Buat instance dari kelas [Lembar Kerja][13].
- Akses lembar kerja dari koleksi [Lembar Kerja][14] berdasarkan indeksnya.
- Buat instance dari kelas [InsertOptions][22].
- Atur properti [CopyFormatType][23].
- Panggil metode [InsertRows()][24] dengan indeks baris, total baris untuk disisipkan dan meneruskan InsertOptions.
- Panggil metode [Save()][16] dengan jalur file keluaran.
Contoh kode berikut menunjukkan cara menyisipkan baris dengan pemformatan dalam lembar Excel menggunakan C#.
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
// Instantiating a Workbook object // Opening the Excel file through the file stream Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx"); // Accessing the first worksheet in the Excel file Worksheet worksheet = workbook.Worksheets[0]; // Setting Formatting options InsertOptions insertOptions = new InsertOptions(); insertOptions.CopyFormatType = CopyFormatType.SameAsAbove; // Inserting a row into the worksheet at 3rd position worksheet.Cells.InsertRows(2, 1, insertOptions); // Saving the modified Excel file workbook.Save(@"C:\Files\output.xlsx");
- SameAsAbove — memungkinkan penyalinan format yang sama dengan baris di atas.
- SameAsBelow — memungkinkan penyalinan format yang sama dengan baris di bawah ini.
- Clear — memungkinkan pembersihan pemformatan.
Hapus Baris dari Lembar Kerja Excel menggunakan C#
Anda dapat menghapus baris dari lembar Excel secara terprogram dengan mengikuti langkah-langkah yang disebutkan di bawah ini:
- Buat instance kelas [Buku Kerja][12] dengan jalur file input.
- Buat instance dari kelas [Lembar Kerja][13].
- Akses lembar kerja dari koleksi [Lembar Kerja][14] berdasarkan indeksnya.
- Hapus baris dengan memanggil metode [DeleteRows()][25] dan teruskan indeks baris dan total baris yang akan dihapus.
- Panggil metode [Save()][16] dengan jalur file keluaran.
Contoh kode berikut menunjukkan cara menghapus baris dari lembar Excel menggunakan C#.
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
// Instantiating a Workbook object // Opening the Excel file through the file stream Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx"); // Accessing the first worksheet in the Excel file Worksheet worksheet = workbook.Worksheets[0]; // Deleting 10 rows into the worksheet starting from 3rd row worksheet.Cells.DeleteRows(2, 10); // Saving the modified Excel file workbook.Save(@"C:\Files\output.xlsx");
Sisipkan Kolom di Lembar Kerja Excel menggunakan C#
Anda dapat menyisipkan kolom di lembar Excel secara terprogram dengan mengikuti langkah-langkah yang disebutkan di bawah ini:
- Buat instance kelas [Buku Kerja][12] dengan jalur file input.
- Buat instance dari kelas [Lembar Kerja][13].
- Akses lembar kerja dari koleksi [Lembar Kerja][14] berdasarkan indeksnya.
- Sisipkan kolom dengan memanggil metode [InsertColumn()][27] dan berikan indeks kolom tempat menyisipkan kolom baru.
- Panggil metode [Save()][16] dengan jalur file keluaran.
Contoh kode berikut menunjukkan cara menyisipkan kolom di lembar Excel menggunakan C#.
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
// Instantiating a Workbook object // Opening the Excel file through the file stream Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx"); // Accessing the first worksheet in the Excel file Worksheet worksheet = workbook.Worksheets[0]; // Inserting a column into the worksheet at 2nd position worksheet.Cells.InsertColumn(1); // Saving the modified Excel file workbook.Save(@"C:\Files\output.xlsx"); Sisipkan satu Kolom di Lembar Kerja Excel menggunakan C#.
Demikian pula, Anda dapat menyisipkan banyak kolom dalam lembar Excel menggunakan contoh kode yang diberikan di bawah ini:
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Inserting 5 column into the worksheet starting at 2nd position | |
worksheet.Cells.InsertColumns(2, 5); | |
// Saving the modified Excel file | |
workbook.Save(@"C:\Files\output.xlsx"); |

Sisipkan Banyak Kolom di Lembar Kerja Excel menggunakan C#.
Untuk menyisipkan kolom di lembar kerja Excel, kelas [Cells][20] menyediakan metode [InsertColumns()][30] untuk menyisipkan banyak kolom di lembar kerja. Dibutuhkan indeks kolom dari mana mulai memasukkan kolom dan jumlah total kolom baru untuk disisipkan sebagai parameter input. Kelas Cells juga menyediakan metode [InsertColumn()][27] untuk menyisipkan satu kolom pada indeks yang ditentukan.
Hapus Kolom dari Lembar Kerja Excel menggunakan C#
Anda dapat menghapus kolom dari lembar Excel secara terprogram dengan mengikuti langkah-langkah yang disebutkan di bawah ini:
- Buat instance kelas [Buku Kerja][12] dengan jalur file input.
- Buat instance dari kelas [Lembar Kerja][13].
- Akses lembar kerja dari koleksi [Lembar Kerja][14] berdasarkan indeksnya.
- Hapus kolom dengan memanggil metode [DeleteColumn()][31] dan teruskan indeks kolom untuk dihapus.
- Panggil metode [Save()][16] dengan jalur file keluaran.
Contoh kode berikut menunjukkan cara menghapus kolom dari lembar Excel menggunakan C#.
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
// Instantiating a Workbook object // Opening the Excel file through the file stream Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx"); // Accessing the first worksheet in the Excel file Worksheet worksheet = workbook.Worksheets[0]; // Deleting a 3rd column from the worksheet worksheet.Cells.DeleteColumn(2); // Saving the modified Excel file workbook.Save(@"C:\Files\output.xlsx"); 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// Instantiating a Workbook object // Opening the Excel file through the file stream Workbook workbook = new Workbook(@"C:\Files\Book1.xlsx"); // Accessing the first worksheet in the Excel file Worksheet worksheet = workbook.Worksheets[0]; // Deleting 5 columns from the worksheet starting from 3rd column worksheet.Cells.DeleteColumns(2, 5, false); // Saving the modified Excel file workbook.Save(@"C:\Files\output.xlsx");
Dapatkan Lisensi Gratis
Anda dapat mencoba API tanpa batasan evaluasi dengan meminta [lisensi sementara gratis][33].
Kesimpulan
Dalam artikel ini, Anda telah mempelajari cara menyisipkan baris atau kolom dalam file Excel menggunakan C#. Anda juga telah mempelajari cara menghapus baris dan kolom dari file Excel secara terprogram. Selain itu, Anda telah mempelajari cara menyisipkan beberapa baris atau kolom dalam lembar Excel. Selanjutnya, Anda telah mempelajari cara menghapus beberapa baris atau kolom dari file Excel menggunakan C#. Anda dapat mempelajari lebih lanjut tentang Aspose.Cells untuk .NET API menggunakan [dokumentasi][34]. Jika ada ambiguitas, jangan ragu untuk menghubungi kami di [forum][35].
Lihat juga
- [Sembunyikan dan Tampilkan Baris atau Kolom di Lembar Kerja Excel menggunakan C#][36]
- [Hapus Baris dan Kolom Kosong di Excel menggunakan C#][37] [2]: #CSharp-API-to-Insert-or-Delete-Rows-and-Columns [3]: #Insert-Rows-in-Excel-Worksheets-using-CSharp [4]: #Insert-Rows-with-Formatting-in-Excel-Worksheets-using-CSharp [5]: #Delete-Rows-from-Excel-Worksheets-using-CSharp [6]: #Insert-Columns-in-Excel-Worksheets-using-CSharp [7]: #Delete-Columns-from-Excel-Worksheets-using-CSharp [8]: https://docs.fileformat.com/spreadsheet/xlsx/ [9]: https://products.aspose.com/cells/net/ [10]: https://releases.aspose.com/cells/net/ [11]: https://www.nuget.org/packages/aspose.cells [12]: https://apireference.aspose.com/cells/net/aspose.cells/workbook [13]: https://apireference.aspose.com/cells/net/aspose.cells/worksheet [14]: https://apireference.aspose.com/cells/net/aspose.cells/workbook/properties/worksheets [15]: https://apireference.aspose.com/cells/net/aspose.cells/cells/methods/insertrows [16]: https://apireference.aspose.com/cells/net/aspose.cells.workbook/save/methods/2 [19]: https://apireference.aspose.com/cells/net/aspose.cells/worksheet/properties/cells [20]: https://apireference.aspose.com/cells/net/aspose.cells/cells [21]: https://apireference.aspose.com/cells/net/aspose.cells/cells/methods/insertrow [22]: https://apireference.aspose.com/cells/net/aspose.cells/insertoptions [23]: https://apireference.aspose.com/cells/net/aspose.cells/insertoptions/properties/copyformattype [24]: https://apireference.aspose.com/cells/net/aspose.cells.cells/insertrows/methods/1 [25]: https://apireference.aspose.com/cells/net/aspose.cells/cells/methods/deleterows [26]: https://apireference.aspose.com/cells/net/aspose.cells/cells/methods/deleterow [27]: https://apireference.aspose.com/cells/net/aspose.cells/cells/methods/insertcolumn [30]: https://apireference.aspose.com/cells/net/aspose.cells/cells/methods/insertcolumns [31]: https://apireference.aspose.com/cells/net/aspose.cells/cells/methods/deletecolumn [32]: https://apireference.aspose.com/cells/net/aspose.cells/cells/methods/deletecolumns [33]: https://purchase.aspose.com/temporary-license [34]: https://docs.aspose.com/cells/net/ [35]: https://forum.aspose.com/c/cells/9 [36]: https://blog.conholdate.com/id/total/hide-and-show-rows-or-columns-in-excel-using-csharp/ [37]: https://blog.conholdate.com/id/total/delete-blank-rows-and-columns-in-excel-using-csharp/