Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Insert and Delete Rows and Columns in Excel using Java
// Load Excel file
Workbook workbook = new Workbook("Book1.xls");
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Delete a column from the worksheet at 2nd position
worksheet.getCells().deleteColumns(1, 1, true);
// Save the modified Excel file in default format
workbook.save("Delete Columns.xls");
// Load Excel file
Workbook workbook = new Workbook("Book1.xls");
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Delete 10 rows from the worksheet starting from 3rd row
worksheet.getCells().deleteRows(2, 10, true);
// Save the modified Excel file in default format
workbook.save("Delete Rows.xls");
// Load Excel file
Workbook workbook = new Workbook("Book1.xls");
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Insert a column into the worksheet at 2nd position
worksheet.getCells().insertColumns(1, 1);
// Save the modified Excel file in default format
workbook.save("Insert Column.xls");
// Load Excel file
Workbook workbook = new Workbook("Book1.xls");
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Insert a row into the worksheet at 3rd position
worksheet.getCells().insertRows(2, 1);
// Save the modified Excel file in default format
workbook.save("Insert Rows.xls");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment