1 2 17 18 19 package org.apache.poi.hssf.usermodel; 20 21 import junit.framework.TestCase; 22 import org.apache.poi.hssf.usermodel.HSSFSheet; 23 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 24 import org.apache.poi.util.TempFile; 25 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 30 37 38 public class TestSheetShiftRows extends TestCase { 39 40 44 public TestSheetShiftRows(String arg0) { 45 super(arg0); 46 } 47 48 56 public void testShiftRows() throws Exception 57 { 58 String filename = System.getProperty( "HSSF.testdata.path" ); 60 filename = filename + "/SimpleMultiCell.xls"; 61 FileInputStream fin = new FileInputStream ( filename ); 62 HSSFWorkbook wb = new HSSFWorkbook( fin ); 63 fin.close(); 64 HSSFSheet s = wb.getSheetAt( 0 ); 65 66 s.shiftRows( 1, 1, 1 ); 68 File tempFile = TempFile.createTempFile( "shift", "test.xls" ); 69 FileOutputStream fout = new FileOutputStream ( tempFile ); 70 wb.write( fout ); 71 fout.close(); 72 73 fin = new FileInputStream ( tempFile ); 76 wb = new HSSFWorkbook( fin ); 77 fin.close(); 78 s = wb.getSheetAt( 0 ); 79 80 assertEquals( s.getRow( 0 ).getPhysicalNumberOfCells(), 1 ); 81 assertTrue( s.getRow( 1 ) == null || s.getRow( 1 ).getPhysicalNumberOfCells() == 0 ); 82 assertEquals( s.getRow( 2 ).getPhysicalNumberOfCells(), 2 ); 83 assertEquals( s.getRow( 3 ).getPhysicalNumberOfCells(), 4 ); 84 assertEquals( s.getRow( 4 ).getPhysicalNumberOfCells(), 5 ); 85 86 s.shiftRows( 0, 2, 3 ); 89 tempFile = TempFile.createTempFile( "shift", "test.xls" ); 90 fout = new FileOutputStream ( tempFile ); 91 wb.write( fout ); 92 fout.close(); 93 94 fin = new FileInputStream ( tempFile ); 96 wb = new HSSFWorkbook( fin ); 97 fin.close(); 98 s = wb.getSheetAt( 0 ); 99 assertTrue( s.getRow( 0 ) == null || s.getRow( 0 ).getPhysicalNumberOfCells() == 0 ); 100 assertTrue( s.getRow( 1 ) == null || s.getRow( 1 ).getPhysicalNumberOfCells() == 0 ); 101 assertTrue( s.getRow( 2 ) == null || s.getRow( 2 ).getPhysicalNumberOfCells() == 0 ); 102 assertEquals( s.getRow( 3 ).getPhysicalNumberOfCells(), 1 ); 103 assertTrue( s.getRow( 4 ) == null || s.getRow( 4 ).getPhysicalNumberOfCells() == 0 ); 104 assertEquals( s.getRow( 5 ).getPhysicalNumberOfCells(), 2 ); 105 106 fin = new FileInputStream ( filename ); 108 wb = new HSSFWorkbook( fin ); 109 fin.close(); 110 s = wb.getSheetAt( 0 ); 111 112 s.shiftRows( 2, 3, -2 ); 114 tempFile = TempFile.createTempFile( "shift", "test.xls" ); 115 fout = new FileOutputStream ( tempFile ); 116 wb.write( fout ); 117 fout.close(); 118 119 fin = new FileInputStream ( tempFile ); 121 wb = new HSSFWorkbook( fin ); 122 fin.close(); 123 s = wb.getSheetAt( 0 ); 124 assertEquals( s.getRow( 0 ).getPhysicalNumberOfCells(), 3 ); 125 assertEquals( s.getRow( 1 ).getPhysicalNumberOfCells(), 4 ); 126 assertTrue( s.getRow( 2 ) == null || s.getRow( 2 ).getPhysicalNumberOfCells() == 0 ); 127 assertTrue( s.getRow( 3 ) == null || s.getRow( 3 ).getPhysicalNumberOfCells() == 0 ); 128 assertEquals( s.getRow( 4 ).getPhysicalNumberOfCells(), 5 ); 129 } 130 131 136 public void testShiftRow(){ 137 HSSFWorkbook b = new HSSFWorkbook(); 138 HSSFSheet s = b.createSheet(); 139 s.createRow(0).createCell((short)0).setCellValue("TEST1"); 140 s.createRow(3).createCell((short)0).setCellValue("TEST2"); 141 s.shiftRows(0,4,1); 142 } 143 144 149 public void testShiftRow0(){ 150 HSSFWorkbook b = new HSSFWorkbook(); 151 HSSFSheet s = b.createSheet(); 152 s.createRow(0).createCell((short)0).setCellValue("TEST1"); 153 s.createRow(3).createCell((short)0).setCellValue("TEST2"); 154 s.shiftRows(0,4,1); 155 } 156 157 161 public void testShiftRowBreaks(){ 162 HSSFWorkbook b = new HSSFWorkbook(); 163 HSSFSheet s = b.createSheet(); 164 HSSFRow row = s.createRow(4); 165 row.createCell((short)0).setCellValue("test"); 166 s.setRowBreak(4); 167 168 s.shiftRows(4, 4, 2); 169 assertTrue("Row number 6 should have a pagebreak", s.isRowBroken(6)); 170 171 } 172 } 173 174 | Popular Tags |