📄 xfrecord.java
字号:
/*********************************************************************** Copyright (C) 2002 Andrew Khan** This library is free software; you can redistribute it and/or* modify it under the terms of the GNU Lesser General Public* License as published by the Free Software Foundation; either* version 2.1 of the License, or (at your option) any later version.** This library is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU* Lesser General Public License for more details.** You should have received a copy of the GNU Lesser General Public* License along with this library; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA***************************************************************************/package jxl.biff;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.text.NumberFormat;import java.text.DecimalFormat;import common.Assert;import common.Logger;import jxl.format.CellFormat;import jxl.format.Format;import jxl.format.Font;import jxl.format.Alignment;import jxl.format.VerticalAlignment;import jxl.format.BorderLineStyle;import jxl.format.Border;import jxl.format.Colour;import jxl.format.Pattern;import jxl.format.Orientation;import jxl.read.biff.Record;/** * Holds an extended formatting record */public class XFRecord extends WritableRecordData implements CellFormat{ /** * The logger */ private static Logger logger = Logger.getLogger(XFRecord.class); /** * The index to the format record */ private int formatIndex; /** * The index of the parent format */ private int parentFormat; /** * The format type */ private XFType xfFormatType; /** * Indicates whether this is a date formatting record */ private boolean date; /** * Indicates whether this is a number formatting record */ private boolean number; /** * The date format for this record. Deduced when the record is * read in from a spreadsheet */ private DateFormat dateFormat; /** * The number format for this record. Deduced when the record is read in * from a spreadsheet */ private NumberFormat numberFormat; /** * The used attribute. Needs to be preserved in order to get accurate * rationalization */ private byte usedAttributes; /** * The index to the font record used by this XF record */ private int fontIndex; /** * Flag to indicate whether this XF record represents a locked cell */ private boolean locked; /** * Flag to indicate whether this XF record is hidden */ private boolean hidden; /** * The alignment for this cell (left, right, centre) */ private Alignment align; /** * The vertical alignment for the cell (top, bottom, centre) */ private VerticalAlignment valign; /** * The orientation of the cell */ private Orientation orientation; /** * Flag to indicates whether the data (normally text) in the cell will be * wrapped around to fit in the cell width */ private boolean wrap; /** * Indentation of the cell text */ private int indentation; /** * Flag to indicate that this format is shrink to fit */ private boolean shrinkToFit; /** * The border indicator for the left of this cell */ private BorderLineStyle leftBorder; /** * The border indicator for the right of the cell */ private BorderLineStyle rightBorder; /** * The border indicator for the top of the cell */ private BorderLineStyle topBorder; /** * The border indicator for the bottom of the cell */ private BorderLineStyle bottomBorder; /** * The border colour for the left of the cell */ private Colour leftBorderColour; /** * The border colour for the right of the cell */ private Colour rightBorderColour; /** * The border colour for the top of the cell */ private Colour topBorderColour; /** * The border colour for the bottom of the cell */ private Colour bottomBorderColour; /** * The background colour */ private Colour backgroundColour; /** * The background pattern */ private Pattern pattern; /** * The options mask which is used to store the processed cell options (such * as alignment, borders etc) */ private int options; /** * The index of this XF record within the workbook */ private int xfIndex; /** * The font object for this XF record */ private FontRecord font; /** * The format object for this XF record. This is used when creating * a writable record */ private DisplayFormat format; /** * Flag to indicate whether this XF record has been initialized */ private boolean initialized; /** * Indicates whether this cell was constructed by an API or read * from an existing Excel file */ private boolean read; /** * The excel format for this record. This is used to display the actual * excel format string back to the user (eg. when generating certain * types of XML document) as opposed to the java equivalent */ private Format excelFormat; /** * Flag to indicate whether the format information has been initialized. * This is false if the xf record has been read in, but true if it * has been written */ private boolean formatInfoInitialized; /** * Flag to indicate whether this cell was copied. If it was copied, then * it can be set to uninitialized, allowing us to change certain format * information */ private boolean copied; /** * A handle to the formatting records. The purpose of this is * to read the formatting information back, for the purposes of * output eg. to some form of XML */ private FormattingRecords formattingRecords; /** * The list of built in date format values */ private static final int[] dateFormats = new int[] {0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x2d, 0x2e, 0x2f}; /** * The list of java date format equivalents */ private static final DateFormat[] javaDateFormats = new DateFormat[] {new SimpleDateFormat("dd/MM/yyyy"), new SimpleDateFormat("d-MMM-yy"), new SimpleDateFormat("d-MMM"), new SimpleDateFormat("MMM-yy"), new SimpleDateFormat("h:mm a"), new SimpleDateFormat("h:mm:ss a"), new SimpleDateFormat("H:mm"), new SimpleDateFormat("H:mm:ss"), new SimpleDateFormat("M/d/yy H:mm"), new SimpleDateFormat("mm:ss"), new SimpleDateFormat("H:mm:ss"), new SimpleDateFormat("mm:ss.S")}; /** * The list of built in number format values */ private static int[] numberFormats = new int[] {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x30}; /** * The list of java number format equivalents */ private static NumberFormat[] javaNumberFormats = new NumberFormat[] {new DecimalFormat("0"), new DecimalFormat("0.00"), new DecimalFormat("#,##0"), new DecimalFormat("#,##0.00"), new DecimalFormat("$#,##0;($#,##0)"), new DecimalFormat("$#,##0;($#,##0)"), new DecimalFormat("$#,##0.00;($#,##0.00)"), new DecimalFormat("$#,##0.00;($#,##0.00)"), new DecimalFormat("0%"), new DecimalFormat("0.00%"), new DecimalFormat("0.00E00"), new DecimalFormat("#,##0;(#,##0)"), new DecimalFormat("#,##0;(#,##0)"), new DecimalFormat("#,##0.00;(#,##0.00)"), new DecimalFormat("#,##0.00;(#,##0.00)"), new DecimalFormat("#,##0;(#,##0)"), new DecimalFormat("$#,##0;($#,##0)"), new DecimalFormat("#,##0.00;(#,##0.00)"), new DecimalFormat("$#,##0.00;($#,##0.00)"), new DecimalFormat("##0.0E0")}; // Type to distinguish between biff7 and biff8 private static class BiffType {}; public static final BiffType biff8 = new BiffType(); public static final BiffType biff7 = new BiffType(); /** * The biff type */ private BiffType biffType; // Type to distinguish between cell and style records private static class XFType { } protected static final XFType cell = new XFType(); protected static final XFType style = new XFType(); /** * Constructs this object from the raw data * * @param t the raw data * @param bt the biff type */ public XFRecord(Record t, BiffType bt) { super(t); biffType = bt; byte[] data = getRecord().getData(); fontIndex = IntegerHelper.getInt(data[0], data[1]); formatIndex = IntegerHelper.getInt(data[2], data[3]); date = false; number = false; // Compare against the date formats for (int i = 0; i < dateFormats.length && date == false; i++) { if (formatIndex == dateFormats[i]) { date = true; dateFormat = javaDateFormats[i]; } } // Compare against the number formats for (int i = 0; i < numberFormats.length && number == false; i++) { if (formatIndex == numberFormats[i]) { number = true; numberFormat = javaNumberFormats[i]; } } // Initialize the parent format and the type int cellAttributes = IntegerHelper.getInt(data[4], data[5]); parentFormat = (cellAttributes & 0xfff0) >> 4; int formatType = cellAttributes & 0x4; xfFormatType = formatType == 0 ? cell : style; locked = ((cellAttributes & 0x1) != 0); hidden = ((cellAttributes & 0x2) != 0); if (xfFormatType == cell && (parentFormat & 0xfff) == 0xfff) { // Something is screwy with the parent format - set to zero parentFormat = 0; logger.warn("Invalid parent format found - ignoring"); } initialized = false; read = true; formatInfoInitialized = false; copied = false; } /** * A constructor used when creating a writable record * * @param fnt the font * @param form the format */ public XFRecord(FontRecord fnt, DisplayFormat form) { super(Type.XF); initialized = false; locked = true; hidden = false; align = Alignment.GENERAL; valign = VerticalAlignment.BOTTOM; orientation = Orientation.HORIZONTAL; wrap = false; leftBorder = BorderLineStyle.NONE; rightBorder = BorderLineStyle.NONE; topBorder = BorderLineStyle.NONE; bottomBorder = BorderLineStyle.NONE; leftBorderColour = Colour.AUTOMATIC; rightBorderColour = Colour.AUTOMATIC; topBorderColour = Colour.AUTOMATIC; bottomBorderColour = Colour.AUTOMATIC; pattern = Pattern.NONE; backgroundColour = Colour.DEFAULT_BACKGROUND; indentation = 0; shrinkToFit = false; // This will be set by the initialize method and the subclass respectively parentFormat = 0; xfFormatType = null; font = fnt; format = form; biffType = biff8; read = false; copied = false; formatInfoInitialized = true; Assert.verify(font != null); Assert.verify(format != null); } /** * Copy constructor. Used for copying writable formats, typically * when duplicating formats to handle merged cells * * @param fmt XFRecord */ protected XFRecord(XFRecord fmt) { super(Type.XF); initialized = false; locked = fmt.locked; hidden = fmt.hidden; align = fmt.align; valign = fmt.valign; orientation = fmt.orientation; wrap = fmt.wrap; leftBorder = fmt.leftBorder; rightBorder = fmt.rightBorder; topBorder = fmt.topBorder; bottomBorder = fmt.bottomBorder; leftBorderColour = fmt.leftBorderColour; rightBorderColour = fmt.rightBorderColour; topBorderColour = fmt.topBorderColour; bottomBorderColour = fmt.bottomBorderColour; pattern = fmt.pattern; xfFormatType = fmt.xfFormatType; indentation = fmt.indentation; shrinkToFit = fmt.shrinkToFit; parentFormat = fmt.parentFormat; backgroundColour = fmt.backgroundColour; // Shallow copy is sufficient for these purposes font = fmt.font; format = fmt.format; fontIndex = fmt.fontIndex; formatIndex = fmt.formatIndex; formatInfoInitialized = fmt.formatInfoInitialized; biffType = biff8; read = false; copied = true; } /** * A public copy constructor which can be used for copy formats between * different sheets. Unlike the the other copy constructor, this * version does a deep copy * * @param cellFormat the format to copy */ protected XFRecord(CellFormat cellFormat) { super(Type.XF); Assert.verify(cellFormat != null); Assert.verify(cellFormat instanceof XFRecord); XFRecord fmt = (XFRecord) cellFormat; if (!fmt.formatInfoInitialized) { fmt.initializeFormatInformation(); } locked = fmt.locked; hidden = fmt.hidden; align = fmt.align; valign = fmt.valign; orientation = fmt.orientation; wrap = fmt.wrap; leftBorder = fmt.leftBorder; rightBorder = fmt.rightBorder; topBorder = fmt.topBorder; bottomBorder = fmt.bottomBorder; leftBorderColour = fmt.leftBorderColour; rightBorderColour = fmt.rightBorderColour; topBorderColour = fmt.topBorderColour; bottomBorderColour = fmt.bottomBorderColour; pattern = fmt.pattern; xfFormatType = fmt.xfFormatType; parentFormat = fmt.parentFormat; indentation = fmt.indentation; shrinkToFit = fmt.shrinkToFit; backgroundColour = fmt.backgroundColour; // Deep copy of the font font = new FontRecord(fmt.getFont()); // Copy the format if (fmt.getFormat() == null) { // format is writable if (fmt.format.isBuiltIn()) { format = fmt.format; } else { // Format is not built in, so do a deep copy format = new FormatRecord((FormatRecord) fmt.format); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -