Java Code Examples for org.apache.poi.ss.usermodel.CellStyle

The following examples show how to use org.apache.poi.ss.usermodel.CellStyle. These examples are extracted from open source projects. 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 Project: autopoi   Source File: ExcelExportStylerColorImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
	CellStyle style = workbook.createCellStyle();
	style.setBorderLeft((short) 1); // 左边框
	style.setBorderRight((short) 1); // 右边框
	style.setBorderBottom((short) 1);
	style.setBorderTop((short) 1);
	style.setFillForegroundColor((short) 41); // 填充的背景颜色
	style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充图案
	style.setAlignment(CellStyle.ALIGN_CENTER);
	style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	style.setDataFormat(STRING_FORMAT);
	if (isWarp) {
		style.setWrapText(true);
	}
	return style;
}
 
Example 2
Source Project: hy.common.report   Source File: RWorkbook.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 创建一个新的样式,样式从i_DataCell中克隆出来。
 * 
 * @author      ZhengWei(HY)
 * @createDate  2017-09-11
 * @version     v1.0
 *
 * @param i_ID         标记ID。由调用者设定
 * @param i_DataCell   被克隆的单元格样式
 * @return
 */
public synchronized CellStyle getCellStyleByCopy(String i_ID ,Cell i_DataCell ,RTemplate i_RTemplate)
{
    CellStyle v_NewCellStyle = this.cellStylesByCopy.get(i_ID);
    
    if ( v_NewCellStyle == null )
    {
        v_NewCellStyle = this.workbook.createCellStyle();
        
        ExcelHelp.copyCellStyle(i_DataCell.getCellStyle(), v_NewCellStyle);
        
        Font v_FromFont = this.workbook.getFontAt(i_DataCell.getCellStyle().getFontIndex());
        Font v_NewFont  = this.workbook.createFont();
        ExcelHelp.copyFont(v_FromFont ,v_NewFont);
        
        v_NewCellStyle.setFont(v_NewFont);
        
        this.cellStylesByCopy.put(i_ID ,v_NewCellStyle);
    }
    
    return v_NewCellStyle;
}
 
Example 3
@Override
protected void exportXLSLine(final Sheet sheet, final CellStyle cellStyle, final List<Object> cells,
        final int offset) {

    final org.apache.poi.ss.usermodel.Row row = sheet.createRow(sheet.getLastRowNum() + offset);
    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

    int count = 0;
    for (final Object cellValue : cells) {
        if (++count == 3) {
            cellStyle.setAlignment(HorizontalAlignment.LEFT);
        } else {
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
        }
        addColumn(cellStyle, row, cellValue);
    }
}
 
Example 4
private CellStyle getDecimalNumberFormat(int j, Sheet sheet, CreationHelper createHelper, CellStyle dCellStyle) {

		if (decimalFormats.get(j) != null)
			return decimalFormats.get(j);
		String decimals = "";
		for (int i = 0; i < j; i++) {
			decimals += "0";
		}

		CellStyle cellStyleDoub = this.buildCellStyle(sheet); // cellStyleDoub is the default cell style for doubles
		cellStyleDoub.cloneStyleFrom(dCellStyle);
		DataFormat df = createHelper.createDataFormat();
		String format = "#,##0";
		if (decimals.length() > 0) {
			format += "." + decimals;
		}
		cellStyleDoub.setDataFormat(df.getFormat(format));

		decimalFormats.put(j, cellStyleDoub);
		return cellStyleDoub;
	}
 
Example 5
Source Project: Octopus   Source File: WorkbookContext.java    License: MIT License 6 votes vote down vote up
public CellStyle getCellStyle(Field field) {
    CellStyle style = cellStyleMap.get(field);
    if (style == null) {
        style = book.createCellStyle();
        Font font = book.createFont();
        FieldCellStyle fieldCellStyle = field.getFieldCellStyle();


        font.setFontHeightInPoints(fieldCellStyle.getFontSize());
        font.setBold(fieldCellStyle.isBold());
        ColorUtils.setColor(book, font, fieldCellStyle.getColor());
        style.setFont(font);
        ColorUtils.setForegroundColor(book, style, fieldCellStyle.getForegroundColor());

        setStyleBorder(style, fieldCellStyle.getBorder());
        ColorUtils.setBorderColor(book, style, fieldCellStyle.getBorderColor());

        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyleMap.put(field, style);
        return style;
    }
    return style;
}
 
Example 6
Source Project: tools   Source File: NonStandardLicensesSheet.java    License: Apache License 2.0 6 votes vote down vote up
public static void create(Workbook wb, String sheetName) {
	int sheetNum = wb.getSheetIndex(sheetName);
	if (sheetNum >= 0) {
		wb.removeSheetAt(sheetNum);
	}
	Sheet sheet = wb.createSheet(sheetName);
	CellStyle headerStyle = AbstractSheet.createHeaderStyle(wb);
	CellStyle centerStyle = AbstractSheet.createCenterStyle(wb);
	CellStyle wrapStyle = AbstractSheet.createLeftWrapStyle(wb);

	Row row = sheet.createRow(0);
	for (int i = 0; i < HEADER_TITLES.length; i++) {
		sheet.setColumnWidth(i, COLUMN_WIDTHS[i]*256);
		if (LEFT_WRAP[i]) {
			sheet.setDefaultColumnStyle(i, wrapStyle);
		} else if (CENTER_NOWRAP[i]) {
			sheet.setDefaultColumnStyle(i, centerStyle);
		}
		Cell cell = row.createCell(i);
		cell.setCellStyle(headerStyle);
		cell.setCellValue(HEADER_TITLES[i]);
	}
}
 
Example 7
Source Project: tools   Source File: PackageInfoSheetV09d3.java    License: Apache License 2.0 6 votes vote down vote up
public static void create(Workbook wb, String sheetName) {
	int sheetNum = wb.getSheetIndex(sheetName);
	if (sheetNum >= 0) {
		wb.removeSheetAt(sheetNum);
	}
	Sheet sheet = wb.createSheet(sheetName);
	CellStyle headerStyle = AbstractSheet.createHeaderStyle(wb);
	CellStyle defaultStyle = AbstractSheet.createLeftWrapStyle(wb);
	Row row = sheet.createRow(0);
	for (int i = 0; i < HEADER_TITLES.length; i++) {
		sheet.setColumnWidth(i, COLUMN_WIDTHS[i]*256);
		sheet.setDefaultColumnStyle(i, defaultStyle);
		Cell cell = row.createCell(i);
		cell.setCellStyle(headerStyle);
		cell.setCellValue(HEADER_TITLES[i]);
	}
}
 
Example 8
Source Project: o2oa   Source File: ActionTemplate.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
private void template(XSSFWorkbook workbook) throws Exception {
	XSSFSheet sheet = workbook.createSheet("人员");
	Row row = sheet.createRow(0);
	Cell cell = row.createCell(0);
	cell.setCellValue("姓名");
	cell = row.createCell(1);
	cell.setCellValue("手机号");
	cell = row.createCell(2);
	cell.setCellValue("电子邮件");
	cell = row.createCell(3);
	cell.setCellValue("唯一编码");
	cell = row.createCell(4);
	cell.setCellValue("员工号");
	cell = row.createCell(5);
	cell.setCellValue("性别");
	cell = row.createCell(6);
	cell.setCellValue("(地址)");
	CellStyle cellStyle = workbook.createCellStyle();
	cellStyle.setWrapText(true);
	IntStream.rangeClosed(0, 6).forEach(i -> {
		sheet.setDefaultColumnStyle(i, cellStyle);
	});
}
 
Example 9
public boolean hasValue(Cell cell) {

        if(ADD_CELL_COLOR_AS_PLAYER || INTERPRET_COLOR_AS_VALID_CELL_VALUE) {
            CellStyle style = cell.getCellStyle();
            short color = style.getFillBackgroundColor();
            if(color != 0) {
                return true;
            }
        }
        
        String str = getCellValueAsString(cell);
        if(str == null) return false;
        if(INTERPRET_FALSE_AS_EMPTY_CELL && "false".equalsIgnoreCase(str)) return false;
        if(INTERPRET_ZERO_AS_EMPTY_CELL && "0".equalsIgnoreCase(str)) return false;
        if(INTERPRET_ZERO_LENGTH_STRING_AS_EMPTY_CELL && "".equalsIgnoreCase(str)) return false;
        
        return true;
    }
 
Example 10
Source Project: Octopus   Source File: WorkbookContext.java    License: MIT License 6 votes vote down vote up
private void setStyleBorder(CellStyle style, BorderStyle[] border) {
    if (border != null) {
        if (border[0] != null) {
            style.setBorderTop(border[0]);
        }
        if (border[1] != null) {
            style.setBorderRight(border[1]);
        }
        if (border[2] != null) {
            style.setBorderBottom(border[2]);
        }
        if (border[3] != null) {
            style.setBorderLeft(border[3]);
        }
    }
}
 
Example 11
Source Project: neoscada   Source File: ExportEventsImpl.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private void makeHeader ( final List<Field> columns, final HSSFSheet sheet )
{
    final Font font = sheet.getWorkbook ().createFont ();
    font.setFontName ( "Arial" );
    font.setBoldweight ( Font.BOLDWEIGHT_BOLD );
    font.setColor ( HSSFColor.WHITE.index );

    final CellStyle style = sheet.getWorkbook ().createCellStyle ();
    style.setFont ( font );
    style.setFillForegroundColor ( HSSFColor.BLACK.index );
    style.setFillPattern ( PatternFormatting.SOLID_FOREGROUND );

    final HSSFRow row = sheet.createRow ( 0 );

    for ( int i = 0; i < columns.size (); i++ )
    {
        final Field field = columns.get ( i );

        final HSSFCell cell = row.createCell ( i );
        cell.setCellValue ( field.getHeader () );
        cell.setCellStyle ( style );
    }
}
 
Example 12
Source Project: tmxeditor8   Source File: Excel2007Writer.java    License: GNU General Public License v2.0 6 votes vote down vote up
private int addLangCell(Row header, TmxSegement segment) {
	int CellNum = header.getLastCellNum();
	if (-1 == CellNum) {
		CellNum = 0;
	}
	Cell createCell = header.createCell(CellNum);
	CellStyle cellStyle = wb.createCellStyle();
	XSSFFont headerFont = (XSSFFont) wb.createFont();
	headerFont.setBold(true);
	cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
	cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
	cellStyle.setFont(headerFont);
	createCell.setCellValue(segment.getLangCode());
	createCell.setCellStyle(cellStyle);
	sh.setColumnWidth(CellNum, (100 * 7 + 5) / 7 * 256);
	return CellNum;
}
 
Example 13
Source Project: birt   Source File: StyleManagerHUtils.java    License: 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 HSSFCellStyle) {
		HSSFCellStyle cellStyle = (HSSFCellStyle)style;
		short colourIndex = getHColour((HSSFWorkbook)workbook, colour);
		if( colourIndex > 0 ) {
			cellStyle.setFillForegroundColor(colourIndex);
			cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		}
	}
}
 
Example 14
Source Project: jeasypoi   Source File: ExcelExportServer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 创建 表头改变
 * 
 * @param entity
 * @param sheet
 * @param workbook
 * @param feildWidth
 */
public int createHeaderRow(ExportParams entity, Sheet sheet, Workbook workbook, int feildWidth) {
	Row row = sheet.createRow(0);
	row.setHeight(entity.getTitleHeight());
	createStringCell(row, 0, entity.getTitle(), getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
	for (int i = 1; i <= feildWidth; i++) {
		createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
	}
	sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, feildWidth));
	if (entity.getSecondTitle() != null) {
		row = sheet.createRow(1);
		row.setHeight(entity.getSecondTitleHeight());
		CellStyle style = workbook.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_RIGHT);
		createStringCell(row, 0, entity.getSecondTitle(), style, null);
		for (int i = 1; i <= feildWidth; i++) {
			createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
		}
		sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, feildWidth));
		return 2;
	}
	return 1;
}
 
Example 15
Source Project: bdf3   Source File: AbstractStyleBuilder.java    License: Apache License 2.0 6 votes vote down vote up
public void setCellStyleFont(Workbook workbook, CellStyle style, int i) {
	Font font = workbook.createFont();
	if (i == 0) {
		// 正常
	} else if (i == 4) {
		// 下划线
		font.setUnderline(Font.U_SINGLE);
		style.setFont(font);
	} else if (i == 2) {
		// 倾斜
		font.setItalic(true);
		style.setFont(font);
	} else if (i == 1) {
		// 加粗
		font.setBoldweight(Font.BOLDWEIGHT_BOLD);
		style.setFont(font);
	}
}
 
Example 16
Source Project: sep4j   Source File: Ssio.java    License: Apache License 2.0 6 votes vote down vote up
private static Row createHeaders(Map<String, String> headerMap,
		Sheet sheet) {
	CellStyle style = sheet.getWorkbook().createCellStyle();
	style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
	style.setFillPattern(CellStyle.SOLID_FOREGROUND);
	style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
	style.setBorderTop(HSSFCellStyle.BORDER_THIN);
	style.setBorderRight(HSSFCellStyle.BORDER_THIN);
	style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

	Row header = sheet.createRow(0);
	int columnIndex = 0;
	for (Map.Entry<String, String> entry : headerMap.entrySet()) {
		String headerText = StringUtils.defaultString(entry.getValue());
		Cell cell = createCell(header, columnIndex);
		cell.setCellValue(headerText);
		cell.setCellStyle(style);
		sheet.autoSizeColumn(columnIndex);
		columnIndex++;
	}

	return header;
}
 
Example 17
Source Project: poi   Source File: TestExportExcel2007.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * XSSFRichTextString.utfDecode()<br/>
 * value.contains("_x")<br/>
 * Pattern.compile("_x([0-9A-Fa-f]{4})_");
 */
private static void export2007WithStyle(String filePath) {
	try {
		OutputStream os = new FileOutputStream(filePath);
		XSSFWorkbook wb = new XSSFWorkbook();
		XSSFSheet sheet = wb.createSheet(Globals.SHEETNAME);
		XSSFCell cell = sheet.createRow(0).createCell(0);
		cell.setCellValue(TestUtil.RICH_TEXT_STRINGS[0]
				+ escape(TestUtil.REGEX + TestUtil.RICH_TEXT_STRINGS[1]
						+ TestUtil.REGEX) + TestUtil.RICH_TEXT_STRINGS[2]);
		CellStyle style = sheet.getWorkbook().createCellStyle();
		XSSFFont font = wb.createFont();
		font.setColor(IndexedColors.BLUE.index);
		style.setFont(font);
		cell.setCellStyle(style);
		// richString.applyFont(font);
		wb.write(os);
		os.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 18
Source Project: birt   Source File: Borders2ReportTest.java    License: Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testRunReport() throws BirtException, IOException {

	removeEmptyRows = false;
	InputStream inputStream = runAndRenderReport("Borders2.rptdesign", "xlsx");
	assertNotNull(inputStream);
	try {
		
		XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
		assertNotNull(workbook);
		
		assertEquals( 1, workbook.getNumberOfSheets() );
		assertEquals( "Borders Test Report 2", workbook.getSheetAt(0).getSheetName());
		
		Sheet sheet = workbook.getSheetAt(0);
		assertEquals( 4, firstNullRow(sheet));
		
		assertBorder( sheet, 1, 2, CellStyle.BORDER_MEDIUM, CellStyle.BORDER_MEDIUM, CellStyle.BORDER_MEDIUM, CellStyle.BORDER_MEDIUM );

		assertBorder( sheet, 1, 4, CellStyle.BORDER_MEDIUM, CellStyle.BORDER_MEDIUM, CellStyle.BORDER_MEDIUM, CellStyle.BORDER_MEDIUM );
		
	} finally {
		inputStream.close();
	}
}
 
Example 19
Source Project: myexcel   Source File: ThDefaultCellStyle.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle supply(Workbook workbook) {
    CellStyle style = workbook.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBorderRight(BorderStyle.THIN);
    style.setBorderTop(BorderStyle.THIN);
    style.setBorderLeft(BorderStyle.THIN);
    Font font = workbook.createFont();
    font.setBold(true);
    style.setFont(font);
    return style;
}
 
Example 20
Source Project: jeasypoi   Source File: ExcelExportStylerDefaultImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
	CellStyle style = workbook.createCellStyle();
	style.setAlignment(CellStyle.ALIGN_CENTER);
	style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	style.setDataFormat(STRING_FORMAT);
	if (isWarp) {
		style.setWrapText(true);
	}
	return style;
}
 
Example 21
Source Project: hy.common.report   Source File: ColorListener.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 对变量名称反射出来的值进行加工处理
 * 
 * @author      ZhengWei(HY)
 * @createDate  2017-06-29
 * @version     v1.0
 *
 * @param i_RTemplate     模板
 * @param i_TemplateCell  模板单元格对象
 * @param i_DataCell      数据单元格对象
 * @param i_DataWorkbook  数据工作薄对象
 * @param i_RSystemValue  系统变量信息
 * @param i_Datas         本行对应的数据
 * @param i_Value         反射出来的变量名称对应的值
 * @return 
 */
public String getValue(RTemplate i_RTemplate ,Cell i_TemplateCell ,Cell i_DataCell ,RWorkbook i_DataWorkbook ,RSystemValue i_RSystemValue ,Object i_Datas ,Object i_Value)
{
    CellStyle v_NewCellStyle = null;
    
    if ( i_DataCell.getRow().getRowNum() % 2 == 1 )
    {
        v_NewCellStyle = i_DataWorkbook.getCellStyleByCopy("奇数行" ,i_DataCell ,i_RTemplate);
        v_NewCellStyle.setFillForegroundColor(this.oddNumberolor);
    }
    else
    {
        v_NewCellStyle = i_DataWorkbook.getCellStyleByCopy("偶数行" ,i_DataCell ,i_RTemplate);
        v_NewCellStyle.setFillForegroundColor(this.evenNumberColor);
    }
    
    v_NewCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    
    Row v_Row       = i_DataCell.getRow();
    int v_CellCount = v_Row.getLastCellNum();
    for (int i=0; i<v_CellCount; i++)
    {
        v_Row.getCell(i).setCellStyle(v_NewCellStyle);
    }
    
    if ( i_Value == null )
    {
        return "";
    }
    
    return i_Value.toString();
}
 
Example 22
Source Project: myexcel   Source File: TextAlignStyle.java    License: Apache License 2.0 5 votes vote down vote up
public static void setTextAlign(CellStyle cellStyle, Map<String, String> tdStyle) {
    if (tdStyle == null) {
        return;
    }
    String textAlign = tdStyle.get(TEXT_ALIGN);
    if (horizontalAlignmentMap.containsKey(textAlign)) {
        cellStyle.setAlignment(horizontalAlignmentMap.get(textAlign));
    }
    String verticalAlign = tdStyle.get(VERTICAL_ALIGN);
    if (verticalAlignmentMap.containsKey(verticalAlign)) {
        cellStyle.setVerticalAlignment(verticalAlignmentMap.get(verticalAlign));
    }
}
 
Example 23
Source Project: sep4j   Source File: Ssio.java    License: Apache License 2.0 5 votes vote down vote up
private static <T> Row createRow(Map<String, String> headerMap,
								 T record, SepRecordType recordType, int recordIndex, Sheet sheet, int rowIndex,
								 String datumErrPlaceholder, List<DatumError> datumErrors) {
	Row row = sheet.createRow(rowIndex);
	int columnIndex = 0;

	for (Map.Entry<String, String> entry : headerMap.entrySet()) {
		boolean datumErr = false;
		String propName = entry.getKey();
		Object propValue = null;
		try {
			propValue = getProperty(record, recordType, propName);
		} catch (Exception e) {
			if (datumErrors != null) {
				DatumError de = new DatumError();
				de.setPropName(propName);
				de.setRecordIndex(recordIndex);
				de.setCause(e);
				datumErrors.add(de);
			}
			datumErr = true;
			propValue = datumErrPlaceholder;
		}
		String propValueText = (propValue == null ? null : propValue
				.toString());
		Cell cell = createCell(row, columnIndex);
		cell.setCellValue(StringUtils.defaultString(propValueText));

		if (datumErr) {
			CellStyle errStyle = sheet.getWorkbook().createCellStyle();
			errStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
			errStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
			cell.setCellStyle(errStyle);
		}

		columnIndex++;
	}

	return row;
}
 
Example 24
Source Project: jeasypoi   Source File: ExcelExportStylerColorImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle getHeaderStyle(short headerColor) {
	CellStyle titleStyle = workbook.createCellStyle();
	Font font = workbook.createFont();
	font.setFontHeightInPoints((short) 24);
	titleStyle.setFont(font);
	titleStyle.setFillForegroundColor(headerColor);
	titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
	titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	return titleStyle;
}
 
Example 25
Source Project: xlsmapper   Source File: VerticalRecordsProcessor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * ネストしたレコードの親のセルを結合する
 * @param sheet シート
 * @param mergedSize 結合するセルのサイズ
 * @param valueCellPositions 結合する開始位置のセルのアドレス
 */
private void processSavingNestedMergedRecord(final Sheet sheet, final int mergedSize,
        final List<CellPosition> valueCellPositions) {

    if(mergedSize <= 1) {
        return;
    }

    // ネストした場合、上のセルのスタイルをコピーして、結合する
    for(CellPosition position : valueCellPositions) {
        Cell valueCell = POIUtils.getCell(sheet, position);
        if(valueCell == null) {
            continue;
        }

        final CellStyle style = valueCell.getCellStyle();

        // 結合するセルに対して、上のセルのスタイルをコピーする。
        // 列を挿入するときなどに必要になるため、スタイルを設定する。
        for(int i=1; i < mergedSize; i++) {
            Cell mergedCell = POIUtils.getCell(sheet, position.getColumn() + i, position.getRow());
            mergedCell.setCellStyle(style);
            mergedCell.setCellType(CellType.BLANK);
        }

        final CellRangeAddress range = new CellRangeAddress(position.getRow(), position.getRow(),
                position.getColumn(), position.getColumn() + mergedSize -1);

        // 既に結合済みのセルがある場合、外す。
        for(int colIdx=range.getFirstColumn(); colIdx <= range.getLastColumn(); colIdx++) {
            CellRangeAddress r = POIUtils.getMergedRegion(sheet, position.getRow(), colIdx);
            if(r != null) {
                POIUtils.removeMergedRange(sheet, r);
            }
        }

        sheet.addMergedRegion(range);
    }

}
 
Example 26
Source Project: birt   Source File: StyleManagerUtils.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Convert a BIRT text alignment string into a POI CellStyle constant.
 * @param alignment
 * The BIRT alignment string.
 * @return
 * One of the CellStyle.ALIGN* constants.
 */
public short poiAlignmentFromBirtAlignment(String alignment) {
	if(CSSConstants.CSS_LEFT_VALUE.equals(alignment)) {
		return CellStyle.ALIGN_LEFT;
	}
	if(CSSConstants.CSS_RIGHT_VALUE.equals(alignment)) {
		return CellStyle.ALIGN_RIGHT;
	}
	if(CSSConstants.CSS_CENTER_VALUE.equals(alignment)) {
		return CellStyle.ALIGN_CENTER;
	}
	return CellStyle.ALIGN_GENERAL;
}
 
Example 27
Source Project: birt   Source File: CellContentHandler.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Set the style of the current cell based on the style of a BIRT element.
 * @param element
 * The BIRT element to take the style from.
 */
@SuppressWarnings("deprecation")
private void setCellStyle( StyleManager sm, Cell cell, BirtStyle birtStyle, Object value ) {
	
	if( ( StyleManagerUtils.getNumberFormat(birtStyle) == null )
			&& ( StyleManagerUtils.getDateFormat(birtStyle) == null )
			&& ( StyleManagerUtils.getDateTimeFormat(birtStyle) == null )
			&& ( StyleManagerUtils.getTimeFormat(birtStyle) == null )
			&& ( value != null )
			) {
		if( value instanceof Date ) {
			long time = ((Date)value).getTime();
			time = time - ((Date)value).getTimezoneOffset() * 60000;
			if( time % oneDay == 0 ) {
				StyleManagerUtils.setDateFormat( birtStyle, "Short Date", null);
			} else if( time < oneDay ) {
				StyleManagerUtils.setTimeFormat( birtStyle, "Short Time", null);
			} else {
				StyleManagerUtils.setDateTimeFormat( birtStyle, "General Date", null);
			}
		}
	}
	
	// log.debug( "BirtStyle: ", birtStyle);
	CellStyle cellStyle = sm.getStyle(birtStyle);
	cell.setCellStyle(cellStyle);
}
 
Example 28
Source Project: easyexcel   Source File: StyleUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Build content cell style
 *
 * @param workbook
 * @param writeCellStyle
 * @return
 */
public static CellStyle buildContentCellStyle(Workbook workbook, WriteCellStyle writeCellStyle) {
    CellStyle cellStyle = workbook.createCellStyle();
    if (writeCellStyle == null) {
        return cellStyle;
    }
    buildCellStyle(workbook, cellStyle, writeCellStyle, false);
    return cellStyle;
}
 
Example 29
Source Project: pentaho-kettle   Source File: ExcelWriterStep.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Set specified cell format
 *
 * @param excelFieldFormat the specified format
 * @param cell             the cell to set up format
 */
private void setDataFormat( String excelFieldFormat, Cell cell ) {
  if ( log.isDebug() ) {
    logDebug( BaseMessages.getString( PKG, "ExcelWriterStep.Log.SetDataFormat", excelFieldFormat, CellReference.convertNumToColString( cell.getColumnIndex() ), cell.getRowIndex() ) );
  }

  DataFormat format = data.wb.createDataFormat();
  short formatIndex = format.getFormat( excelFieldFormat );
  CellStyle style = data.wb.createCellStyle();
  style.cloneStyleFrom( cell.getCellStyle() );
  style.setDataFormat( formatIndex );
  cell.setCellStyle( style );
}
 
Example 30
Source Project: bdf3   Source File: AbstractStyleBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public CellStyle createBorderCellStyle(Workbook workbook, boolean showBorder) {
	CellStyle style = workbook.createCellStyle();
	if (showBorder) {
		style.setBorderRight(CellStyle.BORDER_THIN);
		style.setRightBorderColor(IndexedColors.BLACK.getIndex());
		style.setBorderBottom(CellStyle.BORDER_THIN);
		style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		style.setBorderLeft(CellStyle.BORDER_THIN);
		style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		style.setBorderTop(CellStyle.BORDER_THIN);
		style.setTopBorderColor(IndexedColors.BLACK.getIndex());
	}
	return style;
}