Sometimes we need to hide some rows and columns in Excel worksheets so that the data appears completely on one screen. At other times, we need to show all the hidden rows and columns to view the data completely. In this article, you will learn how to hide and show rows or columns in Excel in Java applications from the following four parts.
- Hide Excel Rows and Columns
- Show Hidden Rows and Columns in Excel
- Hide Multiple Rows and Columns
- Show All Hidden Rows and Columns
Install Spire.XLS for Java
First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls</artifactId> <version>14.11.0</version> </dependency> </dependencies>
Hide Rows and Columns
The detailed steps are listed as below.
- Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Hide a specific column using Worksheet.hideColumn(int columnIndex)method.
- Hide a specific row using Worksheet.hideRow(int rowIndex) method.
- Save the document to file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class HideRowsColumns { public static void main(String[] args) throws Exception { //Load the sample document Workbook wb = new Workbook(); wb.loadFromFile("Sample.xlsx "); //Get the first worksheet Worksheet sheet = wb.getWorksheets().get(0); //Hide the third column sheet.hideColumn(3); //Hide the third row sheet.hideRow(3); //Save the document wb.saveToFile("HideRowsColumns.xlsx", ExcelVersion.Version2016); } }
Show Hidden Rows and Columns
- Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Show a hidden column using Worksheet.showColumn(int columnIndex)method.
- Show a hidden row using Worksheet.showRow(int rowIndex) method.
- Save the document to file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class ShowRowsColumns { public static void main(String[] args) throws Exception { //Load the sample document Workbook wb = new Workbook(); wb.loadFromFile("HideRowsColumns.xlsx "); //Get the first worksheet Worksheet sheet = wb.getWorksheets().get(0); //Unhide the third column sheet.showColumn(3); //Unhide the third row sheet.showRow(3); //Save the document wb.saveToFile("ShowRowsColumns.xlsx", ExcelVersion.Version2016); } }
Hide Multiple Rows and Columns
- Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Hide multiple columns using Worksheet.hideColumns(int columnIndex, int columnCount)method.
- Hide multiple rows using worksheet.hideRows(int rowIndex, int rowCount) method.
- Save the document to file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class HideMultiRowsColumns { public static void main(String[] args) throws Exception { //Load the sample document Workbook wb = new Workbook(); wb.loadFromFile("Sample01.xlsx "); //Get the first worksheet Worksheet sheet = wb.getWorksheets().get(0); //Hide multiple columns sheet.hideColumns(2,2); //Hide multiple rows sheet.hideRows(3,3); //Save the document wb.saveToFile("HideMultiRowsColumns.xlsx", ExcelVersion.Version2016); } }
Show Multiple Rows and Columns
- Create a Workbook instance and load a sample Excel document using Workbook.loadFromFile() method.
- Get a specified worksheet using Workbook.getWorksheets().get() method.
- Loop through the rows and find the hidden rows using Worksheet.getRowIsHide() method.
- Show all hidden rows using Worksheet.showRow(i) method.
- Save the document to file using Workbook.saveToFile() method.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class ShowMultiRowsColumns { public static void main(String[] args) throws Exception { //Load the sample document Workbook wb = new Workbook(); wb.loadFromFile("HideMultiRowsColumns.xlsx"); //Get the first worksheet Worksheet sheet = wb.getWorksheets().get(0); //Traverse all the rows for (int i = 1; i <= sheet.getLastRow(); i++) { //detect if the row is hidden if (sheet.getRowIsHide(i)) { //Show the hidden rows sheet.showRow(i); } } //Traverse the columns and show all the hidden columns for (int j = 1; j <= sheet.getLastColumn(); j++) { if (sheet.getColumnIsHide(j)) { sheet.showColumn(j); } //Save the document wb.saveToFile("ShowMultiRowsColumns.xlsx", ExcelVersion.Version2016); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.