Browse Source

Merged revisions 697562,697580,697584,697589,697595,697599 via svnmerge from

https://svn.apache.org/repos/asf/poi/trunk

........
  r697562 | nick | 2008-09-21 18:49:20 +0100 (Sun, 21 Sep 2008) | 1 line
  
  Fix bug #45784 - Support long chart titles in SeriesTextRecords
........
  r697580 | nick | 2008-09-21 19:43:49 +0100 (Sun, 21 Sep 2008) | 1 line
  
  Include the sheet name in the output of examples.XLS2CSVmra
........
  r697584 | nick | 2008-09-21 19:56:32 +0100 (Sun, 21 Sep 2008) | 1 line
  
  Test to show that bug #45492 is invalid
........
  r697589 | nick | 2008-09-21 20:17:41 +0100 (Sun, 21 Sep 2008) | 1 line
  
  Update hssf.extractor.ExcelExtractor to optionally output blank cells too
........
  r697595 | nick | 2008-09-21 20:31:34 +0100 (Sun, 21 Sep 2008) | 1 line
  
  Improved tests to show that bugs #45062 and #44292 are fixed
........
  r697599 | nick | 2008-09-21 20:50:21 +0100 (Sun, 21 Sep 2008) | 1 line
  
  Apply part of patch from bug #16936, with the rest made more HSSFCell like - Initial support for whole-row cell styling
........


git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@698257 13f79535-47bb-0310-9956-ffa450edef68
tags/ooxml_20081107
Nick Burch 15 years ago
parent
commit
3901d88e01

+ 4
- 0
src/documentation/content/xdocs/changes.xml View File

@@ -67,6 +67,10 @@
<action dev="POI-DEVELOPERS" type="add">Created a common interface for handling Excel files, irrespective of if they are .xls or .xlsx</action>
</release>
<release version="3.2-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="add">16936 - Initial support for whole-row cell styling</action>
<action dev="POI-DEVELOPERS" type="add">Update hssf.extractor.ExcelExtractor to optionally output blank cells too</action>
<action dev="POI-DEVELOPERS" type="add">Include the sheet name in the output of examples.XLS2CSVmra</action>
<action dev="POI-DEVELOPERS" type="fix">45784 - Support long chart titles in SeriesTextRecords</action>
<action dev="POI-DEVELOPERS" type="fix">45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out</action>
<action dev="POI-DEVELOPERS" type="add">45844 - Addtional diagnostics for HSLF SlideShowRecordDumper</action>
<action dev="POI-DEVELOPERS" type="fix">45829 - HSSFPicture.getImageDimension() failed when DPI of image is zero</action>

+ 4
- 0
src/documentation/content/xdocs/status.xml View File

@@ -64,6 +64,10 @@
<action dev="POI-DEVELOPERS" type="add">Created a common interface for handling Excel files, irrespective of if they are .xls or .xlsx</action>
</release>
<release version="3.2-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="add">16936 - Initial support for whole-row cell styling</action>
<action dev="POI-DEVELOPERS" type="add">Update hssf.extractor.ExcelExtractor to optionally output blank cells too</action>
<action dev="POI-DEVELOPERS" type="add">Include the sheet name in the output of examples.XLS2CSVmra</action>
<action dev="POI-DEVELOPERS" type="fix">45784 - Support long chart titles in SeriesTextRecords</action>
<action dev="POI-DEVELOPERS" type="fix">45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out</action>
<action dev="POI-DEVELOPERS" type="add">45844 - Addtional diagnostics for HSLF SlideShowRecordDumper</action>
<action dev="POI-DEVELOPERS" type="fix">45829 - HSSFPicture.getImageDimension() failed when DPI of image is zero</action>

+ 21
- 0
src/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java View File

@@ -21,6 +21,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
@@ -34,6 +35,7 @@ import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.LabelRecord;
import org.apache.poi.hssf.record.LabelSSTRecord;
@@ -69,6 +71,11 @@ public class XLS2CSVmra implements HSSFListener {
// Records we pick up as we process
private SSTRecord sstRecord;
private FormatTrackingHSSFListener formatListener;
/** So we known which sheet we're on */
private int sheetIndex = -1;
private BoundSheetRecord[] orderedBSRs;
private ArrayList boundSheetRecords = new ArrayList();

// For handling formulas with string results
private int nextRow;
@@ -132,6 +139,9 @@ public class XLS2CSVmra implements HSSFListener {

switch (record.getSid())
{
case BoundSheetRecord.sid:
boundSheetRecords.add(record);
break;
case BOFRecord.sid:
BOFRecord br = (BOFRecord)record;
if(br.getType() == BOFRecord.TYPE_WORKSHEET) {
@@ -139,6 +149,17 @@ public class XLS2CSVmra implements HSSFListener {
if(workbookBuildingListener != null && stubWorkbook == null) {
stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
}
// Output the worksheet name
// Works by ordering the BSRs by the location of
// their BOFRecords, and then knowing that we
// process BOFRecords in byte offset order
sheetIndex++;
if(orderedBSRs == null) {
orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
}
output.println();
output.println(orderedBSRs[sheetIndex].getSheetname() + ":");
}
break;


+ 72
- 53
src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java View File

@@ -45,6 +45,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
private boolean includeSheetNames = true;
private boolean formulasNotResults = false;
private boolean includeCellComments = false;
private boolean includeBlankCells = false;
public ExcelExtractor(HSSFWorkbook wb) {
super(wb);
@@ -77,13 +78,26 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
public void setIncludeCellComments(boolean includeCellComments) {
this.includeCellComments = includeCellComments;
}
/**
* Should blank cells be output? Default is to only
* output cells that are present in the file and are
* non-blank.
*/
public void setIncludeBlankCells(boolean includeBlankCells) {
this.includeBlankCells = includeBlankCells;
}
/**
* Retreives the text contents of the file
*/
public String getText() {
StringBuffer text = new StringBuffer();

// We don't care about the differnce between
// null (missing) and blank cells
wb.setMissingCellPolicy(HSSFRow.RETURN_BLANK_AS_NULL);
// Process each sheet in turn
for(int i=0;i<wb.getNumberOfSheets();i++) {
HSSFSheet sheet = wb.getSheetAt(i);
if(sheet == null) { continue; }
@@ -112,63 +126,68 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
// Check each cell in turn
int firstCell = row.getFirstCellNum();
int lastCell = row.getLastCellNum();
if(includeBlankCells) {
firstCell = 0;
}
for(int k=firstCell;k<lastCell;k++) {
HSSFCell cell = row.getCell(k);
if(cell == null) { continue; }
boolean outputContents = true;
switch(cell.getCellType()) {
case HSSFCell.CELL_TYPE_BLANK:
outputContents = false;
break;
case HSSFCell.CELL_TYPE_STRING:
text.append(cell.getRichStringCellValue().getString());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
// Note - we don't apply any formatting!
text.append(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
text.append(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
text.append(ErrorEval.getText(cell.getErrorCellValue()));
break;
case HSSFCell.CELL_TYPE_FORMULA:
if(formulasNotResults) {
text.append(cell.getCellFormula());
} else {
switch(cell.getCachedFormulaResultType()) {
case HSSFCell.CELL_TYPE_STRING:
HSSFRichTextString str = cell.getRichStringCellValue();
if(str != null && str.length() > 0) {
text.append(str.toString());
}
break;
case HSSFCell.CELL_TYPE_NUMERIC:
text.append(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
text.append(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
text.append(ErrorEval.getText(cell.getErrorCellValue()));
break;

if(cell == null) {
// Only output if requested
outputContents = includeBlankCells;
} else {
switch(cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
text.append(cell.getRichStringCellValue().getString());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
// Note - we don't apply any formatting!
text.append(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
text.append(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
text.append(ErrorEval.getText(cell.getErrorCellValue()));
break;
case HSSFCell.CELL_TYPE_FORMULA:
if(formulasNotResults) {
text.append(cell.getCellFormula());
} else {
switch(cell.getCachedFormulaResultType()) {
case HSSFCell.CELL_TYPE_STRING:
HSSFRichTextString str = cell.getRichStringCellValue();
if(str != null && str.length() > 0) {
text.append(str.toString());
}
break;
case HSSFCell.CELL_TYPE_NUMERIC:
text.append(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
text.append(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
text.append(ErrorEval.getText(cell.getErrorCellValue()));
break;
}
}
}
break;
default:
throw new RuntimeException("Unexpected cell type (" + cell.getCellType() + ")");
}
// Output the comment, if requested and exists
HSSFComment comment = cell.getCellComment();
if(includeCellComments && comment != null) {
// Replace any newlines with spaces, otherwise it
// breaks the output
String commentText = comment.getString().getString().replace('\n', ' ');
text.append(" Comment by "+comment.getAuthor()+": "+commentText);
break;
default:
throw new RuntimeException("Unexpected cell type (" + cell.getCellType() + ")");
}
// Output the comment, if requested and exists
HSSFComment comment = cell.getCellComment();
if(includeCellComments && comment != null) {
// Replace any newlines with spaces, otherwise it
// breaks the output
String commentText = comment.getString().getString().replace('\n', ' ');
text.append(" Comment by "+comment.getAuthor()+": "+commentText);
}
}
// Output a tab if we're not on the last cell

+ 31
- 0
src/java/org/apache/poi/hssf/record/BoundSheetRecord.java View File

@@ -17,6 +17,10 @@

package org.apache.poi.hssf.record;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import org.apache.poi.util.BitField;
import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.LittleEndian;
@@ -329,4 +333,31 @@ public final class BoundSheetRecord extends Record {
public void setVeryHidden(boolean veryHidden) {
field_2_option_flags = veryHiddenFlag.setShortBoolean(field_2_option_flags, veryHidden);
}
/**
* Takes a list of BoundSheetRecords, and returns the all
* ordered by the position of their BOFs.
*/
public static BoundSheetRecord[] orderByBofPosition(List boundSheetRecords) {
BoundSheetRecord[] bsrs = (BoundSheetRecord[])boundSheetRecords.toArray(
new BoundSheetRecord[boundSheetRecords.size()]);
// Sort
Arrays.sort(bsrs, new BOFComparator());
// All done
return bsrs;
}
private static class BOFComparator implements Comparator {
public int compare(Object bsr1, Object bsr2) {
return compare((BoundSheetRecord)bsr1, (BoundSheetRecord)bsr2);
}
public int compare(BoundSheetRecord bsr1, BoundSheetRecord bsr2) {
if(bsr1.field_1_position_of_BOF < bsr2.field_1_position_of_BOF)
return -1;
if(bsr1.field_1_position_of_BOF == bsr2.field_1_position_of_BOF)
return 0;
return 1;
}
}
}

+ 20
- 3
src/java/org/apache/poi/hssf/record/SeriesTextRecord.java View File

@@ -75,7 +75,8 @@ public class SeriesTextRecord
field_1_id = in.readShort();
field_2_textLength = in.readByte();
field_3_undocumented = in.readByte();
field_4_text = in.readUnicodeLEString(field_2_textLength);
field_4_text = in.readUnicodeLEString(
LittleEndian.ubyteToInt(field_2_textLength));
}

public String toString()
@@ -163,18 +164,34 @@ public class SeriesTextRecord
/**
* Get the text length field for the SeriesText record.
*/
public byte getTextLength()
public int getTextLength()
{
return field_2_textLength;
return LittleEndian.ubyteToInt(field_2_textLength);
}

/**
* Set the text length field for the SeriesText record.
* Needs to be wrapped.
*/
public void setTextLength(byte field_2_textLength)
{
this.field_2_textLength = field_2_textLength;
}
/**
* Set the text length field for the SeriesText record.
*/
public void setTextLength(int field_2_textLength)
{
if(field_2_textLength > 255) {
throw new IllegalArgumentException("Length must be 0-255");
}
if(field_2_textLength > 127) {
this.field_2_textLength = (byte)
(field_2_textLength-256);
} else {
this.field_2_textLength = (byte)field_2_textLength;
}
}

/**
* Get the undocumented field for the SeriesText record.

+ 6
- 2
src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java View File

@@ -926,7 +926,9 @@ public class HSSFCellStyle implements CellStyle
}

/**
* get the background fill color
* Get the background fill color.
* Note - many cells are actually filled with a foreground
* fill, not a background fill - see {@link #getFillForegroundColor()}
* @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
* @return fill color
*/
@@ -952,7 +954,9 @@ public class HSSFCellStyle implements CellStyle
}

/**
* get the foreground fill color
* Get the foreground fill color.
* Many cells are filled with this, instead of a
* background color ({@link #getFillBackgroundColor()})
* @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short)
* @return fill color
*/

+ 9
- 0
src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java View File

@@ -58,6 +58,15 @@ public class HSSFPalette implements Palette
}
return null;
}
/**
* Retrieves the color at a given index
*
* @param index the palette index, between 0x8 to 0x40 inclusive
* @return the color, or null if the index is not populated
*/
public HSSFColor getColor(int index) {
return getColor((short)index);
}
/**
* Finds the first occurance of a given color

+ 28
- 0
src/java/org/apache/poi/hssf/usermodel/HSSFRow.java View File

@@ -21,6 +21,7 @@ import java.util.Iterator;
import java.util.NoSuchElementException;

import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.RowRecord;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
@@ -525,6 +526,33 @@ public final class HSSFRow implements Comparable, Row {
return -1;
return cellnum;
}
/**
* Is this row formatted? Most aren't, but some rows
* do have whole-row styles. For those that do, you
* can get the formatting from {@link #getRowStyle()}
*/
public boolean isFormatted() {
return row.getFormatted();
}
/**
* Returns the whole-row cell styles. Most rows won't
* have one of these, so will return null. Call
* {@link #isFormatted()} to check first.
*/
public HSSFCellStyle getRowStyle() {
if(!isFormatted()) { return null; }
short styleIndex = row.getXFIndex();
ExtendedFormatRecord xf = book.getWorkbook().getExFormatAt(styleIndex);
return new HSSFCellStyle(styleIndex, xf, book);
}
/**
* Applies a whole-row cell styling to the row.
*/
public void setRowStyle(HSSFCellStyle style) {
row.setFormatted(true);
row.setXFIndex(style.getIndex());
}

/**
* @return cell iterator of the physically defined cells.

+ 7
- 1
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestProblems.java View File

@@ -74,7 +74,8 @@ public class TestProblems extends TestCase {
}

/**
* Test for TableCell not skipping the last paragraph
* Test for TableCell not skipping the last paragraph.
* Bugs #45062 and #44292
*/
public void testTableCellLastParagraph() throws Exception {
HWPFDocument doc = new HWPFDocument(new FileInputStream(
@@ -93,22 +94,27 @@ public class TestProblems extends TestCase {
Table t = r.getTable(p);
//get the only row
assertEquals(1, t.numRows());
TableRow row = t.getRow(0);
//get the first cell
TableCell cell = row.getCell(0);
// First cell should have one paragraph
assertEquals(1, cell.numParagraphs());
assertEquals("One paragraph is ok\7", cell.getParagraph(0).text());
//get the second
cell = row.getCell(1);
// Second cell should be detected as having two paragraphs
assertEquals(2, cell.numParagraphs());
assertEquals("First para is ok\r", cell.getParagraph(0).text());
assertEquals("Second paragraph is skipped\7", cell.getParagraph(1).text());
//get the last cell
cell = row.getCell(2);
// Last cell should have one paragraph
assertEquals(1, cell.numParagraphs());
assertEquals("One paragraph is ok\7", cell.getParagraph(0).text());
}

public void testRangeDelete() throws Exception {

BIN
src/testcases/org/apache/poi/hssf/data/45492.xls View File


BIN
src/testcases/org/apache/poi/hssf/data/45784.xls View File


+ 21
- 0
src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java View File

@@ -187,6 +187,27 @@ public final class TestExcelExtractor extends TestCase {
);
}
public void testWithBlank() throws Exception {
ExcelExtractor extractor = createExtractor("MissingBits.xls");
String def = extractor.getText();
extractor.setIncludeBlankCells(true);
String padded = extractor.getText();
assertTrue(def.startsWith(
"Sheet1\n" +
"&[TAB]\t\n" +
"Hello\t\n" +
"11.0\t23.0\t\n"
));
assertTrue(padded.startsWith(
"Sheet1\n" +
"&[TAB]\t\n" +
"Hello\t\t\t\t\t\t\t\t\t\t\t\n" +
"11.0\t\t\t23.0\t\t\t\t\t\t\t\t\n"
));
}
/**
* Embded in a non-excel file

+ 21
- 0
src/testcases/org/apache/poi/hssf/record/TestBOFRecord.java View File

@@ -18,6 +18,7 @@
package org.apache.poi.hssf.record;

import java.io.InputStream;
import java.util.ArrayList;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
@@ -38,4 +39,24 @@ public final class TestBOFRecord extends TestCase {
throw new AssertionFailedError("Identified bug 42794");
}
}
public void testOrdering() throws Exception {
BoundSheetRecord bs1 = new BoundSheetRecord();
BoundSheetRecord bs2 = new BoundSheetRecord();
BoundSheetRecord bs3 = new BoundSheetRecord();
bs1.setPositionOfBof(11);
bs2.setPositionOfBof(33);
bs3.setPositionOfBof(22);
ArrayList l = new ArrayList();
l.add(bs1);
l.add(bs2);
l.add(bs3);
BoundSheetRecord[] r = BoundSheetRecord.orderByBofPosition(l);
assertEquals(3, r.length);
assertEquals(bs1, r[0]);
assertEquals(bs3, r[1]);
assertEquals(bs2, r[2]);
}
}

+ 45
- 0
src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java View File

@@ -1476,4 +1476,49 @@ public final class TestBugs extends TestCase {
fail();
} catch(IllegalArgumentException e) {}
}
/**
* Charts with long titles
*/
public void test45784() {
// This used to break
HSSFWorkbook wb = openSample("45784.xls");
assertEquals(1, wb.getNumberOfSheets());
}
/**
* Cell background colours
*/
public void test45492() {
HSSFWorkbook wb = openSample("45492.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFRow r = s.getRow(0);
HSSFPalette p = wb.getCustomPalette();
HSSFCell auto = r.getCell(0);
HSSFCell grey = r.getCell(1);
HSSFCell red = r.getCell(2);
HSSFCell blue = r.getCell(3);
HSSFCell green = r.getCell(4);
assertEquals(64, auto.getCellStyle().getFillForegroundColor());
assertEquals(64, auto.getCellStyle().getFillBackgroundColor());
assertEquals("0:0:0", p.getColor(64).getHexString());
assertEquals(22, grey.getCellStyle().getFillForegroundColor());
assertEquals(64, grey.getCellStyle().getFillBackgroundColor());
assertEquals("C0C0:C0C0:C0C0", p.getColor(22).getHexString());
assertEquals(10, red.getCellStyle().getFillForegroundColor());
assertEquals(64, red.getCellStyle().getFillBackgroundColor());
assertEquals("FFFF:0:0", p.getColor(10).getHexString());
assertEquals(12, blue.getCellStyle().getFillForegroundColor());
assertEquals(64, blue.getCellStyle().getFillBackgroundColor());
assertEquals("0:0:FFFF", p.getColor(12).getHexString());
assertEquals(11, green.getCellStyle().getFillForegroundColor());
assertEquals(64, green.getCellStyle().getFillBackgroundColor());
assertEquals("0:FFFF:0", p.getColor(11).getHexString());
}
}

+ 204
- 0
src/testcases/org/apache/poi/hssf/usermodel/TestRowStyle.java View File

@@ -0,0 +1,204 @@
/* ====================================================================
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.IOException;

import junit.framework.TestCase;

import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.util.TempFile;

/**
* Class to test row styling functionality
*
* @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
*/

public class TestRowStyle
extends TestCase
{

/** Creates a new instance of TestCellStyle */

public TestRowStyle(String name)
{
super(name);
}

/**
* TEST NAME: Test Write Sheet Font <P>
* OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with fonts.<P>
* SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
* Last row, first row is tested against the correct values (99,0).<P>
* FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
* HSSFSheet last row or first row is incorrect. <P>
*
*/

public void testWriteSheetFont()
throws IOException
{
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(0);
}
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
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
{
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(0);


// with Calendar:
row = s.createRow((short)1);
cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
row.setRowStyle(cs);
row.createCell(0);

wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
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 <P>
* OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with colors
* and borders.<P>
* SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
* Last row, first row is tested against the correct values (99,0).<P>
* FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
* HSSFSheet last row or first row is incorrect. <P>
*
*/

public void testWriteSheetStyle()
throws IOException
{
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(0);

rownum++;
if (rownum >= 100) break; // I feel too lazy to check if this isreqd :-/
r = s.createRow(rownum);
r.setRowStyle(cs2);
r.createCell(0);
}
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
SanityChecker sanityChecker = new SanityChecker();
sanityChecker.checkHSSFWorkbook(wb);
assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
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);
}
}

Loading…
Cancel
Save