why is not possible to 'properly' hide an Excel row using Apache POI (3.16)? It is just possible to call (XSSFRow) row.setZeroHeight(), which is also what the Busy developer's guide recommends. However, this is not the same as hiding the row the way Excel does it. You can 'Hide' and 'Unhide' rows with the respective context menu options.
I thought setting the row style should work, but it doesn't. In the resulting Excel file, the row can still be seen.
package de.mwe;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.Assert;
import org.testng.annotations.Test;
public class MWE {
@Test
public void testHidingRows() {
final XSSFWorkbook wb = new XSSFWorkbook();
String sname = "HideRowsTestSheet", cname = "TestName", cvalue = "TestVal";
XSSFSheet sheet = wb.createSheet( sname );
XSSFRow row = sheet.createRow( 0 );
XSSFCell cell = row.createCell( (short) 0 );
cell.setCellValue( cvalue );
XSSFCellStyle hiddenRowStyle = wb.createCellStyle();
hiddenRowStyle.setHidden( true );
row.setRowStyle( hiddenRowStyle );
Assert.assertTrue( row.getRowStyle().getHidden() );
try (FileOutputStream fileOut = new FileOutputStream( new File( "target/PoiTestDrive.xlsx" ) )) {
wb.write( fileOut );
} catch ( IOException ex ) {
ex.printStackTrace();
}
// does not work, resulting Excel file shows first row.
}
}