org.apache.poi.ss.usermodel.FillPatternType Java Examples

The following examples show how to use org.apache.poi.ss.usermodel.FillPatternType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XLSXResponseWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
SerialWriteWorkbook() {
  this.swb = new SXSSFWorkbook(100);
  this.sh = this.swb.createSheet();

  this.rowIndex = 0;

  this.headerStyle = (XSSFCellStyle)swb.createCellStyle();
  this.headerStyle.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
  //solid fill
  this.headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  Font headerFont = swb.createFont();
  headerFont.setFontHeightInPoints((short)14);
  headerFont.setBold(true);
  headerFont.setColor(IndexedColors.WHITE.getIndex());
  this.headerStyle.setFont(headerFont);
}
 
Example #2
Source File: ExcelCellStyleBuilder.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
void xlsx_backgroundStyle( final CellBackground bg, final HSSFCellStyleProducer.HSSFCellStyleKey styleKey ) {
  final XSSFCellStyle xssfCellStyle = (XSSFCellStyle) hssfCellStyle;
  if ( BorderStyle.NONE.equals( bg.getBottom().getBorderStyle() ) == false ) {
    hssfCellStyle.setBorderBottom( styleKey.getBorderStrokeBottom() );
    xssfCellStyle.setBorderColor( XSSFCellBorder.BorderSide.BOTTOM, new XSSFColor( styleKey.getExtendedColorBottom() ) );
  }
  if ( BorderStyle.NONE.equals( bg.getTop().getBorderStyle() ) == false ) {
    hssfCellStyle.setBorderTop( styleKey.getBorderStrokeTop() );
    xssfCellStyle.setBorderColor( XSSFCellBorder.BorderSide.TOP, new XSSFColor( styleKey.getExtendedColorTop() ) );
  }
  if ( BorderStyle.NONE.equals( bg.getLeft().getBorderStyle() ) == false ) {
    hssfCellStyle.setBorderLeft( styleKey.getBorderStrokeLeft() );
    xssfCellStyle.setBorderColor( XSSFCellBorder.BorderSide.LEFT, new XSSFColor( styleKey.getExtendedColorLeft() ) );
  }
  if ( BorderStyle.NONE.equals( bg.getRight().getBorderStyle() ) == false ) {
    hssfCellStyle.setBorderRight( styleKey.getBorderStrokeRight() );
    xssfCellStyle.setBorderColor( XSSFCellBorder.BorderSide.RIGHT, new XSSFColor( styleKey.getExtendedColorRight() ) );
  }
  if ( bg.getBackgroundColor() != null ) {
    xssfCellStyle.setFillForegroundColor( new XSSFColor( styleKey.getExtendedColor() ) );
    hssfCellStyle.setFillPattern( FillPatternType.SOLID_FOREGROUND );
  }
}
 
Example #3
Source File: ExcelCellStyleBuilderTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testXls_BackgroundStyle() {
  when( workbook.createCellStyle() ).thenReturn( xlsStyle );
  ExcelCellStyleBuilder builder = new ExcelCellStyleBuilder( workbook );

  CellBackground bg = getBackground();
  HSSFCellStyleProducer.HSSFCellStyleKey styleKey = getXlsKey();

  builder.xls_backgroundStyle( bg, styleKey );

  verify( xlsStyle, times( 1 ) ).setBorderBottom( eq( BorderStyle.MEDIUM_DASH_DOT ) );
  verify( xlsStyle, times( 1 ) ).setBottomBorderColor( eq( (short) 116 ) );
  verify( xlsStyle, times( 1 ) ).setBorderTop( eq( BorderStyle.MEDIUM_DASHED ) );
  verify( xlsStyle, times( 1 ) ).setTopBorderColor( eq( (short) 118 ) );
  verify( xlsStyle, times( 1 ) ).setBorderLeft( eq( BorderStyle.MEDIUM_DASH_DOT_DOT ) );
  verify( xlsStyle, times( 1 ) ).setLeftBorderColor( eq( (short) 120 ) );
  verify( xlsStyle, times( 1 ) ).setBorderRight( eq( BorderStyle.MEDIUM ) );
  verify( xlsStyle, times( 1 ) ).setRightBorderColor( eq( (short) 122 ) );
  verify( xlsStyle, times( 1 ) ).setFillForegroundColor( eq( (short) 123 ) );
  verify( xlsStyle, times( 1 ) ).setFillPattern( eq( FillPatternType.SOLID_FOREGROUND ) );
}
 
Example #4
Source File: DataExporter.java    From rebuild with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 样式
 *
 * @return
 */
protected HorizontalCellStyleStrategy buildStyle() {
    WriteFont baseFont = new WriteFont();
    baseFont.setFontHeightInPoints((short) 12);
    baseFont.setColor(IndexedColors.BLACK.getIndex());

    // 头
    WriteCellStyle headStyle = new WriteCellStyle();
    headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    headStyle.setWriteFont(baseFont);
    // 内容
    WriteCellStyle contentStyle = new WriteCellStyle();
    contentStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
    // 这里需要指定 FillPatternType 为 FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色
    // 头默认了 FillPatternType 所以可以不指定
    contentStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
    contentStyle.setWriteFont(baseFont);
    contentStyle.setBorderBottom(BorderStyle.THIN);
    contentStyle.setBorderRight(BorderStyle.THIN);

    return new HorizontalCellStyleStrategy(headStyle, contentStyle);
}
 
Example #5
Source File: ExcelCellStyleBuilderTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testXlsx_BackgroundStyle() {
  when( workbook.createCellStyle() ).thenReturn( xlsxStyle );
  ExcelCellStyleBuilder builder = new ExcelCellStyleBuilder( workbook );

  CellBackground bg = getBackground();
  HSSFCellStyleProducer.HSSFCellStyleKey styleKey = getXlsxKey();

  builder.withBackgroundStyle( bg, styleKey );

  verify( xlsxStyle, times( 1 ) ).setBorderBottom( eq( BorderStyle.DASH_DOT ) );
  verify( xlsxStyle, times( 1 ) )
    .setBorderColor( eq( XSSFCellBorder.BorderSide.BOTTOM ), notNull( XSSFColor.class ) );
  verify( xlsxStyle, times( 1 ) ).setBorderTop( eq( BorderStyle.DOTTED ) );
  verify( xlsxStyle, times( 1 ) ).setBorderColor( eq( XSSFCellBorder.BorderSide.TOP ), notNull( XSSFColor.class ) );
  verify( xlsxStyle, times( 1 ) ).setBorderLeft( eq( BorderStyle.DASH_DOT_DOT ) );
  verify( xlsxStyle, times( 1 ) ).setBorderColor( eq( XSSFCellBorder.BorderSide.LEFT ), notNull( XSSFColor.class ) );
  verify( xlsxStyle, times( 1 ) ).setBorderRight( eq( BorderStyle.DASHED ) );
  verify( xlsxStyle, times( 1 ) ).setBorderColor( eq( XSSFCellBorder.BorderSide.RIGHT ), notNull( XSSFColor.class ) );
  verify( xlsxStyle, times( 1 ) ).setFillForegroundColor( notNull( XSSFColor.class ) );
  verify( xlsxStyle, times( 1 ) ).setFillPattern( eq( FillPatternType.SOLID_FOREGROUND ) );
}
 
Example #6
Source File: ExcelCellStyleBuilder.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
void xls_backgroundStyle( final CellBackground bg, final HSSFCellStyleProducer.HSSFCellStyleKey styleKey ) {
  if ( BorderStyle.NONE.equals( bg.getBottom().getBorderStyle() ) == false ) {
    hssfCellStyle.setBorderBottom( styleKey.getBorderStrokeBottom() );
    hssfCellStyle.setBottomBorderColor( styleKey.getColorBottom() );
  }
  if ( BorderStyle.NONE.equals( bg.getTop().getBorderStyle() ) == false ) {
    hssfCellStyle.setBorderTop( styleKey.getBorderStrokeTop() );
    hssfCellStyle.setTopBorderColor( styleKey.getColorTop() );
  }
  if ( BorderStyle.NONE.equals( bg.getLeft().getBorderStyle() ) == false ) {
    hssfCellStyle.setBorderLeft( styleKey.getBorderStrokeLeft() );
    hssfCellStyle.setLeftBorderColor( styleKey.getColorLeft() );
  }
  if ( BorderStyle.NONE.equals( bg.getRight().getBorderStyle() ) == false ) {
    hssfCellStyle.setBorderRight( styleKey.getBorderStrokeRight() );
    hssfCellStyle.setRightBorderColor( styleKey.getColorRight() );
  }
  if ( bg.getBackgroundColor() != null ) {
    hssfCellStyle.setFillForegroundColor( styleKey.getColor() );
    hssfCellStyle.setFillPattern( FillPatternType.SOLID_FOREGROUND );
  }
}
 
Example #7
Source File: XLSXWriter.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * create a library of cell styles
 */
private static Map<String, CellStyle> createStyles(Workbook wb){
  Map<String, CellStyle> styles = new HashMap<>();

  CellStyle style;
  Font headerFont = wb.createFont();
  headerFont.setBold(true);
  style = createBorderedStyle(wb);
  style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
  style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  style.setVerticalAlignment(VerticalAlignment.TOP);
  style.setWrapText(true);
  style.setFont(headerFont);
  styles.put("header", style);

  style = createBorderedStyle(wb);
  style.setVerticalAlignment(VerticalAlignment.TOP);
  style.setWrapText(true);    
  styles.put("body", style);

  return styles;
}
 
Example #8
Source File: AnnoFormulaTest.java    From xlsmapper with Apache License 2.0 6 votes vote down vote up
@XlsPostSave
public void handlePostSave(final Sheet sheet) {

    if(!name.equals("平均")) {
        return;
    }

    final Workbook book = sheet.getWorkbook();

    for(Point address : positions.values()) {
        Cell cell = POIUtils.getCell(sheet, address);

        CellStyle style = book.createCellStyle();
        style.cloneStyleFrom(cell.getCellStyle());

        // 塗りつぶし
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 罫線の上部を変更
        style.setBorderTop(BorderStyle.DOUBLE);

        cell.setCellStyle(style);
    }

}
 
Example #9
Source File: TextCellsTest.java    From excel-io with MIT License 6 votes vote down vote up
@Test
public void createsMultipleTextCellsWithStyle() throws IOException {
    final Array<ECell> cells = new TextCells("a", "b", "c")
        .with(
            new XsStyle(
                new FillPattern(FillPatternType.SOLID_FOREGROUND)
            )
        ).asArray();
    try (final Workbook wbook = new XSSFWorkbook()) {
        final Row row = wbook.createSheet().createRow(0);
        for (final ECell cell : cells) {
            cell.attachTo(row);
        }
        MatcherAssert.assertThat(
            row.getCell(0).getCellStyle().getFillPatternEnum(),
            Matchers.equalTo(FillPatternType.SOLID_FOREGROUND)
        );
    }
}
 
Example #10
Source File: CellTemplateTest.java    From excel-io with MIT License 6 votes vote down vote up
/**
 * Create custom cell with additional style.
 * @throws IOException If fails
 */
@Test
public void createsCustomCellWithStyle() throws IOException {
    try (final Workbook workbook = new XSSFWorkbook()) {
        final Cell cell = new CellTemplateTest.GrayTextCell()
            .with(
                new XsStyle(
                    new FillPattern(FillPatternType.SOLID_FOREGROUND)
                )
            )
            .attachTo(workbook.createSheet().createRow(0));
        MatcherAssert.assertThat(
            cell.getCellStyle().getFillPatternEnum(),
            Matchers.equalTo(FillPatternType.SOLID_FOREGROUND)
        );
    }
}
 
Example #11
Source File: ExcelStyle.java    From objectlabkit with Apache License 2.0 6 votes vote down vote up
public CellStyle build(ExcelCell cell) {
    Optional<CellStyle> cs = cell.findStyle(hashCode());
    if (cs.isPresent()) {
        return cs.get();
    }

    CellStyle cellStyle = cell.cloneStyle(hashCode());
    addFontFormat(cell, cellStyle);

    if (header) {
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    }

    addFormat(cell, cellStyle);

    if (backgroundColour != null) {
        cellStyle.setFillForegroundColor(backgroundColour.getIndex());
        cellStyle.setFillPattern(backgroundFillPatternType != null ? backgroundFillPatternType : FillPatternType.SOLID_FOREGROUND);
    }

    addAlignment(cellStyle);
    return cellStyle;
}
 
Example #12
Source File: StyleManagerXUtils.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void addBackgroundColourToStyle(Workbook workbook, CellStyle style, String colour) {
	if(colour == null) {
		return ;
	}
	if(IStyle.TRANSPARENT_VALUE.equals(colour)) {
		return ;
	}
	if(style instanceof XSSFCellStyle) {
		XSSFCellStyle cellStyle = (XSSFCellStyle)style;
		XSSFColor xColour = getXColour(colour);
		if(xColour != null) {
			cellStyle.setFillForegroundColor(xColour);
			cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		}
	}
}
 
Example #13
Source File: AbstractWriteHolder.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
@Deprecated
private WriteCellStyle buildWriteCellStyle(Font font, IndexedColors indexedColors) {
    WriteCellStyle writeCellStyle = new WriteCellStyle();
    if (indexedColors != null) {
        writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        writeCellStyle.setFillForegroundColor(indexedColors.getIndex());
    }
    if (font != null) {
        WriteFont writeFont = new WriteFont();
        writeFont.setFontName(font.getFontName());
        writeFont.setFontHeightInPoints(font.getFontHeightInPoints());
        writeFont.setBold(font.isBold());
        writeCellStyle.setWriteFont(writeFont);
    }
    return writeCellStyle;
}
 
Example #14
Source File: StyleDataTest.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
private void readAndWrite(File file) {
    SimpleColumnWidthStyleStrategy simpleColumnWidthStyleStrategy = new SimpleColumnWidthStyleStrategy(50);
    SimpleRowHeightStyleStrategy simpleRowHeightStyleStrategy =
        new SimpleRowHeightStyleStrategy((short)40, (short)50);

    WriteCellStyle headWriteCellStyle = new WriteCellStyle();
    headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
    WriteFont headWriteFont = new WriteFont();
    headWriteFont.setFontHeightInPoints((short)20);
    headWriteCellStyle.setWriteFont(headWriteFont);
    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
    contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
    contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    WriteFont contentWriteFont = new WriteFont();
    contentWriteFont.setFontHeightInPoints((short)20);
    headWriteCellStyle.setWriteFont(contentWriteFont);
    HorizontalCellStyleStrategy horizontalCellStyleStrategy =
        new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

    OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(2, 2, 0, 1);
    EasyExcel.write(file, StyleData.class).registerWriteHandler(simpleColumnWidthStyleStrategy)
        .registerWriteHandler(simpleRowHeightStyleStrategy).registerWriteHandler(horizontalCellStyleStrategy)
        .registerWriteHandler(onceAbsoluteMergeStrategy).sheet().doWrite(data());
    EasyExcel.read(file, StyleData.class, new StyleDataListener()).sheet().doRead();
}
 
Example #15
Source File: JRXlsMetadataExporter.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void initReport() 
{
	super.initReport();

	XlsReportConfiguration configuration = getCurrentItemConfiguration();
	
	if (!configuration.isWhitePageBackground())
	{
		backgroundMode = FillPatternType.NO_FILL;
	}

	nature = 
		new JRXlsMetadataExporterNature(
			jasperReportsContext, 
			filter, 
			configuration.isIgnoreGraphics(), 
			configuration.isIgnorePageMargins()
			);
}
 
Example #16
Source File: JRXlsExporter.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
public StyleInfo(
	FillPatternType mode,
	short backcolor,
	HorizontalAlignment horizontalAlignment,
	VerticalAlignment verticalAlignment,
	short rotation,
	HSSFFont font,
	JRExporterGridCell gridCell,
	boolean wrapText,
	boolean cellLocked,
	boolean cellHidden,
	boolean shrinkToFit
	)
{
	this(mode, 
		backcolor, 
		horizontalAlignment, 
		verticalAlignment, 
		rotation, 
		font, 
		(gridCell == null ? null : new BoxStyle(gridCell)), 
		wrapText, 
		cellLocked, 
		cellHidden, 
		shrinkToFit);
}
 
Example #17
Source File: XLSXWriter.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * create a library of cell styles
 */
private static Map<String, CellStyle> createStyles(Workbook wb){
  Map<String, CellStyle> styles = new HashMap<>();

  CellStyle style;
  Font headerFont = wb.createFont();
  headerFont.setBold(true);
  style = createBorderedStyle(wb);
  style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
  style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  style.setVerticalAlignment(VerticalAlignment.TOP);
  style.setWrapText(true);
  style.setFont(headerFont);
  styles.put("header", style);

  style = createBorderedStyle(wb);
  style.setVerticalAlignment(VerticalAlignment.TOP);
  style.setWrapText(true);    
  styles.put("body", style);

  return styles;
}
 
Example #18
Source File: JRXlsExporter.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected HSSFCellStyle getLoadedCellStyle(
	FillPatternType mode,
	short backcolor,
	HorizontalAlignment horizontalAlignment,
	VerticalAlignment verticalAlignment,
	short rotation,
	HSSFFont font,
	BoxStyle box,
	boolean isWrapText,
	boolean isCellLocked,
	boolean isCellHidden,
	boolean isShrinkToFit
	)
{
	StyleInfo style = new StyleInfo(mode, backcolor, horizontalAlignment, verticalAlignment, rotation, font, box, isWrapText, isCellLocked, isCellHidden, isShrinkToFit);
	return getLoadedCellStyle(style);
}
 
Example #19
Source File: JRXlsExporter.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void initReport()
{
	super.initReport();

	XlsReportConfiguration configuration = getCurrentItemConfiguration();
	
	if (!configuration.isWhitePageBackground())
	{
		backgroundMode = FillPatternType.NO_FILL;
	}

	nature = 
		new JRXlsExporterNature(
			jasperReportsContext, 
			filter, 
			configuration.isIgnoreGraphics(), 
			configuration.isIgnorePageMargins()
			);
}
 
Example #20
Source File: BackgroundStyle.java    From myexcel with Apache License 2.0 6 votes vote down vote up
public static void setBackgroundColor(CellStyle style, Map<String, String> tdStyle, CustomColor customColor) {
    if (tdStyle == null) {
        return;
    }
    String color = tdStyle.get(BACKGROUND_COLOR);
    if (color == null) {
        return;
    }
    Short colorPredefined = ColorUtil.getPredefinedColorIndex(color);
    if (colorPredefined != null) {
        style.setFillForegroundColor(colorPredefined);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return;
    }
    if (customColor.isXls()) {
        log.warn(".xls does not support custom colors for the time being. Please use predefined colors.");
        return;
    }
    int[] rgb = ColorUtil.getRGBByColor(color);
    setCustomColor(style, rgb, customColor);
}
 
Example #21
Source File: AbstractSheet.java    From tools with Apache License 2.0 5 votes vote down vote up
/**
 * create the styles in the workbook
 */
private void createStyles(Workbook wb) {
	// create the styles
	this.checkboxStyle = wb.createCellStyle();
	this.checkboxStyle.setAlignment(HorizontalAlignment.CENTER);
	this.checkboxStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	this.checkboxStyle.setBorderBottom(BorderStyle.THIN);
	this.checkboxStyle.setBorderLeft(BorderStyle.THIN);
	this.checkboxStyle.setBorderRight(BorderStyle.THIN);
	this.checkboxStyle.setBorderTop(BorderStyle.THIN);
	Font checkboxFont = wb.createFont();
	checkboxFont.setFontHeight(FONT_SIZE);
	checkboxFont.setFontName(CHECKBOX_FONT_NAME);
	this.checkboxStyle.setFont(checkboxFont);
	
	this.dateStyle = wb.createCellStyle();
	DataFormat df = wb.createDataFormat();
	this.dateStyle.setDataFormat(df.getFormat("m/d/yy h:mm"));
	
	this.greenWrapped = createLeftWrapStyle(wb);
	this.greenWrapped.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
	this.greenWrapped.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	this.greenWrapped.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	
	this.yellowWrapped = createLeftWrapStyle(wb);
	this.yellowWrapped.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
	this.yellowWrapped.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	
	this.redWrapped = createLeftWrapStyle(wb);
	this.redWrapped.setFillForegroundColor(HSSFColor.RED.index);
	this.redWrapped.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
 
Example #22
Source File: ExportTimetableXLS.java    From unitime with Apache License 2.0 5 votes vote down vote up
protected CellStyle getMeetingHeaderStyle(P p, P parent, Color bgColor) {
	if (bgColor == null) bgColor = Color.WHITE;
	String styleId = "meeting-header-" + Integer.toHexString(bgColor.getRGB()) + (p.iItalics ? "-italics" : "");
	CellStyle style = iStyles.get(styleId);
	if (style == null) {
		style = iWorkbook.createCellStyle();
        style.setBorderTop(BorderStyle.THICK);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.THICK);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderRight(BorderStyle.THICK);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.TOP);
        style.setFont(getFont(parent));
        String colorId = Integer.toHexString(bgColor.getRGB());
		Short color = iColors.get(colorId);
		if (color == null) {
			HSSFPalette palette = ((HSSFWorkbook)iWorkbook).getCustomPalette();
			HSSFColor clr = palette.findSimilarColor(bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue());
			color = (clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndex());
			iColors.put(colorId, color);
		}
        style.setFillForegroundColor(color);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setWrapText(true);
        iStyles.put(styleId, style);
	}
	return style;
}
 
Example #23
Source File: AbstractSheet.java    From tools with Apache License 2.0 5 votes vote down vote up
public static CellStyle createHeaderStyle(Workbook wb) {
	CellStyle headerStyle = wb.createCellStyle();
	headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
	headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	Font headerFont = wb.createFont();
	headerFont.setFontName("Arial");
	headerFont.setFontHeight(FONT_SIZE);
	headerFont.setBold(true);
	headerStyle.setFont(headerFont);
	headerStyle.setAlignment(HorizontalAlignment.CENTER);
	headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	headerStyle.setWrapText(true);
	return headerStyle;
}
 
Example #24
Source File: ExportTimetableXLS.java    From unitime with Apache License 2.0 5 votes vote down vote up
protected CellStyle getMeetingFooterStyle(P p, P parent, Color bgColor) {
	if (bgColor == null) bgColor = Color.WHITE;
	String styleId = "meeting-footer-" + Integer.toHexString(bgColor.getRGB());
	CellStyle style = iStyles.get(styleId);
	if (style == null) {
		style = iWorkbook.createCellStyle();
        style.setBorderBottom(BorderStyle.THICK);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.THICK);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderRight(BorderStyle.THICK);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.TOP);
        style.setFont(getFont(parent));
        String colorId = Integer.toHexString(bgColor.getRGB());
		Short color = iColors.get(colorId);
		if (color == null) {
			HSSFPalette palette = ((HSSFWorkbook)iWorkbook).getCustomPalette();
			HSSFColor clr = palette.findSimilarColor(bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue());
			color = (clr == null ? IndexedColors.WHITE.getIndex() : clr.getIndex());
			iColors.put(colorId, color);
		}
        style.setFillForegroundColor(color);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setWrapText(true);
        iStyles.put(styleId, style);
	}
	return style;
}
 
Example #25
Source File: JRXlsExporter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void exportFrame(JRPrintFrame frame, JRExporterGridCell gridCell, int x, int y)
{
	FillPatternType mode = backgroundMode;
	short backcolor = whiteIndex;
	if (frame.getModeValue() == ModeEnum.OPAQUE)
	{
		mode = FillPatternType.SOLID_FOREGROUND;
		backcolor = getWorkbookColor(frame.getBackcolor()).getIndex();
	}

	short forecolor = getWorkbookColor(frame.getForecolor()).getIndex();

	HSSFCellStyle cellStyle =
		getLoadedCellStyle(
			mode,
			backcolor,
			HorizontalAlignment.LEFT,
			VerticalAlignment.TOP,
			(short)0,
			getLoadedFont(getDefaultFont(), forecolor, null, getLocale()),
			gridCell,
			isWrapText(frame),
			isCellLocked(frame),
			isCellHidden(frame),
			isShrinkToFit(frame)
			);

	createMergeRegion(gridCell, x, y, cellStyle);

	cell = row.createCell(x);
	cell.setCellStyle(cellStyle);
}
 
Example #26
Source File: SpreadsheetWorkbook.java    From taro with MIT License 5 votes vote down vote up
private CellStyle createNewStyle(SpreadsheetCellStyle style) {
    XSSFCellStyle cellStyle = workbook.createCellStyle();
    if (style.getAlign() != null) cellStyle.setAlignment(style.getAlign());
    if (style.getVerticalAlign() != null) cellStyle.setVerticalAlignment(style.getVerticalAlign());
    if (style.getTopBorder() != null) cellStyle.setBorderTop(style.getTopBorder());
    if (style.getLeftBorder() != null) cellStyle.setBorderLeft(style.getLeftBorder());
    if (style.getBottomBorder() != null) cellStyle.setBorderBottom(style.getBottomBorder());
    if (style.getRightBorder() != null) cellStyle.setBorderRight(style.getRightBorder());
    if (style.getLocked() != null) cellStyle.setLocked(style.getLocked());
    if (style.isHidden() != null) cellStyle.setHidden(style.isHidden());
    if (style.getWrapText() != null) cellStyle.setWrapText(style.getWrapText());
    if (style.getIndention() != null) cellStyle.setIndention(checkedCast(style.getIndention()));
    if (style.getRotation() != null) cellStyle.setRotation(checkedCast(style.getRotation()));
    if (style.getTopBorderColor() != null) cellStyle.setTopBorderColor(new XSSFColor(style.getTopBorderColor()));
    if (style.getLeftBorderColor() != null) cellStyle.setLeftBorderColor(new XSSFColor(style.getLeftBorderColor()));
    if (style.getBottomBorderColor() != null) cellStyle.setBottomBorderColor(new XSSFColor(style.getBottomBorderColor()));
    if (style.getRightBorderColor() != null) cellStyle.setRightBorderColor(new XSSFColor(style.getRightBorderColor()));

    if (style.getFont() != null) {
        cellStyle.setFont(registerFont(style.getFont()));
    }

    if (style.getBackgroundColor() != null) {
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(new XSSFColor(style.getBackgroundColor()));
    }

    if (style.getDataFormatString() != null) {
        cellStyle.setDataFormat(workbook.createDataFormat().getFormat(style.getDataFormatString()));
    }

    return cellStyle;
}
 
Example #27
Source File: JRXlsExporter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected HSSFCellStyle getLoadedCellStyle(
	FillPatternType mode,
	short backcolor,
	HorizontalAlignment horizontalAlignment,
	VerticalAlignment verticalAlignment,
	short rotation,
	HSSFFont font,
	JRExporterGridCell gridCell,
	boolean isWrapText,
	boolean isCellLocked,
	boolean isCellHidden,
	boolean isShrinkToFit
	)
{
	return getLoadedCellStyle(
		new StyleInfo(
			mode, 
			backcolor, 
			horizontalAlignment, 
			verticalAlignment, 
			rotation, 
			font, 
			gridCell, 
			isWrapText, 
			isCellLocked, 
			isCellHidden, 
			isShrinkToFit));
}
 
Example #28
Source File: ExcelUtil.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 创建表格样式
 * 
 * @param wb 工作薄对象
 * @return 样式列表
 */
private Map<String, CellStyle> createStyles(Workbook wb)
{
    // 写入各条记录,每条记录对应excel表中的一行
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
    CellStyle style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderLeft(BorderStyle.THIN);
    style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderTop(BorderStyle.THIN);
    style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    Font dataFont = wb.createFont();
    dataFont.setFontName("Arial");
    dataFont.setFontHeightInPoints((short) 10);
    style.setFont(dataFont);
    styles.put("data", style);

    style = wb.createCellStyle();
    style.cloneStyleFrom(styles.get("data"));
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    Font headerFont = wb.createFont();
    headerFont.setFontName("Arial");
    headerFont.setFontHeightInPoints((short) 10);
    headerFont.setBold(true);
    headerFont.setColor(IndexedColors.WHITE.getIndex());
    style.setFont(headerFont);
    styles.put("header", style);

    return styles;
}
 
Example #29
Source File: JRXlsExporter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void addBlankCell(JRExporterGridCell gridCell, int colIndex, int rowIndex)
{
	cell = row.createCell(colIndex);

	FillPatternType mode = backgroundMode;
	short backcolor = whiteIndex;
	
	if (!Boolean.TRUE.equals(sheetInfo.ignoreCellBackground) && gridCell.getCellBackcolor() != null)
	{
		mode = FillPatternType.SOLID_FOREGROUND;
		backcolor = getWorkbookColor(gridCell.getCellBackcolor()).getIndex();
	}

	short forecolor = blackIndex;
	if (gridCell.getForecolor() != null)
	{
		forecolor = getWorkbookColor(gridCell.getForecolor()).getIndex();
	}

	HSSFCellStyle cellStyle =
		getLoadedCellStyle(
			mode,
			backcolor,
			HorizontalAlignment.LEFT,
			VerticalAlignment.TOP,
			(short)0,
			getLoadedFont(getDefaultFont(), forecolor, null, getLocale()),
			gridCell,
			true, 
			true, 
			false, 
			false
			);

	cell.setCellStyle(cellStyle);
}
 
Example #30
Source File: JRXlsExporter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void exportRectangle(JRPrintGraphicElement element, JRExporterGridCell gridCell, int colIndex, int rowIndex)
{
	short forecolor = getWorkbookColor(element.getLinePen().getLineColor()).getIndex();

	FillPatternType mode = backgroundMode;
	short backcolor = whiteIndex;
	if (!Boolean.TRUE.equals(sheetInfo.ignoreCellBackground) && gridCell.getCellBackcolor() != null)
	{
		mode = FillPatternType.SOLID_FOREGROUND;
		backcolor = getWorkbookColor(gridCell.getCellBackcolor()).getIndex();
	}

	HSSFCellStyle cellStyle =
		getLoadedCellStyle(
			mode,
			backcolor,
			HorizontalAlignment.LEFT,
			VerticalAlignment.TOP,
			(short)0,
			getLoadedFont(getDefaultFont(), forecolor, null, getLocale()),
			gridCell,
			isWrapText(element),
			isCellLocked(element),
			isCellHidden(element),
			isShrinkToFit(element)
			);

	createMergeRegion(gridCell, colIndex, rowIndex, cellStyle);

	cell = row.createCell(colIndex);
	cell.setCellStyle(cellStyle);
}