An array of cells defined in a worksheet is called range.
Using DsExcel Java, you can define a range and access the rows and columns in order to perform essential tasks like cell formatting, insert, merge and delete operations etc.
In order to access a range using different methods, refer to the following example code.
Java |
Copy Code |
---|---|
// Create a new workbook Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); // Using index to access cell A1. worksheet.getRange(0, 0).getInterior().setColor(Color.GetLightGreen()); // Using index to access cell range A1:B2 worksheet.getRange(0, 0, 2, 2).setValue(5); // Using string to access range. worksheet.getRange("A2").getInterior().setColor(Color.GetLightYellow()); worksheet.getRange("C3:D4").getInterior().setColor(Color.GetTomato()); worksheet.getRange("A5:B7, C3, H5:N6").setValue(2); // Using index to access rows worksheet.getRows().get(2).getInterior().setColor(Color.GetLightSalmon()); // Using string to access rows worksheet.getRange("4:4").getInterior().setColor(Color.GetLightSkyBlue()); // Using index to access columns worksheet.getColumns().get(2).getInterior().setColor(Color.GetLightSalmon()); // Using string to access columns worksheet.getRange("D:D").getInterior().setColor(Color.GetLightSkyBlue()); // Using Cells to access range. worksheet.getCells().get(5).getInterior().setColor(Color.GetLightBlue()); worksheet.getCells().get(5, 5).getInterior().setColor(Color.GetLightYellow()); // Access all rows in worksheet String allRows = worksheet.getRows().toString(); // Access all columns in worksheet String allColumns = worksheet.getColumns().toString(); // Access the entire sheet range String entireSheet = worksheet.getCells().toString(); |