1 2 17 18 19 24 package org.apache.poi.hssf.usermodel; 25 26 import java.io.*; 27 28 import java.util.*; 29 30 import junit.framework.*; 31 import org.apache.poi.util.TempFile; 32 33 38 39 public class TestCellStyle 40 extends TestCase 41 { 42 43 44 45 public TestCellStyle(String name) 46 { 47 super(name); 48 } 49 50 59 60 public void testWriteSheetFont() 61 throws IOException 62 { 63 File file = TempFile.createTempFile("testWriteSheetFont", 64 ".xls"); 65 FileOutputStream out = new FileOutputStream(file); 66 HSSFWorkbook wb = new HSSFWorkbook(); 67 HSSFSheet s = wb.createSheet(); 68 HSSFRow r = null; 69 HSSFCell c = null; 70 HSSFFont fnt = wb.createFont(); 71 HSSFCellStyle cs = wb.createCellStyle(); 72 73 fnt.setColor(HSSFFont.COLOR_RED); 74 fnt.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 75 cs.setFont(fnt); 76 for (short rownum = ( short ) 0; rownum < 100; rownum++) 77 { 78 r = s.createRow(rownum); 79 80 for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2) 82 { 83 c = r.createCell(cellnum); 84 c.setCellValue(rownum * 10000 + cellnum 85 + ((( double ) rownum / 1000) 86 + (( double ) cellnum / 10000))); 87 c = r.createCell(( short ) (cellnum + 1)); 88 c.setCellValue("TEST"); 89 c.setCellStyle(cs); 90 } 91 } 92 wb.write(out); 93 out.close(); 94 SanityChecker sanityChecker = new SanityChecker(); 95 sanityChecker.checkHSSFWorkbook(wb); 96 assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); 97 assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); 98 99 } 101 102 105 public void testDataStyle() 106 throws Exception 107 { 108 File file = TempFile.createTempFile("testWriteSheetStyleDate", 109 ".xls"); 110 FileOutputStream out = new FileOutputStream(file); 111 HSSFWorkbook wb = new HSSFWorkbook(); 112 HSSFSheet s = wb.createSheet(); 113 HSSFCellStyle cs = wb.createCellStyle(); 114 HSSFRow row = s.createRow((short)0); 115 116 HSSFCell cell = row.createCell((short)1); 118 cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); 119 cell.setCellStyle(cs); 120 cell.setCellValue(new Date()); 121 122 cell = row.createCell((short)2); 124 cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); 125 cell.setCellStyle(cs); 126 Calendar cal = Calendar.getInstance(); 127 cal.setTime(new Date()); 128 cell.setCellValue(cal); 129 130 wb.write(out); 131 out.close(); 132 SanityChecker sanityChecker = new SanityChecker(); 133 sanityChecker.checkHSSFWorkbook(wb); 134 135 assertEquals("LAST ROW ", 0, s.getLastRowNum()); 136 assertEquals("FIRST ROW ", 0, s.getFirstRowNum()); 137 138 } 139 140 150 151 public void testWriteSheetStyle() 152 throws IOException 153 { 154 File file = TempFile.createTempFile("testWriteSheetStyle", 155 ".xls"); 156 FileOutputStream out = new FileOutputStream(file); 157 HSSFWorkbook wb = new HSSFWorkbook(); 158 HSSFSheet s = wb.createSheet(); 159 HSSFRow r = null; 160 HSSFCell c = null; 161 HSSFFont fnt = wb.createFont(); 162 HSSFCellStyle cs = wb.createCellStyle(); 163 HSSFCellStyle cs2 = wb.createCellStyle(); 164 165 cs.setBorderBottom(HSSFCellStyle.BORDER_THIN); 166 cs.setBorderLeft(HSSFCellStyle.BORDER_THIN); 167 cs.setBorderRight(HSSFCellStyle.BORDER_THIN); 168 cs.setBorderTop(HSSFCellStyle.BORDER_THIN); 169 cs.setFillForegroundColor(( short ) 0xA); 170 cs.setFillPattern(( short ) 1); 171 fnt.setColor(( short ) 0xf); 172 fnt.setItalic(true); 173 cs2.setFillForegroundColor(( short ) 0x0); 174 cs2.setFillPattern(( short ) 1); 175 cs2.setFont(fnt); 176 for (short rownum = ( short ) 0; rownum < 100; rownum++) 177 { 178 r = s.createRow(rownum); 179 180 for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2) 182 { 183 c = r.createCell(cellnum); 184 c.setCellValue(rownum * 10000 + cellnum 185 + ((( double ) rownum / 1000) 186 + (( double ) cellnum / 10000))); 187 c.setCellStyle(cs); 188 c = r.createCell(( short ) (cellnum + 1)); 189 c.setCellValue("TEST"); 190 c.setCellStyle(cs2); 191 } 192 } 193 wb.write(out); 194 out.close(); 195 SanityChecker sanityChecker = new SanityChecker(); 196 sanityChecker.checkHSSFWorkbook(wb); 197 assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); 198 assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); 199 200 } 202 203 public static void main(String [] ignored_args) 204 { 205 System.out 206 .println("Testing org.apache.poi.hssf.usermodel.HSSFCellStyle"); 207 junit.textui.TestRunner.run(TestCellStyle.class); 208 } 209 } 210 | Popular Tags |