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

(-)src/java/org/apache/poi/hssf/usermodel/HSSFRow.java (+34 lines)
Lines 24-29 Link Here
24
import org.apache.poi.hssf.model.Sheet;
24
import org.apache.poi.hssf.model.Sheet;
25
import org.apache.poi.hssf.model.Workbook;
25
import org.apache.poi.hssf.model.Workbook;
26
import org.apache.poi.hssf.record.CellValueRecordInterface;
26
import org.apache.poi.hssf.record.CellValueRecordInterface;
27
import org.apache.poi.hssf.record.ExtendedFormatRecord;
27
import org.apache.poi.hssf.record.RowRecord;
28
import org.apache.poi.hssf.record.RowRecord;
28
29
29
import java.util.HashMap;
30
import java.util.HashMap;
Lines 67-72 Link Here
67
     */
68
     */
68
69
69
    private Sheet sheet;
70
    private Sheet sheet;
71
    
72
    private HSSFCellStyle style;
70
73
71
    protected HSSFRow()
74
    protected HSSFRow()
72
    {
75
    {
Lines 94-99 Link Here
94
        row.setFirstCol((short) -1);
97
        row.setFirstCol((short) -1);
95
98
96
        setRowNum(rowNum);
99
        setRowNum(rowNum);
100
        ExtendedFormatRecord xf = book.getExFormatAt(0xf);
101
        setRowStyle(new HSSFCellStyle(( short ) 0xf, xf));
97
    }
102
    }
98
103
99
    /**
104
    /**
Lines 114-119 Link Here
114
        row = record;
119
        row = record;
115
120
116
        setRowNum(record.getRowNumber());
121
        setRowNum(record.getRowNumber());
122
        ExtendedFormatRecord xf = book.getExFormatAt(record.getXFIndex());
123
        setRowStyle(new HSSFCellStyle(( short ) 0xf, xf));
117
    }
124
    }
118
125
119
    /**
126
    /**
Lines 424-429 Link Here
424
            return -1;
431
            return -1;
425
        return cellnum;
432
        return cellnum;
426
    }
433
    }
434
    
435
436
    /**
437
     * set the style for the entire row.  The style should be an HSSFCellStyle created/retreived from
438
     * the HSSFWorkbook.
439
     *
440
     * @param style  reference contained in the workbook
441
     * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle()
442
     * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
443
     * @see org.apache.poi.hssf.usermodel.HSSFCell#setCellStyle(HSSFCellStyle)
444
     */
445
    public void setRowStyle(HSSFCellStyle style)
446
    {
447
        this.style = style;
448
        row.setXFIndex(style.getIndex());
449
    }
450
451
    /**
452
     * get the style for the entire row.  This is a reference to a cell style contained in the workbook
453
     * object.
454
     * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
455
     * @see org.apache.poi.hssf.usermodel.HSSFCell#getCellStyle()
456
     */
457
    public HSSFCellStyle getRowStyle()
458
    {
459
        return style;
460
    }
427
461
428
    /**
462
    /**
429
     * @return cell iterator of the physically defined cells.  Note element 4 may
463
     * @return cell iterator of the physically defined cells.  Note element 4 may
(-)src/testcases/org/apache/poi/hssf/usermodel/TestRowStyle.java (+217 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.FileInputStream;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
29
30
import junit.framework.TestCase;
31
32
import org.apache.poi.util.TempFile;
33
34
/**
35
 * Class to test row styling functionality
36
 *
37
 * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
38
 */
39
40
public class TestRowStyle
41
    extends TestCase
42
{
43
44
    /** Creates a new instance of TestCellStyle */
45
46
    public TestRowStyle(String name)
47
    {
48
        super(name);
49
    }
50
51
    /**
52
     * TEST NAME:  Test Write Sheet Font <P>
53
     * OBJECTIVE:  Test that HSSF can create a simple spreadsheet with numeric and string values and styled with fonts.<P>
54
     * SUCCESS:    HSSF creates a sheet.  Filesize matches a known good.  HSSFSheet objects
55
     *             Last row, first row is tested against the correct values (99,0).<P>
56
     * FAILURE:    HSSF does not create a sheet or excepts.  Filesize does not match the known good.
57
     *             HSSFSheet last row or first row is incorrect.             <P>
58
     *
59
     */
60
61
    public void testWriteSheetFont()
62
        throws IOException
63
    {
64
        File             file = TempFile.createTempFile("testWriteSheetFont",
65
                                                    ".xls");
66
        FileOutputStream out  = new FileOutputStream("c:/temp/1.xls");
67
        HSSFWorkbook     wb   = new HSSFWorkbook();
68
        HSSFSheet        s    = wb.createSheet();
69
        HSSFRow          r    = null;
70
        HSSFCell         c    = null;
71
        HSSFFont         fnt  = wb.createFont();
72
        HSSFCellStyle    cs   = wb.createCellStyle();
73
74
        fnt.setColor(HSSFFont.COLOR_RED);
75
        fnt.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
76
        cs.setFont(fnt);
77
        for (short rownum = ( short ) 0; rownum < 100; rownum++)
78
        {
79
            r = s.createRow(rownum);
80
            r.setRowStyle(cs);
81
            r.createCell((short) 0);
82
        }
83
        wb.write(out);
84
        out.close();
85
        SanityChecker sanityChecker = new SanityChecker();
86
        sanityChecker.checkHSSFWorkbook(wb);
87
        assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
88
        assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
89
    }
90
91
    /**
92
     * Tests that is creating a file with a date or an calendar works correctly.
93
     */
94
    public void testDataStyle()
95
            throws Exception
96
    {
97
        File             file = TempFile.createTempFile("testWriteSheetStyleDate",
98
                                                    ".xls");
99
        FileOutputStream out  = new FileOutputStream(file);
100
        HSSFWorkbook     wb   = new HSSFWorkbook();
101
        HSSFSheet        s    = wb.createSheet();
102
        HSSFCellStyle    cs   = wb.createCellStyle();
103
        HSSFRow row = s.createRow((short)0);
104
105
        // with Date:
106
        cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
107
        row.setRowStyle(cs);
108
        row.createCell((short) 0);
109
110
111
        // with Calendar:
112
        row = s.createRow((short)1);
113
        cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
114
        row.setRowStyle(cs);
115
        row.createCell((short) 0);
116
117
        wb.write(out);
118
        out.close();
119
        SanityChecker sanityChecker = new SanityChecker();
120
        sanityChecker.checkHSSFWorkbook(wb);
121
122
        assertEquals("LAST ROW ", 1, s.getLastRowNum());
123
        assertEquals("FIRST ROW ", 0, s.getFirstRowNum());
124
125
    }
126
127
    /**
128
     * TEST NAME:  Test Write Sheet Style <P>
129
     * OBJECTIVE:  Test that HSSF can create a simple spreadsheet with numeric and string values and styled with colors
130
     *             and borders.<P>
131
     * SUCCESS:    HSSF creates a sheet.  Filesize matches a known good.  HSSFSheet objects
132
     *             Last row, first row is tested against the correct values (99,0).<P>
133
     * FAILURE:    HSSF does not create a sheet or excepts.  Filesize does not match the known good.
134
     *             HSSFSheet last row or first row is incorrect.             <P>
135
     *
136
     */
137
138
    public void testWriteSheetStyle()
139
        throws IOException
140
    {
141
        File             file = TempFile.createTempFile("testWriteSheetStyle",
142
                                                    ".xls");
143
        FileOutputStream out  = new FileOutputStream(file);
144
        HSSFWorkbook     wb   = new HSSFWorkbook();
145
        HSSFSheet        s    = wb.createSheet();
146
        HSSFRow          r    = null;
147
        HSSFFont         fnt  = wb.createFont();
148
        HSSFCellStyle    cs   = wb.createCellStyle();
149
        HSSFCellStyle    cs2  = wb.createCellStyle();
150
151
        cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);
152
        cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
153
        cs.setBorderRight(HSSFCellStyle.BORDER_THIN);
154
        cs.setBorderTop(HSSFCellStyle.BORDER_THIN);
155
        cs.setFillForegroundColor(( short ) 0xA);
156
        cs.setFillPattern(( short ) 1);
157
        fnt.setColor(( short ) 0xf);
158
        fnt.setItalic(true);
159
        cs2.setFillForegroundColor(( short ) 0x0);
160
        cs2.setFillPattern(( short ) 1);
161
        cs2.setFont(fnt);
162
        for (short rownum = ( short ) 0; rownum < 100; rownum++)
163
        {
164
            r = s.createRow(rownum);
165
            r.setRowStyle(cs);
166
            r.createCell((short) 0);
167
            
168
            rownum++;
169
            if (rownum >= 100) break; // I feel too lazy to check if this isreqd :-/ 
170
            
171
            r = s.createRow(rownum);
172
            r.setRowStyle(cs2);
173
            r.createCell((short) 0);
174
        }
175
        wb.write(out);
176
        out.close();
177
        SanityChecker sanityChecker = new SanityChecker();
178
        sanityChecker.checkHSSFWorkbook(wb);
179
        assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
180
        assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
181
        
182
        FileInputStream fis = new FileInputStream(file);
183
        wb = new HSSFWorkbook(fis);
184
        s    = wb.getSheetAt(0);
185
        assertNotNull("Sheet is not null", s);
186
        
187
        for (short rownum = ( short ) 0; rownum < 100; rownum++)
188
        {
189
            r = s.getRow(rownum);
190
            assertNotNull("Row is not null", r);
191
            
192
            cs = r.getRowStyle();
193
            assertEquals("FillForegroundColor for row: ", cs.getBorderBottom(), HSSFCellStyle.BORDER_THIN);
194
            assertEquals("FillPattern for row: ", cs.getBorderLeft(), HSSFCellStyle.BORDER_THIN);
195
            assertEquals("FillForegroundColor for row: ", cs.getBorderRight(), HSSFCellStyle.BORDER_THIN);
196
            assertEquals("FillPattern for row: ", cs.getBorderTop(), HSSFCellStyle.BORDER_THIN);
197
            assertEquals("FillForegroundColor for row: ", cs.getFillForegroundColor(), 0xA);
198
            assertEquals("FillPattern for row: ", cs.getFillPattern(), (short) 0x1);
199
            
200
            rownum++;
201
            if (rownum >= 100) break; // I feel too lazy to check if this isreqd :-/ 
202
            
203
            r = s.getRow(rownum);
204
            assertNotNull("Row is not null", r);
205
            cs2 = r.getRowStyle();
206
            assertEquals("FillForegroundColor for row: ", cs2.getFillForegroundColor(), (short) 0x0);
207
            assertEquals("FillPattern for row: ", cs2.getFillPattern(), (short) 0x1);
208
        }
209
    }
210
211
    public static void main(String [] ignored_args)
212
    {
213
        System.out
214
            .println("Testing org.apache.poi.hssf.usermodel.HSSFCellStyle");
215
        junit.textui.TestRunner.run(TestCellStyle.class);
216
    }
217
}

Return to bug 16936