1 2 17 18 19 package org.apache.poi.hssf.usermodel; 20 21 import junit.framework.TestCase; 22 23 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 24 import org.apache.poi.hssf.model.Sheet; 25 import org.apache.poi.hssf.record.Record; 26 import org.apache.poi.hssf.record.BOFRecord; 27 import org.apache.poi.hssf.record.EOFRecord; 28 import org.apache.poi.hssf.util.CellReference; 29 import org.apache.poi.hssf.util.HSSFColor; 30 import org.apache.poi.util.TempFile; 31 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 import java.io.FileOutputStream ; 35 36 import java.util.List ; 37 import java.util.Iterator ; 38 import java.util.Date ; 39 import java.util.GregorianCalendar ; 40 41 47 48 public class TestHSSFCell 49 extends TestCase { 50 public TestHSSFCell(String s) { 51 super(s); 52 } 53 54 57 public void testBoolErr() 58 throws java.io.IOException { 59 String readFilename = System.getProperty("HSSF.testdata.path"); 60 61 File file = TempFile.createTempFile("testBoolErr",".xls"); 62 FileOutputStream out = new FileOutputStream (file); 63 HSSFWorkbook wb = new HSSFWorkbook(); 64 HSSFSheet s = wb.createSheet("Sheet1"); 65 HSSFRow r = null; 66 HSSFCell c = null; 67 r = s.createRow((short)0); 68 c=r.createCell((short)1); 69 c.setCellValue(true); 71 72 c=r.createCell((short)2); 73 c.setCellValue(false); 75 76 r = s.createRow((short)1); 77 c=r.createCell((short)1); 78 c.setCellErrorValue((byte)0); 80 81 c=r.createCell((short)2); 82 c.setCellErrorValue((byte)7); 84 85 86 wb.write(out); 87 out.close(); 88 89 assertTrue("file exists",file.exists()); 90 91 FileInputStream in = new FileInputStream (file); 92 wb = new HSSFWorkbook(in); 93 s = wb.getSheetAt(0); 94 r = s.getRow(0); 95 c = r.getCell((short)1); 96 assertTrue("boolean value 0,1 = true",c.getBooleanCellValue()); 97 c = r.getCell((short)2); 98 assertTrue("boolean value 0,2 = false",c.getBooleanCellValue()==false); 99 r = s.getRow(1); 100 c = r.getCell((short)1); 101 assertTrue("boolean value 0,1 = 0",c.getErrorCellValue() == 0); 102 c = r.getCell((short)2); 103 assertTrue("boolean value 0,2 = 7",c.getErrorCellValue() == 7); 104 105 in.close(); 106 } 107 108 113 public void testDateWindowing() throws Exception { 114 GregorianCalendar cal = new GregorianCalendar (2000,0,1); Date date = cal.getTime(); 116 String path = System.getProperty("HSSF.testdata.path"); 117 118 String filename = path + "/1900DateWindowing.xls"; 120 FileInputStream stream = new FileInputStream (filename); 121 POIFSFileSystem fs = new POIFSFileSystem(stream); 122 HSSFWorkbook workbook = new HSSFWorkbook(fs); 123 HSSFSheet sheet = workbook.getSheetAt(0); 124 125 assertEquals("Date from file using 1900 Date Windowing", 126 date.getTime(), 127 sheet.getRow(0).getCell((short)0) 128 .getDateCellValue().getTime()); 129 stream.close(); 130 131 filename = path + "/1904DateWindowing.xls"; 133 stream = new FileInputStream (filename); 134 fs = new POIFSFileSystem(stream); 135 workbook = new HSSFWorkbook(fs); 136 sheet = workbook.getSheetAt(0); 137 138 assertEquals("Date from file using 1904 Date Windowing", 139 date.getTime(), 140 sheet.getRow(0).getCell((short)0) 141 .getDateCellValue().getTime()); 142 stream.close(); 143 } 144 145 148 public void testActiveCell() throws Exception 149 { 150 String dir = System.getProperty("HSSF.testdata.path"); 152 File sample = new File(dir + "/Simple.xls"); 153 assertTrue("Simple.xls exists and is readable", sample.canRead()); 154 FileInputStream fis = new FileInputStream (sample); 155 HSSFWorkbook book = new HSSFWorkbook(fis); 156 fis.close(); 157 158 HSSFSheet umSheet = book.getSheetAt(0); 160 Sheet s = umSheet.getSheet(); 161 assertEquals("Initial active cell should be in col 0", 162 (short) 0, s.getActiveCellCol()); 163 assertEquals("Initial active cell should be on row 1", 164 1, s.getActiveCellRow()); 165 166 HSSFCell cell = umSheet.createRow(3).createCell((short) 2); 168 cell.setAsActiveCell(); 169 assertEquals("After modify, active cell should be in col 2", 170 (short) 2, s.getActiveCellCol()); 171 assertEquals("After modify, active cell should be on row 3", 172 3, s.getActiveCellRow()); 173 174 File temp = TempFile.createTempFile("testActiveCell", ".xls"); 176 FileOutputStream fos = new FileOutputStream (temp); 177 book.write(fos); 178 fos.close(); 179 180 fis = new FileInputStream (temp); 181 book = new HSSFWorkbook(fis); 182 fis.close(); 183 umSheet = book.getSheetAt(0); 184 s = umSheet.getSheet(); 185 186 assertEquals("After serialize, active cell should be in col 2", 187 (short) 2, s.getActiveCellCol()); 188 assertEquals("After serialize, active cell should be on row 3", 189 3, s.getActiveCellRow()); 190 } 191 192 195 public void testFormulaStyle() 196 throws java.io.IOException { 197 String readFilename = System.getProperty("HSSF.testdata.path"); 198 199 File file = TempFile.createTempFile("testFormulaStyle",".xls"); 200 FileOutputStream out = new FileOutputStream (file); 201 HSSFWorkbook wb = new HSSFWorkbook(); 202 HSSFSheet s = wb.createSheet("Sheet1"); 203 HSSFRow r = null; 204 HSSFCell c = null; 205 HSSFCellStyle cs = wb.createCellStyle(); 206 HSSFFont f = wb.createFont(); 207 f.setFontHeightInPoints((short) 20); 208 f.setColor((short) HSSFColor.RED.index); 209 f.setBoldweight(f.BOLDWEIGHT_BOLD); 210 f.setFontName("Arial Unicode MS"); 211 cs.setFillBackgroundColor((short)3); 212 cs.setFont(f); 213 cs.setBorderTop((short)1); 214 cs.setBorderRight((short)1); 215 cs.setBorderLeft((short)1); 216 cs.setBorderBottom((short)1); 217 218 r = s.createRow((short)0); 219 c=r.createCell((short)0); 220 c.setCellStyle(cs); 221 c.setCellFormula("2*3"); 222 223 wb.write(out); 224 out.close(); 225 226 assertTrue("file exists",file.exists()); 227 228 FileInputStream in = new FileInputStream (file); 229 wb = new HSSFWorkbook(in); 230 s = wb.getSheetAt(0); 231 r = s.getRow(0); 232 c = r.getCell((short)0); 233 234 assertTrue("Formula Cell at 0,0", (c.getCellType()==c.CELL_TYPE_FORMULA)); 235 cs = c.getCellStyle(); 236 237 assertNotNull("Formula Cell Style", cs); 238 assertTrue("Font Index Matches", (cs.getFontIndex() == f.getIndex())); 239 assertTrue("Top Border", (cs.getBorderTop() == (short)1)); 240 assertTrue("Left Border", (cs.getBorderLeft() == (short)1)); 241 assertTrue("Right Border", (cs.getBorderRight() == (short)1)); 242 assertTrue("Bottom Border", (cs.getBorderBottom() == (short)1)); 243 244 in.close(); 245 } 246 247 public static void main(String [] args) { 248 System.out 249 .println("Testing org.apache.poi.hssf.usermodel.TestHSSFCell"); 250 junit.textui.TestRunner.run(TestHSSFCell.class); 251 } 252 253 } 254 255
| Popular Tags
|