Index: src/java/org/apache/poi/hssf/usermodel/HSSFRow.java =================================================================== RCS file: /home/cvspublic/jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java,v retrieving revision 1.20 diff -u -r1.20 HSSFRow.java --- src/java/org/apache/poi/hssf/usermodel/HSSFRow.java 1 May 2005 11:26:18 -0000 1.20 +++ src/java/org/apache/poi/hssf/usermodel/HSSFRow.java 20 May 2005 18:29:12 -0000 @@ -24,6 +24,7 @@ import org.apache.poi.hssf.model.Sheet; import org.apache.poi.hssf.model.Workbook; import org.apache.poi.hssf.record.CellValueRecordInterface; +import org.apache.poi.hssf.record.ExtendedFormatRecord; import org.apache.poi.hssf.record.RowRecord; import java.util.HashMap; @@ -67,6 +68,8 @@ */ private Sheet sheet; + + private HSSFCellStyle style; protected HSSFRow() { @@ -94,6 +97,8 @@ row.setFirstCol((short) -1); setRowNum(rowNum); + ExtendedFormatRecord xf = book.getExFormatAt(0xf); + setRowStyle(new HSSFCellStyle(( short ) 0xf, xf)); } /** @@ -114,6 +119,8 @@ row = record; setRowNum(record.getRowNumber()); + ExtendedFormatRecord xf = book.getExFormatAt(record.getXFIndex()); + setRowStyle(new HSSFCellStyle(( short ) 0xf, xf)); } /** @@ -424,6 +431,33 @@ return -1; return cellnum; } + + + /** + * set the style for the entire row. The style should be an HSSFCellStyle created/retreived from + * the HSSFWorkbook. + * + * @param style reference contained in the workbook + * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle() + * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short) + * @see org.apache.poi.hssf.usermodel.HSSFCell#setCellStyle(HSSFCellStyle) + */ + public void setRowStyle(HSSFCellStyle style) + { + this.style = style; + row.setXFIndex(style.getIndex()); + } + + /** + * get the style for the entire row. This is a reference to a cell style contained in the workbook + * object. + * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short) + * @see org.apache.poi.hssf.usermodel.HSSFCell#getCellStyle() + */ + public HSSFCellStyle getRowStyle() + { + return style; + } /** * @return cell iterator of the physically defined cells. Note element 4 may Index: src/testcases/org/apache/poi/hssf/usermodel/TestRowStyle.java =================================================================== RCS file: src/testcases/org/apache/poi/hssf/usermodel/TestRowStyle.java diff -N src/testcases/org/apache/poi/hssf/usermodel/TestRowStyle.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/testcases/org/apache/poi/hssf/usermodel/TestRowStyle.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,217 @@ +/* ==================================================================== + Copyright 2002-2004 Apache Software Foundation + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + + +/* + * TestRowStyle.java + * + * Created on May 20, 2005 + */ +package org.apache.poi.hssf.usermodel; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +import junit.framework.TestCase; + +import org.apache.poi.util.TempFile; + +/** + * Class to test row styling functionality + * + * @author Amol S. Deshmukh < amolweb at ya hoo dot com > + */ + +public class TestRowStyle + extends TestCase +{ + + /** Creates a new instance of TestCellStyle */ + + public TestRowStyle(String name) + { + super(name); + } + + /** + * TEST NAME: Test Write Sheet Font

+ * OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with fonts.

+ * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects + * Last row, first row is tested against the correct values (99,0).

+ * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. + * HSSFSheet last row or first row is incorrect.

+ * + */ + + public void testWriteSheetFont() + throws IOException + { + File file = TempFile.createTempFile("testWriteSheetFont", + ".xls"); + FileOutputStream out = new FileOutputStream("c:/temp/1.xls"); + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet(); + HSSFRow r = null; + HSSFCell c = null; + HSSFFont fnt = wb.createFont(); + HSSFCellStyle cs = wb.createCellStyle(); + + fnt.setColor(HSSFFont.COLOR_RED); + fnt.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + cs.setFont(fnt); + for (short rownum = ( short ) 0; rownum < 100; rownum++) + { + r = s.createRow(rownum); + r.setRowStyle(cs); + r.createCell((short) 0); + } + wb.write(out); + out.close(); + SanityChecker sanityChecker = new SanityChecker(); + sanityChecker.checkHSSFWorkbook(wb); + assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); + assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); + } + + /** + * Tests that is creating a file with a date or an calendar works correctly. + */ + public void testDataStyle() + throws Exception + { + File file = TempFile.createTempFile("testWriteSheetStyleDate", + ".xls"); + FileOutputStream out = new FileOutputStream(file); + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet(); + HSSFCellStyle cs = wb.createCellStyle(); + HSSFRow row = s.createRow((short)0); + + // with Date: + cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); + row.setRowStyle(cs); + row.createCell((short) 0); + + + // with Calendar: + row = s.createRow((short)1); + cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); + row.setRowStyle(cs); + row.createCell((short) 0); + + wb.write(out); + out.close(); + SanityChecker sanityChecker = new SanityChecker(); + sanityChecker.checkHSSFWorkbook(wb); + + assertEquals("LAST ROW ", 1, s.getLastRowNum()); + assertEquals("FIRST ROW ", 0, s.getFirstRowNum()); + + } + + /** + * TEST NAME: Test Write Sheet Style

+ * OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with colors + * and borders.

+ * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects + * Last row, first row is tested against the correct values (99,0).

+ * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good. + * HSSFSheet last row or first row is incorrect.

+ * + */ + + public void testWriteSheetStyle() + throws IOException + { + File file = TempFile.createTempFile("testWriteSheetStyle", + ".xls"); + FileOutputStream out = new FileOutputStream(file); + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet(); + HSSFRow r = null; + HSSFFont fnt = wb.createFont(); + HSSFCellStyle cs = wb.createCellStyle(); + HSSFCellStyle cs2 = wb.createCellStyle(); + + cs.setBorderBottom(HSSFCellStyle.BORDER_THIN); + cs.setBorderLeft(HSSFCellStyle.BORDER_THIN); + cs.setBorderRight(HSSFCellStyle.BORDER_THIN); + cs.setBorderTop(HSSFCellStyle.BORDER_THIN); + cs.setFillForegroundColor(( short ) 0xA); + cs.setFillPattern(( short ) 1); + fnt.setColor(( short ) 0xf); + fnt.setItalic(true); + cs2.setFillForegroundColor(( short ) 0x0); + cs2.setFillPattern(( short ) 1); + cs2.setFont(fnt); + for (short rownum = ( short ) 0; rownum < 100; rownum++) + { + r = s.createRow(rownum); + r.setRowStyle(cs); + r.createCell((short) 0); + + rownum++; + if (rownum >= 100) break; // I feel too lazy to check if this isreqd :-/ + + r = s.createRow(rownum); + r.setRowStyle(cs2); + r.createCell((short) 0); + } + wb.write(out); + out.close(); + SanityChecker sanityChecker = new SanityChecker(); + sanityChecker.checkHSSFWorkbook(wb); + assertEquals("LAST ROW == 99", 99, s.getLastRowNum()); + assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum()); + + FileInputStream fis = new FileInputStream(file); + wb = new HSSFWorkbook(fis); + s = wb.getSheetAt(0); + assertNotNull("Sheet is not null", s); + + for (short rownum = ( short ) 0; rownum < 100; rownum++) + { + r = s.getRow(rownum); + assertNotNull("Row is not null", r); + + cs = r.getRowStyle(); + assertEquals("FillForegroundColor for row: ", cs.getBorderBottom(), HSSFCellStyle.BORDER_THIN); + assertEquals("FillPattern for row: ", cs.getBorderLeft(), HSSFCellStyle.BORDER_THIN); + assertEquals("FillForegroundColor for row: ", cs.getBorderRight(), HSSFCellStyle.BORDER_THIN); + assertEquals("FillPattern for row: ", cs.getBorderTop(), HSSFCellStyle.BORDER_THIN); + assertEquals("FillForegroundColor for row: ", cs.getFillForegroundColor(), 0xA); + assertEquals("FillPattern for row: ", cs.getFillPattern(), (short) 0x1); + + rownum++; + if (rownum >= 100) break; // I feel too lazy to check if this isreqd :-/ + + r = s.getRow(rownum); + assertNotNull("Row is not null", r); + cs2 = r.getRowStyle(); + assertEquals("FillForegroundColor for row: ", cs2.getFillForegroundColor(), (short) 0x0); + assertEquals("FillPattern for row: ", cs2.getFillPattern(), (short) 0x1); + } + } + + public static void main(String [] ignored_args) + { + System.out + .println("Testing org.apache.poi.hssf.usermodel.HSSFCellStyle"); + junit.textui.TestRunner.run(TestCellStyle.class); + } +}