Java Code Examples for org.apache.poi.ss.usermodel.CellStyle#setBorderRight()

The following examples show how to use org.apache.poi.ss.usermodel.CellStyle#setBorderRight() . 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: UKExcelUtil.java    From youkefu with Apache License 2.0 8 votes vote down vote up
@SuppressWarnings("deprecation")
private CellStyle baseCellStyle(){
	CellStyle cellStyle = wb.createCellStyle();
	cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); 

	cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
			
	cellStyle.setWrapText(true);
	cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
	cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
	cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
	cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
	
	Font font = wb.createFont(); 
	font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
	font.setFontName("宋体"); 
	font.setFontHeight((short) 200); 
	cellStyle.setFont(font); 
	
	return cellStyle;
}
 
Example 2
Source File: ExcelExportStylerColorImpl.java    From jeasypoi with 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 3
Source File: FlatFileExtractor.java    From Open-Lowcode with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * a utility class to create style for a border around a cell
 * 
 * @param wb the workbook
 * @return the style to use to put border around a cell
 */
public static CellStyle createBorderedStyle(Workbook wb) {
	CellStyle style = wb.createCellStyle();
	style.setBorderRight(BorderStyle.THIN);
	style.setRightBorderColor(IndexedColors.BLACK.getIndex());
	style.setBorderBottom(BorderStyle.THIN);
	style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
	style.setBorderLeft(BorderStyle.THIN);
	style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
	style.setBorderTop(BorderStyle.THIN);
	style.setTopBorderColor(IndexedColors.BLACK.getIndex());
	return style;
}
 
Example 4
Source File: StyleUtil.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * @param workbook
 * @return
 */
public static CellStyle buildDefaultCellStyle(Workbook workbook) {
    CellStyle newCellStyle = workbook.createCellStyle();
    newCellStyle.setWrapText(true);
    newCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    newCellStyle.setAlignment(HorizontalAlignment.CENTER);
    newCellStyle.setLocked(true);
    newCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    newCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    newCellStyle.setBorderTop(BorderStyle.THIN);
    newCellStyle.setBorderBottom(BorderStyle.THIN);
    newCellStyle.setBorderLeft(BorderStyle.THIN);
    newCellStyle.setBorderRight(BorderStyle.THIN);
    return newCellStyle;
}
 
Example 5
Source File: ExcelExportStylerBorderImpl.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle getHeaderStyle(short color) {
	CellStyle titleStyle = workbook.createCellStyle();
	Font font = workbook.createFont();
	font.setFontHeightInPoints((short) 12);
	titleStyle.setFont(font);
	titleStyle.setBorderLeft((short) 1); // 左边框
	titleStyle.setBorderRight((short) 1); // 右边框
	titleStyle.setBorderBottom((short) 1);
	titleStyle.setBorderTop((short) 1);
	titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
	titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	return titleStyle;
}
 
Example 6
Source File: ExcelExportStylerBorderImpl.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle stringNoneStyle(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.setAlignment(CellStyle.ALIGN_CENTER);
	style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	style.setDataFormat(STRING_FORMAT);
	if (isWarp) {
		style.setWrapText(true);
	}
	return style;
}
 
Example 7
Source File: ExcelExportStylerBorderImpl.java    From autopoi with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle getHeaderStyle(short color) {
	CellStyle titleStyle = workbook.createCellStyle();
	Font font = workbook.createFont();
	font.setFontHeightInPoints((short) 12);
	titleStyle.setFont(font);
	titleStyle.setBorderLeft((short) 1); // 左边框
	titleStyle.setBorderRight((short) 1); // 右边框
	titleStyle.setBorderBottom((short) 1);
	titleStyle.setBorderTop((short) 1);
	titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
	titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	return titleStyle;
}
 
Example 8
Source File: ExcelExportStylerBorderImpl.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle getTitleStyle(short color) {
	CellStyle titleStyle = workbook.createCellStyle();
	titleStyle.setBorderLeft((short) 1); // 左边框
	titleStyle.setBorderRight((short) 1); // 右边框
	titleStyle.setBorderBottom((short) 1);
	titleStyle.setBorderTop((short) 1);
	titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
	titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	titleStyle.setWrapText(true);
	return titleStyle;
}
 
Example 9
Source File: ExcelUtil.java    From RuoYi-Vue 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 10
Source File: ExcelUtil.java    From danyuan-application with Apache License 2.0 5 votes vote down vote up
/**
 * 复制一个单元格样式到目的单元格样式
 *
 * @param fromStyle
 * @param toStyle
 */
public static void copyCellStyle(CellStyle fromStyle, CellStyle toStyle) {
	toStyle.setAlignment(fromStyle.getAlignmentEnum());
	// 边框和边框颜色
	toStyle.setBorderBottom(fromStyle.getBorderBottomEnum());
	toStyle.setBorderLeft(fromStyle.getBorderLeftEnum());
	toStyle.setBorderRight(fromStyle.getBorderRightEnum());
	toStyle.setBorderTop(fromStyle.getBorderTopEnum());
	toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
	toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
	toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
	toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());
	
	// 背景和前景
	toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
	toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());
	
	// 数据格式
	toStyle.setDataFormat(fromStyle.getDataFormat());
	toStyle.setFillPattern(fromStyle.getFillPatternEnum());
	// toStyle.setFont(fromStyle.getFont(null));
	toStyle.setHidden(fromStyle.getHidden());
	toStyle.setIndention(fromStyle.getIndention());// 首行缩进
	toStyle.setLocked(fromStyle.getLocked());
	toStyle.setRotation(fromStyle.getRotation());// 旋转
	toStyle.setVerticalAlignment(fromStyle.getVerticalAlignmentEnum());
	toStyle.setWrapText(fromStyle.getWrapText());
	
}
 
Example 11
Source File: ExcelExportStylerBorderImpl.java    From easypoi with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle getTitleStyle(short color) {
    CellStyle titleStyle = workbook.createCellStyle();
    titleStyle.setBorderLeft((short) 1); // 左边框
    titleStyle.setBorderRight((short) 1); // 右边框
    titleStyle.setBorderBottom((short) 1);
    titleStyle.setBorderTop((short) 1);
    titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
    titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    titleStyle.setWrapText(true);
    return titleStyle;
}
 
Example 12
Source File: ExcelExportStylerBorderImpl.java    From easypoi with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle stringNoneStyle(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.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setDataFormat(STRING_FORMAT);
    if (isWarp) {
        style.setWrapText(true);
    }
    return style;
}
 
Example 13
Source File: ThDefaultCellStyle.java    From myexcel with 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 14
Source File: Util.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
private static void copyCell(Cell oldCell, Cell newCell, List<CellStyle> styleList) {
	if (styleList != null) {
		if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()) {
			newCell.setCellStyle(oldCell.getCellStyle());
		} else {
			DataFormat newDataFormat = newCell.getSheet().getWorkbook().createDataFormat();

			CellStyle newCellStyle = getSameCellStyle(oldCell, newCell, styleList);
			if (newCellStyle == null) {

				Font oldFont = oldCell.getSheet().getWorkbook().getFontAt(oldCell.getCellStyle().getFontIndex());

				Font newFont = newCell.getSheet().getWorkbook().findFont(oldFont.getBoldweight(), oldFont.getColor(), oldFont.getFontHeight(),
						oldFont.getFontName(), oldFont.getItalic(), oldFont.getStrikeout(), oldFont.getTypeOffset(), oldFont.getUnderline());
				if (newFont == null) {
					newFont = newCell.getSheet().getWorkbook().createFont();
					newFont.setBoldweight(oldFont.getBoldweight());
					newFont.setColor(oldFont.getColor());
					newFont.setFontHeight(oldFont.getFontHeight());
					newFont.setFontName(oldFont.getFontName());
					newFont.setItalic(oldFont.getItalic());
					newFont.setStrikeout(oldFont.getStrikeout());
					newFont.setTypeOffset(oldFont.getTypeOffset());
					newFont.setUnderline(oldFont.getUnderline());
					newFont.setCharSet(oldFont.getCharSet());
				}

				short newFormat = newDataFormat.getFormat(oldCell.getCellStyle().getDataFormatString());
				newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
				newCellStyle.setFont(newFont);
				newCellStyle.setDataFormat(newFormat);

				newCellStyle.setAlignment(oldCell.getCellStyle().getAlignment());
				newCellStyle.setHidden(oldCell.getCellStyle().getHidden());
				newCellStyle.setLocked(oldCell.getCellStyle().getLocked());
				newCellStyle.setWrapText(oldCell.getCellStyle().getWrapText());
				newCellStyle.setBorderBottom(oldCell.getCellStyle().getBorderBottom());
				newCellStyle.setBorderLeft(oldCell.getCellStyle().getBorderLeft());
				newCellStyle.setBorderRight(oldCell.getCellStyle().getBorderRight());
				newCellStyle.setBorderTop(oldCell.getCellStyle().getBorderTop());
				newCellStyle.setBottomBorderColor(oldCell.getCellStyle().getBottomBorderColor());
				newCellStyle.setFillBackgroundColor(oldCell.getCellStyle().getFillBackgroundColor());
				newCellStyle.setFillForegroundColor(oldCell.getCellStyle().getFillForegroundColor());
				newCellStyle.setFillPattern(oldCell.getCellStyle().getFillPattern());
				newCellStyle.setIndention(oldCell.getCellStyle().getIndention());
				newCellStyle.setLeftBorderColor(oldCell.getCellStyle().getLeftBorderColor());
				newCellStyle.setRightBorderColor(oldCell.getCellStyle().getRightBorderColor());
				newCellStyle.setRotation(oldCell.getCellStyle().getRotation());
				newCellStyle.setTopBorderColor(oldCell.getCellStyle().getTopBorderColor());
				newCellStyle.setVerticalAlignment(oldCell.getCellStyle().getVerticalAlignment());

				styleList.add(newCellStyle);
			}
			newCell.setCellStyle(newCellStyle);
		}
	}
	switch (oldCell.getCellType()) {
	case Cell.CELL_TYPE_STRING:
		newCell.setCellValue(oldCell.getStringCellValue());
		break;
	case Cell.CELL_TYPE_NUMERIC:
		newCell.setCellValue(oldCell.getNumericCellValue());
		break;
	case Cell.CELL_TYPE_BLANK:
		newCell.setCellType(Cell.CELL_TYPE_BLANK);
		break;
	case Cell.CELL_TYPE_BOOLEAN:
		newCell.setCellValue(oldCell.getBooleanCellValue());
		break;
	case Cell.CELL_TYPE_ERROR:
		newCell.setCellErrorValue(oldCell.getErrorCellValue());
		break;
	case Cell.CELL_TYPE_FORMULA:
		newCell.setCellFormula(oldCell.getCellFormula());
		formulaInfoList.add(new FormulaInfo(oldCell.getSheet().getSheetName(), oldCell.getRowIndex(), oldCell.getColumnIndex(), oldCell.getCellFormula()));
		break;
	default:
		break;
	}
}
 
Example 15
Source File: StyleUtil.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
private static void buildCellStyle(Workbook workbook, CellStyle cellStyle, WriteCellStyle writeCellStyle,
    boolean isHead) {
    buildFont(workbook, cellStyle, writeCellStyle.getWriteFont(), isHead);
    if (writeCellStyle.getDataFormat() != null) {
        cellStyle.setDataFormat(writeCellStyle.getDataFormat());
    }
    if (writeCellStyle.getHidden() != null) {
        cellStyle.setHidden(writeCellStyle.getHidden());
    }
    if (writeCellStyle.getLocked() != null) {
        cellStyle.setLocked(writeCellStyle.getLocked());
    }
    if (writeCellStyle.getQuotePrefix() != null) {
        cellStyle.setQuotePrefixed(writeCellStyle.getQuotePrefix());
    }
    if (writeCellStyle.getHorizontalAlignment() != null) {
        cellStyle.setAlignment(writeCellStyle.getHorizontalAlignment());
    }
    if (writeCellStyle.getWrapped() != null) {
        cellStyle.setWrapText(writeCellStyle.getWrapped());
    }
    if (writeCellStyle.getVerticalAlignment() != null) {
        cellStyle.setVerticalAlignment(writeCellStyle.getVerticalAlignment());
    }
    if (writeCellStyle.getRotation() != null) {
        cellStyle.setRotation(writeCellStyle.getRotation());
    }
    if (writeCellStyle.getIndent() != null) {
        cellStyle.setIndention(writeCellStyle.getIndent());
    }
    if (writeCellStyle.getBorderLeft() != null) {
        cellStyle.setBorderLeft(writeCellStyle.getBorderLeft());
    }
    if (writeCellStyle.getBorderRight() != null) {
        cellStyle.setBorderRight(writeCellStyle.getBorderRight());
    }
    if (writeCellStyle.getBorderTop() != null) {
        cellStyle.setBorderTop(writeCellStyle.getBorderTop());
    }
    if (writeCellStyle.getBorderBottom() != null) {
        cellStyle.setBorderBottom(writeCellStyle.getBorderBottom());
    }
    if (writeCellStyle.getLeftBorderColor() != null) {
        cellStyle.setLeftBorderColor(writeCellStyle.getLeftBorderColor());
    }
    if (writeCellStyle.getRightBorderColor() != null) {
        cellStyle.setRightBorderColor(writeCellStyle.getRightBorderColor());
    }
    if (writeCellStyle.getTopBorderColor() != null) {
        cellStyle.setTopBorderColor(writeCellStyle.getTopBorderColor());
    }
    if (writeCellStyle.getBottomBorderColor() != null) {
        cellStyle.setBottomBorderColor(writeCellStyle.getBottomBorderColor());
    }
    if (writeCellStyle.getFillPatternType() != null) {
        cellStyle.setFillPattern(writeCellStyle.getFillPatternType());
    }
    if (writeCellStyle.getFillBackgroundColor() != null) {
        cellStyle.setFillBackgroundColor(writeCellStyle.getFillBackgroundColor());
    }
    if (writeCellStyle.getFillForegroundColor() != null) {
        cellStyle.setFillForegroundColor(writeCellStyle.getFillForegroundColor());
    }
    if (writeCellStyle.getShrinkToFit() != null) {
        cellStyle.setShrinkToFit(writeCellStyle.getShrinkToFit());
    }
}
 
Example 16
Source File: ExcelWriter_StyleFormatTest.java    From hop with Apache License 2.0 4 votes vote down vote up
/**
 * Setup the data necessary for Excel Writer transform
 *
 * @param fileType
 * @throws HopException
 */
private void createTransformData( String fileType ) throws HopException {
  transformData = new ExcelWriterData();
  transformData.inputRowMeta = transform.getInputRowMeta().clone();
  transformData.outputRowMeta = transform.getInputRowMeta().clone();

  // we don't run pipeline so ExcelWriter.processRow() doesn't get executed
  // we populate the ExcelWriterData with bare minimum required values
  //
  CellReference cellRef = new CellReference( transformMeta.getStartingCell() );
  transformData.startingRow = cellRef.getRow();
  transformData.startingCol = cellRef.getCol();
  transformData.posX = transformData.startingCol;
  transformData.posY = transformData.startingRow;

  int numOfFields = transformData.inputRowMeta.size();
  transformData.fieldnrs = new int[ numOfFields ];
  transformData.linkfieldnrs = new int[ numOfFields ];
  transformData.commentfieldnrs = new int[ numOfFields ];
  for ( int i = 0; i < numOfFields; i++ ) {
    transformData.fieldnrs[ i ] = i;
    transformData.linkfieldnrs[ i ] = -1;
    transformData.commentfieldnrs[ i ] = -1;
  }

  // we avoid reading/writing Excel files, so ExcelWriter.prepareNextOutputFile() doesn't get executed
  // create Excel workbook object
  //
  transformData.wb = transformMeta.getExtension().equalsIgnoreCase( "xlsx" ) ? new XSSFWorkbook() : new HSSFWorkbook();
  transformData.sheet = transformData.wb.createSheet();
  transformData.file = null;
  transformData.clearStyleCache( numOfFields );

  // we avoid reading template file from disk
  // so set beforehand cells with custom style and formatting
  DataFormat format = transformData.wb.createDataFormat();
  Row xlsRow = transformData.sheet.createRow( 0 );

  // Cell F1 has custom style applied, used as template
  Cell cell = xlsRow.createCell( 5 );
  CellStyle cellStyle = transformData.wb.createCellStyle();
  cellStyle.setBorderRight( BorderStyle.THICK );
  cellStyle.setFillPattern( FillPatternType.FINE_DOTS );
  cell.setCellStyle( cellStyle );

  // Cell G1 has same style, but also a custom data format
  cellStyle = transformData.wb.createCellStyle();
  cellStyle.cloneStyleFrom( cell.getCellStyle() );
  cell = xlsRow.createCell( 6 );
  cellStyle.setDataFormat( format.getFormat( "##0,000.0" ) );
  cell.setCellStyle( cellStyle );
}
 
Example 17
Source File: ExcelWriterStep_StyleFormatTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Setup the data necessary for Excel Writer step
 *
 * @param fileType
 * @throws KettleException
 */
private void createStepData( String fileType ) throws KettleException {
  stepData = new ExcelWriterStepData();
  stepData.inputRowMeta = step.getInputRowMeta().clone();
  stepData.outputRowMeta = step.getInputRowMeta().clone();

  // we don't run transformation so ExcelWriterStep.processRow() doesn't get executed
  // we populate the ExcelWriterStepData with bare minimum required values
  CellReference cellRef = new CellReference( stepMeta.getStartingCell() );
  stepData.startingRow = cellRef.getRow();
  stepData.startingCol = cellRef.getCol();
  stepData.posX = stepData.startingCol;
  stepData.posY = stepData.startingRow;

  int numOfFields = stepData.inputRowMeta.size();
  stepData.fieldnrs = new int[numOfFields];
  stepData.linkfieldnrs = new int[numOfFields];
  stepData.commentfieldnrs = new int[numOfFields];
  for ( int i = 0; i < numOfFields; i++ ) {
    stepData.fieldnrs[i] = i;
    stepData.linkfieldnrs[i] = -1;
    stepData.commentfieldnrs[i] = -1;
  }

  // we avoid reading/writing Excel files, so ExcelWriterStep.prepareNextOutputFile() doesn't get executed
  // create Excel workbook object
  stepData.wb = stepMeta.getExtension().equalsIgnoreCase( "xlsx" ) ? new XSSFWorkbook() : new HSSFWorkbook();
  stepData.sheet = stepData.wb.createSheet();
  stepData.file = null;
  stepData.clearStyleCache( numOfFields );

  // we avoid reading template file from disk
  // so set beforehand cells with custom style and formatting
  DataFormat format = stepData.wb.createDataFormat();
  Row xlsRow = stepData.sheet.createRow( 0 );

  // Cell F1 has custom style applied, used as template
  Cell cell = xlsRow.createCell( 5 );
  CellStyle cellStyle = stepData.wb.createCellStyle();
  cellStyle.setBorderRight( BorderStyle.THICK );
  cellStyle.setFillPattern( FillPatternType.FINE_DOTS );
  cell.setCellStyle( cellStyle );

  // Cell G1 has same style, but also a custom data format
  cellStyle = stepData.wb.createCellStyle();
  cellStyle.cloneStyleFrom( cell.getCellStyle() );
  cell = xlsRow.createCell( 6 );
  cellStyle.setDataFormat( format.getFormat( "##0,000.0" ) );
  cell.setCellStyle( cellStyle );
}
 
Example 18
Source File: CrosstabXLSExporter.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public CellStyle buildDataCellStyle(Sheet sheet) {
	CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
	cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
	cellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);

	String cellBGColor = (String) this.getProperty(PROPERTY_CELL_BACKGROUND_COLOR);
	logger.debug("Cell background color : " + cellBGColor);
	short backgroundColorIndex = cellBGColor != null ? IndexedColors.valueOf(cellBGColor).getIndex() : IndexedColors.valueOf(DEFAULT_CELL_BACKGROUND_COLOR)
			.getIndex();
	cellStyle.setFillForegroundColor(backgroundColorIndex);

	cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

	cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
	cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
	cellStyle.setBorderRight(CellStyle.BORDER_THIN);
	cellStyle.setBorderTop(CellStyle.BORDER_THIN);

	String bordeBorderColor = (String) this.getProperty(PROPERTY_CELL_BORDER_COLOR);
	logger.debug("Cell border color : " + bordeBorderColor);
	short borderColorIndex = bordeBorderColor != null ? IndexedColors.valueOf(bordeBorderColor).getIndex() : IndexedColors.valueOf(
			DEFAULT_CELL_BORDER_COLOR).getIndex();

	cellStyle.setLeftBorderColor(borderColorIndex);
	cellStyle.setRightBorderColor(borderColorIndex);
	cellStyle.setBottomBorderColor(borderColorIndex);
	cellStyle.setTopBorderColor(borderColorIndex);

	Font font = sheet.getWorkbook().createFont();

	Short cellFontSize = (Short) this.getProperty(PROPERTY_CELL_FONT_SIZE);
	logger.debug("Cell font size : " + cellFontSize);
	short cellFontSizeShort = cellFontSize != null ? cellFontSize.shortValue() : DEFAULT_CELL_FONT_SIZE;
	font.setFontHeightInPoints(cellFontSizeShort);

	String fontName = (String) this.getProperty(PROPERTY_FONT_NAME);
	logger.debug("Font name : " + fontName);
	fontName = fontName != null ? fontName : DEFAULT_FONT_NAME;
	font.setFontName(fontName);

	String cellColor = (String) this.getProperty(PROPERTY_CELL_COLOR);
	logger.debug("Cell color : " + cellColor);
	short cellColorIndex = cellColor != null ? IndexedColors.valueOf(cellColor).getIndex() : IndexedColors.valueOf(DEFAULT_CELL_COLOR).getIndex();
	font.setColor(cellColorIndex);

	cellStyle.setFont(font);
	return cellStyle;
}
 
Example 19
Source File: QbeXLSExporter.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public CellStyle buildCellStyle(Sheet sheet) {

		CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
		cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
		cellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);

		String cellBGColor = (String) this.getProperty(PROPERTY_CELL_BACKGROUND_COLOR);
		logger.debug("Cell background color : " + cellBGColor);
		short backgroundColorIndex = cellBGColor != null ? IndexedColors.valueOf(cellBGColor).getIndex()
				: IndexedColors.valueOf(DEFAULT_CELL_BACKGROUND_COLOR).getIndex();
		cellStyle.setFillForegroundColor(backgroundColorIndex);

		cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

		cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
		cellStyle.setBorderRight(CellStyle.BORDER_THIN);
		cellStyle.setBorderTop(CellStyle.BORDER_THIN);

		String bordeBorderColor = (String) this.getProperty(PROPERTY_CELL_BORDER_COLOR);
		logger.debug("Cell border color : " + bordeBorderColor);
		short borderColorIndex = bordeBorderColor != null ? IndexedColors.valueOf(bordeBorderColor).getIndex()
				: IndexedColors.valueOf(DEFAULT_CELL_BORDER_COLOR).getIndex();

		cellStyle.setLeftBorderColor(borderColorIndex);
		cellStyle.setRightBorderColor(borderColorIndex);
		cellStyle.setBottomBorderColor(borderColorIndex);
		cellStyle.setTopBorderColor(borderColorIndex);

		Font font = sheet.getWorkbook().createFont();

		Short cellFontSize = (Short) this.getProperty(PROPERTY_CELL_FONT_SIZE);
		logger.debug("Cell font size : " + cellFontSize);
		short cellFontSizeShort = cellFontSize != null ? cellFontSize.shortValue() : DEFAULT_CELL_FONT_SIZE;
		font.setFontHeightInPoints(cellFontSizeShort);

		String fontName = (String) this.getProperty(PROPERTY_FONT_NAME);
		logger.debug("Font name : " + fontName);
		fontName = fontName != null ? fontName : DEFAULT_FONT_NAME;
		font.setFontName(fontName);

		String cellColor = (String) this.getProperty(PROPERTY_CELL_COLOR);
		logger.debug("Cell color : " + cellColor);
		short cellColorIndex = cellColor != null ? IndexedColors.valueOf(cellColor).getIndex() : IndexedColors.valueOf(DEFAULT_CELL_COLOR).getIndex();
		font.setColor(cellColorIndex);

		cellStyle.setFont(font);
		return cellStyle;
	}
 
Example 20
Source File: QbeXLSExporter.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public CellStyle buildHeaderCellStyle(Sheet sheet) {

		CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
		cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
		cellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);

		String headerBGColor = (String) this.getProperty(PROPERTY_HEADER_BACKGROUND_COLOR);
		logger.debug("Header background color : " + headerBGColor);
		short backgroundColorIndex = headerBGColor != null ? IndexedColors.valueOf(headerBGColor).getIndex()
				: IndexedColors.valueOf(DEFAULT_HEADER_BACKGROUND_COLOR).getIndex();
		cellStyle.setFillForegroundColor(backgroundColorIndex);

		cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

		cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
		cellStyle.setBorderRight(CellStyle.BORDER_THIN);
		cellStyle.setBorderTop(CellStyle.BORDER_THIN);

		String bordeBorderColor = (String) this.getProperty(PROPERTY_HEADER_BORDER_COLOR);
		logger.debug("Header border color : " + bordeBorderColor);
		short borderColorIndex = bordeBorderColor != null ? IndexedColors.valueOf(bordeBorderColor).getIndex()
				: IndexedColors.valueOf(DEFAULT_HEADER_BORDER_COLOR).getIndex();

		cellStyle.setLeftBorderColor(borderColorIndex);
		cellStyle.setRightBorderColor(borderColorIndex);
		cellStyle.setBottomBorderColor(borderColorIndex);
		cellStyle.setTopBorderColor(borderColorIndex);

		Font font = sheet.getWorkbook().createFont();

		Short headerFontSize = (Short) this.getProperty(PROPERTY_HEADER_FONT_SIZE);
		logger.debug("Header font size : " + headerFontSize);
		short headerFontSizeShort = headerFontSize != null ? headerFontSize.shortValue() : DEFAULT_HEADER_FONT_SIZE;
		font.setFontHeightInPoints(headerFontSizeShort);

		String fontName = (String) this.getProperty(PROPERTY_FONT_NAME);
		logger.debug("Font name : " + fontName);
		fontName = fontName != null ? fontName : DEFAULT_FONT_NAME;
		font.setFontName(fontName);

		String headerColor = (String) this.getProperty(PROPERTY_HEADER_COLOR);
		logger.debug("Header color : " + headerColor);
		short headerColorIndex = bordeBorderColor != null ? IndexedColors.valueOf(headerColor).getIndex()
				: IndexedColors.valueOf(DEFAULT_HEADER_COLOR).getIndex();
		font.setColor(headerColorIndex);

		font.setBoldweight(Font.BOLDWEIGHT_BOLD);
		cellStyle.setFont(font);
		return cellStyle;
	}