From ad050421733f4d4d69c6af7cb7abf12e65cfed97 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 21 Mar 2008 21:04:47 +0000 Subject: [PATCH] Tweak how you get dataformat strings out of cell styles, to be more logical, and in keeping with how we'll want to do things for xssf too git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@639836 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFCell.java | 4 +-- .../poi/hssf/usermodel/HSSFCellStyle.java | 25 +++++++++++++++---- .../poi/hssf/usermodel/HSSFWorkbook.java | 4 +-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index cb2a76848a..670b33372d 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -211,7 +211,7 @@ public class HSSFCell } ExtendedFormatRecord xf = book.getExFormatAt(cval.getXFIndex()); - setCellStyle(new HSSFCellStyle(( short ) cval.getXFIndex(), xf)); + setCellStyle(new HSSFCellStyle(( short ) cval.getXFIndex(), xf, book)); } /** @@ -914,7 +914,7 @@ public class HSSFCell { short styleIndex=record.getXFIndex(); ExtendedFormatRecord xf = book.getExFormatAt(styleIndex); - return new HSSFCellStyle(styleIndex, xf); + return new HSSFCellStyle(styleIndex, xf, book); } /** diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index 3c9b6b1bb5..f0fb6ebfb3 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -20,7 +20,7 @@ package org.apache.poi.hssf.usermodel; import org.apache.poi.hssf.model.Workbook; import org.apache.poi.hssf.record.ExtendedFormatRecord; -import org.apache.poi.hssf.util.*; +import org.apache.poi.hssf.util.HSSFColor; /** * High level representation of the style of a cell in a sheet of a workbook. @@ -38,6 +38,7 @@ public class HSSFCellStyle { private ExtendedFormatRecord format = null; private short index = 0; + private Workbook workbook = null; /** * general (normal) horizontal alignment @@ -230,9 +231,13 @@ public class HSSFCellStyle /** Creates new HSSFCellStyle why would you want to do this?? */ - - protected HSSFCellStyle(short index, ExtendedFormatRecord rec) + protected HSSFCellStyle(short index, ExtendedFormatRecord rec, HSSFWorkbook workbook) + { + this(index, rec, workbook.getWorkbook()); + } + protected HSSFCellStyle(short index, ExtendedFormatRecord rec, Workbook workbook) { + this.workbook = workbook; this.index = index; format = rec; } @@ -268,6 +273,16 @@ public class HSSFCellStyle return format.getFormatIndex(); } + /** + * Get the contents of the format string, by looking up + * the DataFormat against the bound workbook + * @see org.apache.poi.hssf.usermodel.HSSFDataFormat + */ + public String getDataFormatString() { + HSSFDataFormat format = new HSSFDataFormat(workbook); + + return format.getFormat(getDataFormat()); + } /** * Get the contents of the format string, by looking up * the DataFormat against the supplied workbook @@ -289,7 +304,7 @@ public class HSSFCellStyle public void setFont(HSSFFont font) { format.setIndentNotParentFont(true); - short fontindex = font.getIndex(); + short fontindex = ((HSSFFont) font).getIndex(); format.setFontIndex(fontindex); } @@ -309,7 +324,7 @@ public class HSSFCellStyle * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) */ public HSSFFont getFont(HSSFWorkbook parentWorkbook) { - return parentWorkbook.getFontAt(getFontIndex()); + return ((HSSFWorkbook) parentWorkbook).getFontAt(getFontIndex()); } /** diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 1e89d6bebd..3838e634d9 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -903,7 +903,7 @@ public class HSSFWorkbook extends POIDocument { ExtendedFormatRecord xfr = workbook.createCellXF(); short index = (short) (getNumCellStyles() - 1); - HSSFCellStyle style = new HSSFCellStyle(index, xfr); + HSSFCellStyle style = new HSSFCellStyle(index, xfr, this); return style; } @@ -927,7 +927,7 @@ public class HSSFWorkbook extends POIDocument public HSSFCellStyle getCellStyleAt(short idx) { ExtendedFormatRecord xfr = workbook.getExFormatAt(idx); - HSSFCellStyle style = new HSSFCellStyle(idx, xfr); + HSSFCellStyle style = new HSSFCellStyle(idx, xfr, this); return style; } -- 2.39.5