本文主要是介绍【POI】Java设置Excel(.xlsx)单元格属性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用过的几种样式的设置方式:
XXXServiceImpl.java
public class XXXServiceImpl implements XXXService {@Overridepublic void test() {Map<String, Object> styleMap = new HashMap<String, Object>();styleMap.put("backgroundColor", new XSSFColor(new Color(0, 0, 0)));
// styleMap.put("backgroundColor", IndexedColors.WHITE.getIndex());styleMap.put("fontColor", new XSSFColor(new Color(255, 255, 255)));styleMap.put("fontName", "MS Pゴシック");
// styleMap.put("fontName", "Meiryo");styleMap.put("fontSize", (short) 10);styleMap.put("fontBold", true);styleMap.put("isHorizontalAlignCenter", true);// 细线styleMap.put("borderTopStyle", BorderStyle.THIN);
// // 粗线
// styleMap.put("borderTopStyle", BorderStyle.MEDIUM);
// // 虚线
// styleMap.put("borderTopStyle", BorderStyle.HAIR);styleMap.put("borderTopColor", new XSSFColor(new Color(0, 0, 0)));styleMap.put("dataFomrat", "#,##0");
// styleMap.put("dataFomrat", "0%");
// styleMap.put("dataFomrat", "0.00%");
// styleMap.put("dataFomrat", "¥#,##0.0");
// styleMap.put("dataFomrat", "¥#,##0");}
}
XSSFUtils.java
public class XSSFUtils {/*** セルの外観を設定する** @param workbook* @param cell* @param styleMap* backgroundColor XSSFColor* fontColor XSSFColor* fontName String* fontSize Number* fontBold boolean* isHorizontalAlignCenter boolean* borderTopStyle BorderStyle* borderBottomStyle BorderStyle* borderLeftStyle BorderStyle* borderRightStyle BorderStyle* borderTopColor XSSFColor* borderBottomColor XSSFColor* borderLeftColor XSSFColor* borderRightColor XSSFColor* dataFomrat String*/public static void clearAndSetCellStyle(XSSFWorkbook workbook, XSSFCell cell, Map<String, Object> styleMap) {XSSFCellStyle cellStyle = workbook.createCellStyle();if (styleMap.get("backgroundColor") != null) {cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);cellStyle.setFillForegroundColor((XSSFColor) styleMap.get("backgroundColor"));}XSSFFont font = workbook.createFont();if (styleMap.get("fontName") != null) {font.setFontName(styleMap.get("fontName").toString());}if (styleMap.get("fontColor") != null) {font.setColor((XSSFColor) styleMap.get("fontColor"));}if (styleMap.get("fontBold") != null) {font.setBold((boolean) styleMap.get("fontBold"));}if (styleMap.get("fontSize") != null) {font.setFontHeightInPoints(((Number) styleMap.get("fontSize")).shortValue());}cellStyle.setFont(font);if (styleMap.get("isHorizontalAlignCenter") != null) {boolean isHorizontalAlignCenter = (boolean) styleMap.get("isHorizontalAlignCenter");if (isHorizontalAlignCenter) {cellStyle.setAlignment(HorizontalAlignment.CENTER);}}cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);if (styleMap.get("borderTopStyle") != null) {cellStyle.setBorderTop((BorderStyle) styleMap.get("borderTopStyle"));}if (styleMap.get("borderBottomStyle") != null) {cellStyle.setBorderBottom((BorderStyle) styleMap.get("borderBottomStyle"));}if (styleMap.get("borderLeftStyle") != null) {cellStyle.setBorderLeft((BorderStyle) styleMap.get("borderLeftStyle"));}if (styleMap.get("borderRightStyle") != null) {cellStyle.setBorderRight((BorderStyle) styleMap.get("borderRightStyle"));}if (styleMap.get("borderTopColor") != null) {cellStyle.setTopBorderColor((XSSFColor) styleMap.get("borderTopColor"));}if (styleMap.get("borderBottomColor") != null) {cellStyle.setBottomBorderColor((XSSFColor) styleMap.get("borderBottomColor"));}if (styleMap.get("borderLeftColor") != null) {cellStyle.setLeftBorderColor((XSSFColor) styleMap.get("borderLeftColor"));}if (styleMap.get("borderRightColor") != null) {cellStyle.setRightBorderColor((XSSFColor) styleMap.get("borderRightColor"));}if (styleMap.get("dataFomrat") != null) {cellStyle.setDataFormat(workbook.createDataFormat().getFormat(styleMap.get("dataFomrat").toString()));cell.setCellStyle(cellStyle);}cell.setCellStyle(cellStyle);}
}
这篇关于【POI】Java设置Excel(.xlsx)单元格属性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!