When we create a workbook from existing Excel file and create a new sheet from the work book, the "setRepeatingRowsAndColumns" method does not work. I have write a Junit test case to test it, and it is repeatable. The Junit test is bellow: package org.apache.poi.hssf.usermodel; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import junit.framework.TestCase; import org.apache.poi.hssf.model.Workbook; import org.apache.poi.hssf.record.BackupRecord; import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate; import org.apache.poi.hssf.util.Region; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class TestWorkbook extends TestCase { /** * Constructor TestWorkbook * * @param name */ public TestWorkbook(String name) { super(name); } /** * Generate a file to visually/programmatically verify repeating rows and cols made it */ public void testRepeatingColsRows() throws IOException { FileInputStream fis = new FileInputStream(new File ("c://temp//myExistFile.xls")); HSSFWorkbook workbook = new HSSFWorkbook(fis); HSSFSheet sheet = workbook.createSheet("Test Print Titles"); String sheetName = workbook.getSheetName(0); HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell((short)1); cell.setCellValue("hi"); workbook.setRepeatingRowsAndColumns(workbook.getSheetIndex ("Test Print Titles"), 0, 1, 0, 0); File file;// = File.createTempFile ("testPrintTitles",".xls"); file = new File("c://temp//mytest.xls"); FileOutputStream fileOut = new FileOutputStream(file); workbook.write(fileOut); fileOut.close(); assertTrue("file exists",file.exists()); } public static void main(String [] ignored_args) { String filename = System.getProperty("HSSF.testdata.path"); // assume this is relative to basedir if (filename == null) { System.setProperty( "HSSF.testdata.path", "src/testcases/org/apache/poi/hssf/data"); } System.out .println("Testing org.apache.poi.hssf.usermodel.HSSFWorkbook"); junit.textui.TestRunner.run(TestWorkbook.class); } }
Fixed. Was result of not setting correct index record in the name record. Was using extern sheet index rather than actual sheet index.