hssfcellstyle.java
来自「EXCEL read and write」· Java 代码 · 共 1,068 行 · 第 1/2 页
JAVA
1,068 行
public void setIndention(short indent) { format.setIndent(indent); } /** * get the number of spaces to indent the text in the cell * @return indent - number of spaces */ public short getIndention() { return format.getIndent(); } /** * set the type of border to use for the left border of the cell * @param border type * @see #BORDER_NONE * @see #BORDER_THIN * @see #BORDER_MEDIUM * @see #BORDER_DASHED * @see #BORDER_DOTTED * @see #BORDER_THICK * @see #BORDER_DOUBLE * @see #BORDER_HAIR * @see #BORDER_MEDIUM_DASHED * @see #BORDER_DASH_DOT * @see #BORDER_MEDIUM_DASH_DOT * @see #BORDER_DASH_DOT_DOT * @see #BORDER_MEDIUM_DASH_DOT_DOT * @see #BORDER_SLANTED_DASH_DOT */ public void setBorderLeft(short border) { format.setIndentNotParentBorder(true); format.setBorderLeft(border); } /** * get the type of border to use for the left border of the cell * @return border type * @see #BORDER_NONE * @see #BORDER_THIN * @see #BORDER_MEDIUM * @see #BORDER_DASHED * @see #BORDER_DOTTED * @see #BORDER_THICK * @see #BORDER_DOUBLE * @see #BORDER_HAIR * @see #BORDER_MEDIUM_DASHED * @see #BORDER_DASH_DOT * @see #BORDER_MEDIUM_DASH_DOT * @see #BORDER_DASH_DOT_DOT * @see #BORDER_MEDIUM_DASH_DOT_DOT * @see #BORDER_SLANTED_DASH_DOT */ public short getBorderLeft() { return format.getBorderLeft(); } /** * set the type of border to use for the right border of the cell * @param border type * @see #BORDER_NONE * @see #BORDER_THIN * @see #BORDER_MEDIUM * @see #BORDER_DASHED * @see #BORDER_DOTTED * @see #BORDER_THICK * @see #BORDER_DOUBLE * @see #BORDER_HAIR * @see #BORDER_MEDIUM_DASHED * @see #BORDER_DASH_DOT * @see #BORDER_MEDIUM_DASH_DOT * @see #BORDER_DASH_DOT_DOT * @see #BORDER_MEDIUM_DASH_DOT_DOT * @see #BORDER_SLANTED_DASH_DOT */ public void setBorderRight(short border) { format.setIndentNotParentBorder(true); format.setBorderRight(border); } /** * get the type of border to use for the right border of the cell * @return border type * @see #BORDER_NONE * @see #BORDER_THIN * @see #BORDER_MEDIUM * @see #BORDER_DASHED * @see #BORDER_DOTTED * @see #BORDER_THICK * @see #BORDER_DOUBLE * @see #BORDER_HAIR * @see #BORDER_MEDIUM_DASHED * @see #BORDER_DASH_DOT * @see #BORDER_MEDIUM_DASH_DOT * @see #BORDER_DASH_DOT_DOT * @see #BORDER_MEDIUM_DASH_DOT_DOT * @see #BORDER_SLANTED_DASH_DOT */ public short getBorderRight() { return format.getBorderRight(); } /** * set the type of border to use for the top border of the cell * @param border type * @see #BORDER_NONE * @see #BORDER_THIN * @see #BORDER_MEDIUM * @see #BORDER_DASHED * @see #BORDER_DOTTED * @see #BORDER_THICK * @see #BORDER_DOUBLE * @see #BORDER_HAIR * @see #BORDER_MEDIUM_DASHED * @see #BORDER_DASH_DOT * @see #BORDER_MEDIUM_DASH_DOT * @see #BORDER_DASH_DOT_DOT * @see #BORDER_MEDIUM_DASH_DOT_DOT * @see #BORDER_SLANTED_DASH_DOT */ public void setBorderTop(short border) { format.setIndentNotParentBorder(true); format.setBorderTop(border); } /** * get the type of border to use for the top border of the cell * @return border type * @see #BORDER_NONE * @see #BORDER_THIN * @see #BORDER_MEDIUM * @see #BORDER_DASHED * @see #BORDER_DOTTED * @see #BORDER_THICK * @see #BORDER_DOUBLE * @see #BORDER_HAIR * @see #BORDER_MEDIUM_DASHED * @see #BORDER_DASH_DOT * @see #BORDER_MEDIUM_DASH_DOT * @see #BORDER_DASH_DOT_DOT * @see #BORDER_MEDIUM_DASH_DOT_DOT * @see #BORDER_SLANTED_DASH_DOT */ public short getBorderTop() { return format.getBorderTop(); } /** * set the type of border to use for the bottom border of the cell * @param border type * @see #BORDER_NONE * @see #BORDER_THIN * @see #BORDER_MEDIUM * @see #BORDER_DASHED * @see #BORDER_DOTTED * @see #BORDER_THICK * @see #BORDER_DOUBLE * @see #BORDER_HAIR * @see #BORDER_MEDIUM_DASHED * @see #BORDER_DASH_DOT * @see #BORDER_MEDIUM_DASH_DOT * @see #BORDER_DASH_DOT_DOT * @see #BORDER_MEDIUM_DASH_DOT_DOT * @see #BORDER_SLANTED_DASH_DOT */ public void setBorderBottom(short border) { format.setIndentNotParentBorder(true); format.setBorderBottom(border); } /** * get the type of border to use for the bottom border of the cell * @return border type * @see #BORDER_NONE * @see #BORDER_THIN * @see #BORDER_MEDIUM * @see #BORDER_DASHED * @see #BORDER_DOTTED * @see #BORDER_THICK * @see #BORDER_DOUBLE * @see #BORDER_HAIR * @see #BORDER_MEDIUM_DASHED * @see #BORDER_DASH_DOT * @see #BORDER_MEDIUM_DASH_DOT * @see #BORDER_DASH_DOT_DOT * @see #BORDER_MEDIUM_DASH_DOT_DOT * @see #BORDER_SLANTED_DASH_DOT */ public short getBorderBottom() { return format.getBorderBottom(); } /** * set the color to use for the left border * @param color The index of the color definition */ public void setLeftBorderColor(short color) { format.setLeftBorderPaletteIdx(color); } /** * get the color to use for the left border * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short) * @param color The index of the color definition */ public short getLeftBorderColor() { return format.getLeftBorderPaletteIdx(); } /** * set the color to use for the right border * @param color The index of the color definition */ public void setRightBorderColor(short color) { format.setRightBorderPaletteIdx(color); } /** * get the color to use for the left border * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short) * @param color The index of the color definition */ public short getRightBorderColor() { return format.getRightBorderPaletteIdx(); } /** * set the color to use for the top border * @param color The index of the color definition */ public void setTopBorderColor(short color) { format.setTopBorderPaletteIdx(color); } /** * get the color to use for the top border * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short) * @param color The index of the color definition */ public short getTopBorderColor() { return format.getTopBorderPaletteIdx(); } /** * set the color to use for the bottom border * @param color The index of the color definition */ public void setBottomBorderColor(short color) { format.setBottomBorderPaletteIdx(color); } /** * get the color to use for the left border * @see org.apache.poi.hssf.usermodel.HSSFPalette#getColor(short) * @param color The index of the color definition */ public short getBottomBorderColor() { return format.getBottomBorderPaletteIdx(); } /** * setting to one fills the cell with the foreground color... No idea about * other values * * @see #NO_FILL * @see #SOLID_FOREGROUND * @see #FINE_DOTS * @see #ALT_BARS * @see #SPARSE_DOTS * @see #THICK_HORZ_BANDS * @see #THICK_VERT_BANDS * @see #THICK_BACKWARD_DIAG * @see #THICK_FORWARD_DIAG * @see #BIG_SPOTS * @see #BRICKS * @see #THIN_HORZ_BANDS * @see #THIN_VERT_BANDS * @see #THIN_BACKWARD_DIAG * @see #THIN_FORWARD_DIAG * @see #SQUARES * @see #DIAMONDS * * @param fp fill pattern (set to 1 to fill w/foreground color) */ public void setFillPattern(short fp) { format.setAdtlFillPattern(fp); } /** * get the fill pattern (??) - set to 1 to fill with foreground color * @return fill pattern */ public short getFillPattern() { return format.getAdtlFillPattern(); } /** * Checks if the background and foreground fills are set correctly when one * or the other is set to the default color. * <p>Works like the logic table below:</p> * <p>BACKGROUND FOREGROUND</p> * <p>NONE AUTOMATIC</p> * <p>0x41 0x40</p> * <p>NONE RED/ANYTHING</p> * <p>0x40 0xSOMETHING</p> */ private void checkDefaultBackgroundFills() { if (format.getFillForeground() == org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index) { //JMH: Why +1, hell why not. I guess it made some sense to someone at the time. Doesnt //to me now.... But experience has shown that when the fore is set to AUTOMATIC then the //background needs to be incremented...... if (format.getFillBackground() != (org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1)) setFillBackgroundColor((short)(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1)); } else if (format.getFillBackground() == org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1) //Now if the forground changes to a non-AUTOMATIC color the background resets itself!!! if (format.getFillForeground() != org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index) setFillBackgroundColor(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index); } /** * set the background fill color. * <p> * For example: * <pre> * cs.setFillPattern(HSSFCellStyle.FINE_DOTS ); * cs.setFillBackgroundColor(new HSSFColor.RED().getIndex()); * </pre> * optionally a Foreground and background fill can be applied: * <i>Note: Ensure Foreground color is set prior to background</i> * <pre> * cs.setFillPattern(HSSFCellStyle.FINE_DOTS ); * cs.setFillForegroundColor(new HSSFColor.BLUE().getIndex()); * cs.setFillBackgroundColor(new HSSFColor.RED().getIndex()); * </pre> * or, for the special case of SOLID_FILL: * <pre> * cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND ); * cs.setFillForegroundColor(new HSSFColor.RED().getIndex()); * </pre> * It is necessary to set the fill style in order * for the color to be shown in the cell. * * @param bg color */ public void setFillBackgroundColor(short bg) { format.setFillBackground(bg); checkDefaultBackgroundFills(); } /** * 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 */ public short getFillBackgroundColor() { short result = format.getFillBackground(); //JMH: Do this ridiculous conversion, and let HSSFCellStyle //internally migrate back and forth if (result == (HSSFColor.AUTOMATIC.index+1)) return HSSFColor.AUTOMATIC.index; else return result; } /** * set the foreground fill color * <i>Note: Ensure Foreground color is set prior to background color.</i> * @param bg color */ public void setFillForegroundColor(short bg) { format.setFillForeground(bg); checkDefaultBackgroundFills(); } /** * 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 */ public short getFillForegroundColor() { return format.getFillForeground(); } /** * Gets the name of the user defined style. * Returns null for built in styles, and * styles where no name has been defined */ public String getUserStyleName() { StyleRecord sr = workbook.getStyleRecord(index); if(sr == null) { return null; } if(sr.getType() == StyleRecord.STYLE_BUILT_IN) { return null; } return sr.getName(); } /** * Sets the name of the user defined style. * Will complain if you try this on a built in style. */ public void setUserStyleName(String styleName) { StyleRecord sr = workbook.getStyleRecord(index); if(sr == null) { sr = workbook.createStyleRecord(index); } if(sr.getType() == StyleRecord.STYLE_BUILT_IN) { throw new IllegalArgumentException("Unable to set user specified style names for built in styles!"); } sr.setName(styleName); } /** * Verifies that this style belongs to the supplied Workbook. * Will throw an exception if it belongs to a different one. * This is normally called when trying to assign a style to a * cell, to ensure the cell and the style are from the same * workbook (if they're not, it won't work) * @throws IllegalArgumentException if there's a workbook mis-match */ public void verifyBelongsToWorkbook(HSSFWorkbook wb) { if(wb.getWorkbook() != workbook) { throw new IllegalArgumentException("This Style does not belong to the supplied Workbook. Are you trying to assign a style from one workbook to the cell of a differnt workbook?"); } } /** * Clones all the style information from another * HSSFCellStyle, onto this one. This * HSSFCellStyle will then have all the same * properties as the source, but the two may * be edited independently. * Any stylings on this HSSFCellStyle will be lost! * * The source HSSFCellStyle could be from another * HSSFWorkbook if you like. This allows you to * copy styles from one HSSFWorkbook to another. */ public void cloneStyleFrom(HSSFCellStyle source) { // First we need to clone the extended format // record format.cloneStyleFrom(source.format); // Handle matching things if we cross workbooks if(workbook != source.workbook) { // Then we need to clone the format string, // and update the format record for this short fmt = workbook.createFormat( source.getDataFormatString() ); setDataFormat(fmt); // Finally we need to clone the font, // and update the format record for this FontRecord fr = workbook.createNewFont(); fr.cloneStyleFrom( source.workbook.getFontRecordAt( source.getFontIndex() ) ); HSSFFont font = new HSSFFont( (short)workbook.getFontIndex(fr), fr ); setFont(font); } } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((format == null) ? 0 : format.hashCode()); result = prime * result + index; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof HSSFCellStyle) { final HSSFCellStyle other = (HSSFCellStyle) obj; if (format == null) { if (other.format != null) return false; } else if (!format.equals(other.format)) return false; if (index != other.index) return false; return true; } return false; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?