If you’re a programmer, you might need to work with Excel spreadsheets. Sometimes you’ll need to create and fill Excel files, which I explained in a previous article. Today, I’ll show you another useful feature: inserting and deleting Excel rows and columns in Java. This can come in handy when you want to add or remove rows and columns as needed. Let’s see how you can do this in Java.
- Java Library to Insert or Delete Rows and Columns in Excel
- Insert Rows in an Excel Sheet in Java
- Insert Columns in an Excel Sheet in Java
- Delete Rows in an Excel Sheet in Java
- Delete Columns in an Excel Sheet in Java
Java Library to Insert or Delete Excel Rows and Columns
Aspose.Cells for Java is a popular spreadsheet manipulation library that provides an amazing set of features to generate and manipulate Excel files. We’ll use this library to manipulate rows and columns of the Excel sheets. You can either download its JAR or install it using the following Maven configuration.
<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>23.4</version>
</dependency>
Insert Rows in Excel using Java
The following are the steps to insert rows in an Excel worksheet in Java.
- First, use Workbook class to load the Excel file.
- Then, access the desired worksheet by index using Workbook.getWorksheets().get(index) method.
- Use Worksheet.getCells().insertRows(rowIndex, totalRows) method to insert the rows. The first parameter is the row index, whereas the second parameter is the number of rows you want to insert.
- Finally, save the Excel file using Workbook.save(String) method.
The following code sample shows how to insert rows in an Excel worksheet in Java.
Insert Columns in an Excel Sheet in Java
The procedure of adding the columns to an Excel sheet is similar to addition of the rows. Below are the steps to insert columns in a worksheet in Java.
- First, load the Excel file using the Workbook class.
- Get a reference of the desired worksheet by index using Workbook.getWorksheets().get(index) method.
- Insert columns using Worksheet.getCells().insertColumns(columnIndex, totalColumns) method. Here, the first parameter is the column’s index and the second parameter is the number of columns you want to insert.
- Finally, save the updated Excel file using Workbook.save(String) method.
The following code sample shows how to insert columns in an Excel sheet in Java.
Let’s now have a look at how to delete the rows and columns in Excel sheets using Java. First, we will discuss the deletion of rows.
Delete Rows in an Excel Sheet in Java
The following are the steps to delete rows from an Excel worksheet in Java.
- First, load the Excel file using the Workbook class.
- Then, get the reference of the worksheet by index using Workbook.getWorksheets().get(index) method.
- Delete rows using Worksheet.getCells().deleteRows(rowIndex, totalRows) method in which the first parameter is the row index and the second parameter is the number of rows you want to delete.
- At the end, save the Excel file using Workbook.save(String) method.
The following code sample shows how to delete rows from an Excel worksheet in Java.
Delete Columns in Excel Sheet in Java
The following are the steps to delete columns from an Excel worksheet in Java.
- First, load the Excel file using the Workbook class.
- Access the desired worksheet by index using Workbook.getWorksheets().get(index) method.
- Delete columns using Worksheet.getCells().deleteColumns(columnIndex, totalColumns, updateReference) method. The first parameter is the column index, the second parameter is the number of columns you want to delete and the third parameter indicates if references need to be updated in other worksheets.
- Finally, save the updated file using Workbook.save(String) method.
The following code sample shows how to delete columns from an Excel worksheet using Java.
Free Java Excel Library
You can get a free temporary license to manipulate Excel rows and columns without evaluation limitations.
Conclusion
In this article, you have learned how to manipulate rows and columns in Excel programmatically. Particularly, you have seen how to insert and delete rows and columns in Excel worksheets using Java.
You can explore more about the Java Excel library using the documentation. Furthermore, you can share your queries with us via our forum.