View | Details | Raw Unified | Return to bug 16936
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/usermodel/HSSFRow.java (+29 lines)
Lines 67-72 Link Here
67
     */
67
     */
68
68
69
    private Sheet sheet;
69
    private Sheet sheet;
70
    
71
    private HSSFCellStyle style;
70
72
71
    protected HSSFRow()
73
    protected HSSFRow()
72
    {
74
    {
Lines 424-429 Link Here
424
            return -1;
426
            return -1;
425
        return cellnum;
427
        return cellnum;
426
    }
428
    }
429
    
430
431
    /**
432
     * set the style for the entire row.  The style should be an HSSFCellStyle created/retreived from
433
     * the HSSFWorkbook.
434
     *
435
     * @param style  reference contained in the workbook
436
     * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle()
437
     * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
438
     * @see org.apache.poi.hssf.usermodel.HSSFCell#setCellStyle(HSSFCellStyle)
439
     */
440
    public void setRowStyle(HSSFCellStyle style)
441
    {
442
        this.style = style;
443
        row.setXFIndex(style.getIndex());
444
    }
445
446
    /**
447
     * get the style for the entire row.  This is a reference to a cell style contained in the workbook
448
     * object.
449
     * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
450
     * @see org.apache.poi.hssf.usermodel.HSSFCell#getCellStyle()
451
     */
452
    public HSSFCellStyle getRowStyle()
453
    {
454
        return style;
455
    }
427
456
428
    /**
457
    /**
429
     * @return cell iterator of the physically defined cells.  Note element 4 may
458
     * @return cell iterator of the physically defined cells.  Note element 4 may
(-)src/testcases/org/apache/poi/hssf/usermodel/TestRowStyle.java (+190 lines)
Added Link Here
1
/* ====================================================================
2
   Copyright 2002-2004   Apache Software Foundation
3
4
   Licensed under the Apache License, Version 2.0 (the "License");
5
   you may not use this file except in compliance with the License.
6
   You may obtain a copy of the License at
7
8
       http://www.apache.org/licenses/LICENSE-2.0
9
10
   Unless required by applicable law or agreed to in writing, software
11
   distributed under the License is distributed on an "AS IS" BASIS,
12
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
   See the License for the specific language governing permissions and
14
   limitations under the License.
15
==================================================================== */
16
        
17
18
/*
19
 * TestRowStyle.java
20
 *
21
 * Created on May 20, 2005
22
 */
23
package org.apache.poi.hssf.usermodel;
24
25
import java.io.File;
26
import java.io.FileOutputStream;
27
import java.io.IOException;
28
29
import junit.framework.TestCase;
30
31
import org.apache.poi.util.TempFile;
32
33
/**
34
 * Class to test row styling functionality
35
 *
36
 * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
37
 */
38
39
public class TestRowStyle
40
    extends TestCase
41
{
42
43
    /** Creates a new instance of TestCellStyle */
44
45
    public TestRowStyle(String name)
46
    {
47
        super(name);
48
    }
49
50
    /**
51
     * TEST NAME:  Test Write Sheet Font <P>
52
     * OBJECTIVE:  Test that HSSF can create a simple spreadsheet with numeric and string values and styled with fonts.<P>
53
     * SUCCESS:    HSSF creates a sheet.  Filesize matches a known good.  HSSFSheet objects
54
     *             Last row, first row is tested against the correct values (99,0).<P>
55
     * FAILURE:    HSSF does not create a sheet or excepts.  Filesize does not match the known good.
56
     *             HSSFSheet last row or first row is incorrect.             <P>
57
     *
58
     */
59
60
    public void testWriteSheetFont()
61
        throws IOException
62
    {
63
        File             file = TempFile.createTempFile("testWriteSheetFont",
64
                                                    ".xls");
65
        FileOutputStream out  = new FileOutputStream("c:/temp/1.xls");
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
            r.setRowStyle(cs);
80
            r.createCell((short) 0);
81
        }
82
        wb.write(out);
83
        out.close();
84
        SanityChecker sanityChecker = new SanityChecker();
85
        sanityChecker.checkHSSFWorkbook(wb);
86
        assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
87
        assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
88
    }
89
90
    /**
91
     * Tests that is creating a file with a date or an calendar works correctly.
92
     */
93
    public void testDataStyle()
94
            throws Exception
95
    {
96
        File             file = TempFile.createTempFile("testWriteSheetStyleDate",
97
                                                    ".xls");
98
        FileOutputStream out  = new FileOutputStream(file);
99
        HSSFWorkbook     wb   = new HSSFWorkbook();
100
        HSSFSheet        s    = wb.createSheet();
101
        HSSFCellStyle    cs   = wb.createCellStyle();
102
        HSSFRow row = s.createRow((short)0);
103
104
        // with Date:
105
        cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
106
        row.setRowStyle(cs);
107
        row.createCell((short) 0);
108
109
110
        // with Calendar:
111
        row = s.createRow((short)1);
112
        cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
113
        row.setRowStyle(cs);
114
        row.createCell((short) 0);
115
116
        wb.write(out);
117
        out.close();
118
        SanityChecker sanityChecker = new SanityChecker();
119
        sanityChecker.checkHSSFWorkbook(wb);
120
121
        assertEquals("LAST ROW ", 1, s.getLastRowNum());
122
        assertEquals("FIRST ROW ", 0, s.getFirstRowNum());
123
124
    }
125
126
    /**
127
     * TEST NAME:  Test Write Sheet Style <P>
128
     * OBJECTIVE:  Test that HSSF can create a simple spreadsheet with numeric and string values and styled with colors
129
     *             and borders.<P>
130
     * SUCCESS:    HSSF creates a sheet.  Filesize matches a known good.  HSSFSheet objects
131
     *             Last row, first row is tested against the correct values (99,0).<P>
132
     * FAILURE:    HSSF does not create a sheet or excepts.  Filesize does not match the known good.
133
     *             HSSFSheet last row or first row is incorrect.             <P>
134
     *
135
     */
136
137
    public void testWriteSheetStyle()
138
        throws IOException
139
    {
140
        File             file = TempFile.createTempFile("testWriteSheetStyle",
141
                                                    ".xls");
142
        FileOutputStream out  = new FileOutputStream(file);
143
        HSSFWorkbook     wb   = new HSSFWorkbook();
144
        HSSFSheet        s    = wb.createSheet();
145
        HSSFRow          r    = null;
146
        HSSFFont         fnt  = wb.createFont();
147
        HSSFCellStyle    cs   = wb.createCellStyle();
148
        HSSFCellStyle    cs2  = wb.createCellStyle();
149
150
        cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);
151
        cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
152
        cs.setBorderRight(HSSFCellStyle.BORDER_THIN);
153
        cs.setBorderTop(HSSFCellStyle.BORDER_THIN);
154
        cs.setFillForegroundColor(( short ) 0xA);
155
        cs.setFillPattern(( short ) 1);
156
        fnt.setColor(( short ) 0xf);
157
        fnt.setItalic(true);
158
        cs2.setFillForegroundColor(( short ) 0x0);
159
        cs2.setFillPattern(( short ) 1);
160
        cs2.setFont(fnt);
161
        for (short rownum = ( short ) 0; rownum < 100; rownum++)
162
        {
163
            r = s.createRow(rownum);
164
            r.setRowStyle(cs);
165
            r.createCell((short) 0);
166
            
167
            rownum++;
168
            if (rownum >= 100) break; // I feel too lazy to check if this isreqd :-/ 
169
            
170
            r = s.createRow(rownum);
171
            r.setRowStyle(cs2);
172
            r.createCell((short) 0);
173
        }
174
        wb.write(out);
175
        out.close();
176
        SanityChecker sanityChecker = new SanityChecker();
177
        sanityChecker.checkHSSFWorkbook(wb);
178
        assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
179
        assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
180
181
        // assert((s.getLastRowNum() == 99));
182
    }
183
184
    public static void main(String [] ignored_args)
185
    {
186
        System.out
187
            .println("Testing org.apache.poi.hssf.usermodel.HSSFCellStyle");
188
        junit.textui.TestRunner.run(TestCellStyle.class);
189
    }
190
}

Return to bug 16936