import org.apache.poi.ss.usermodel.Font; //导入方法依赖的package包/类
/**
* Find a CellStyle
with all the same attributes as the given
* CellStyle
but with the given font index.
* @param workbook The Workbook
.
* @param cellStyle The CellStyle
to find.
* @param font The Font
to find.
* @return The CellStyle
from the Workbook
if
* found, or null
if not found.
*/
private static CellStyle findCellStyle(Workbook workbook, CellStyle cellStyle, Font font)
{
int numCellStyles = workbook.getNumCellStyles();
for (short i = 0; i < numCellStyles; i++)
{
CellStyle cs = workbook.getCellStyleAt(i);
Font f = workbook.getFontAt(cs.getFontIndex());
if (cs.getFillForegroundColor() == cellStyle.getFillForegroundColor() &&
cs.getFillBackgroundColor() == cellStyle.getFillBackgroundColor() &&
cs.getDataFormat() == cellStyle.getDataFormat() &&
cs.getAlignment() == cellStyle.getAlignment() &&
cs.getBorderBottom() == cellStyle.getBorderBottom() &&
cs.getBorderLeft() == cellStyle.getBorderLeft() &&
cs.getBorderRight() == cellStyle.getBorderRight() &&
cs.getBorderTop() == cellStyle.getBorderTop() &&
cs.getFillPattern() == cellStyle.getFillPattern() &&
cs.getWrapText() == cellStyle.getWrapText() &&
cs.getRotation() == cellStyle.getRotation() &&
cs.getBottomBorderColor() == cellStyle.getBottomBorderColor() &&
cs.getTopBorderColor() == cellStyle.getTopBorderColor() &&
cs.getLeftBorderColor() == cellStyle.getLeftBorderColor() &&
cs.getRightBorderColor() == cellStyle.getRightBorderColor() &&
cs.getVerticalAlignment() == cellStyle.getVerticalAlignment() &&
cs.getIndention() == cellStyle.getIndention() &&
cs.getLocked() == cellStyle.getLocked() &&
cs.getHidden() == cellStyle.getHidden() &&
f.getBoldweight() == font.getBoldweight() &&
f.getItalic() == font.getItalic() &&
f.getColor() == font.getColor() &&
f.getFontHeight() == font.getFontHeight() &&
f.getUnderline() == font.getUnderline() &&
f.getFontName().equals(font.getFontName()) &&
f.getTypeOffset() == font.getTypeOffset()
)
{
if (!(font instanceof XSSFFont && f instanceof XSSFFont) ||
((XSSFFont) font).getXSSFColor().getARGBHex().equals(((XSSFFont) f).getXSSFColor().getARGBHex()))
{
if (DEBUG)
System.err.println(" Found existing, matching CellStyle with the Font!");
return cs;
}
}
}
if (DEBUG)
System.err.println(" Did NOT find existing, matching CellStyle with the Font!");
return null;
}