org.apache.poi.xssf.usermodel.XSSFCellStyle Java Examples

The following examples show how to use org.apache.poi.xssf.usermodel.XSSFCellStyle. 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: EpidemicReport.java    From MyBox with Apache License 2.0 21 votes vote down vote up
public static List<String> writeExcelHeader(XSSFWorkbook wb, XSSFSheet sheet, List<String> extraFields) {
    try {
        List<String> columns = externalNames(extraFields);
        sheet.setDefaultColumnWidth(20);
        XSSFRow titleRow = sheet.createRow(0);
        XSSFCellStyle horizontalCenter = wb.createCellStyle();
        horizontalCenter.setAlignment(HorizontalAlignment.CENTER);
        for (int i = 0; i < columns.size(); i++) {
            XSSFCell cell = titleRow.createCell(i);
            cell.setCellValue(columns.get(i));
            cell.setCellStyle(horizontalCenter);
        }
        return columns;
    } catch (Exception e) {
        return null;
    }
}
 
Example #2
Source File: HSSFCellStyleProducer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected HSSFCellStyleKey( final XSSFCellStyle style ) {
  this.color = style.getFillForegroundColor();
  this.colorTop = style.getTopBorderColor();
  this.colorLeft = style.getLeftBorderColor();
  this.colorBottom = style.getBottomBorderColor();
  this.colorRight = style.getRightBorderColor();

  this.xColor = createColor( style.getFillBackgroundXSSFColor() );
  this.xColorTop = createColor( style.getTopBorderXSSFColor() );
  this.xColorLeft = createColor( style.getLeftBorderXSSFColor() );
  this.xColorBottom = createColor( style.getBottomBorderXSSFColor() );
  this.xColorRight = createColor( style.getRightBorderXSSFColor() );

  this.borderStrokeTop = style.getBorderTopEnum();
  this.borderStrokeLeft = style.getBorderLeftEnum();
  this.borderStrokeBottom = style.getBorderBottomEnum();
  this.borderStrokeRight = style.getBorderRightEnum();

  this.dataStyle = style.getDataFormat();
  this.font = style.getFontIndex();
  this.horizontalAlignment = style.getAlignmentEnum();
  this.verticalAlignment = style.getVerticalAlignmentEnum();
  this.wrapText = style.getWrapText();
  this.textRotation = TextRotation.getInstance( style.getRotation() );
}
 
Example #3
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 #4
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 #5
Source File: GeographyCode.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static List<String> writeExcelHeader(XSSFWorkbook wb, XSSFSheet sheet) {
    try {
        List<String> columns = externalNames();
        sheet.setDefaultColumnWidth(20);
        XSSFRow titleRow = sheet.createRow(0);
        XSSFCellStyle horizontalCenter = wb.createCellStyle();
        horizontalCenter.setAlignment(HorizontalAlignment.CENTER);
        for (int i = 0;
                i < columns.size();
                i++) {
            XSSFCell cell = titleRow.createCell(i);
            cell.setCellValue(columns.get(i));
            cell.setCellStyle(horizontalCenter);
        }
        return columns;
    } catch (Exception e) {
        return null;
    }
}
 
Example #6
Source File: StreamingSheetReader.java    From components with Apache License 2.0 6 votes vote down vote up
/**
 * Read the numeric format string out of the styles table for this cell. Stores the result in the Cell.
 *
 * @param startElement
 * @param cell
 */
void setFormatString(StartElement startElement, StreamingCell cell) {
    Attribute cellStyle = startElement.getAttributeByName(new QName("s"));
    String cellStyleString = (cellStyle != null) ? cellStyle.getValue() : null;
    XSSFCellStyle style = null;

    if (cellStyleString != null) {
        style = stylesTable.getStyleAt(Integer.parseInt(cellStyleString));
    } else if (stylesTable.getNumCellStyles() > 0) {
        style = stylesTable.getStyleAt(0);
    }

    if (style != null) {
        cell.setNumericFormatIndex(style.getDataFormat());
        String formatString = style.getDataFormatString();

        if (formatString != null) {
            cell.setNumericFormat(formatString);
        } else {
            cell.setNumericFormat(BuiltinFormats.getBuiltinFormat(cell.getNumericFormatIndex()));
        }
    } else {
        cell.setNumericFormatIndex(null);
        cell.setNumericFormat(null);
    }
}
 
Example #7
Source File: Excel2007Writer.java    From tmxeditor8 with 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 #8
Source File: StreamingSheetReader.java    From data-prep with Apache License 2.0 6 votes vote down vote up
/**
 * Read the numeric format string out of the styles table for this cell. Stores the result in the Cell.
 *
 * @param startElement
 * @param cell
 */
void setFormatString(StartElement startElement, StreamingCell cell) {
    Attribute cellStyle = startElement.getAttributeByName(new QName("s"));
    String cellStyleString = (cellStyle != null) ? cellStyle.getValue() : null;
    XSSFCellStyle style = null;

    if (cellStyleString != null) {
        style = stylesTable.getStyleAt(Integer.parseInt(cellStyleString));
    } else if (stylesTable.getNumCellStyles() > 0) {
        style = stylesTable.getStyleAt(0);
    }

    if (style != null) {
        cell.setNumericFormatIndex(style.getDataFormat());
        String formatString = style.getDataFormatString();

        if (formatString != null) {
            cell.setNumericFormat(formatString);
        } else {
            cell.setNumericFormat(BuiltinFormats.getBuiltinFormat(cell.getNumericFormatIndex()));
        }
    } else {
        cell.setNumericFormatIndex(null);
        cell.setNumericFormat(null);
    }
}
 
Example #9
Source File: StreamingSheetReader.java    From excel-streaming-reader with Apache License 2.0 6 votes vote down vote up
/**
 * Read the numeric format string out of the styles table for this cell. Stores
 * the result in the Cell.
 *
 * @param startElement
 * @param cell
 */
void setFormatString(StartElement startElement, StreamingCell cell) {
  Attribute cellStyle = startElement.getAttributeByName(new QName("s"));
  String cellStyleString = (cellStyle != null) ? cellStyle.getValue() : null;
  XSSFCellStyle style = null;

  if(cellStyleString != null) {
    style = stylesTable.getStyleAt(Integer.parseInt(cellStyleString));
  } else if(stylesTable.getNumCellStyles() > 0) {
    style = stylesTable.getStyleAt(0);
  }

  if(style != null) {
    cell.setNumericFormatIndex(style.getDataFormat());
    String formatString = style.getDataFormatString();

    if(formatString != null) {
      cell.setNumericFormat(formatString);
    } else {
      cell.setNumericFormat(BuiltinFormats.getBuiltinFormat(cell.getNumericFormatIndex()));
    }
  } else {
    cell.setNumericFormatIndex(null);
    cell.setNumericFormat(null);
  }
}
 
Example #10
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 #11
Source File: ExcelServices.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets the {@link MStyle} of the given {@link XSSFCell}.
 * 
 * @param cellthe
 *            {@link XSSFCell}
 * @return the {@link MStyle} of the given {@link XSSFCell}
 */
private MStyle getStyle(XSSFCell cell) {
    final XSSFCellStyle style = cell.getCellStyle();
    final XSSFFont font = style.getFont();
    int modifiers = 0;
    if (font.getBold()) {
        modifiers |= MStyle.FONT_BOLD;
    }
    if (font.getItalic()) {
        modifiers |= MStyle.FONT_ITALIC;
    }
    if (font.getStrikeout()) {
        modifiers |= MStyle.FONT_STRIKE_THROUGH;
    }
    if (font.getUnderline() != 0) {
        modifiers |= MStyle.FONT_UNDERLINE;
    }
    return new MStyleImpl(font.getFontName(), font.getFontHeightInPoints(), getColor(font.getXSSFColor()), null,
            modifiers);
}
 
Example #12
Source File: PackageServletHandler.java    From urule with Apache License 2.0 5 votes vote down vote up
private void buildSheet(SXSSFWorkbook wb,VariableCategory vc,XSSFCellStyle style){
	String name=vc.getName();
	Sheet sheet=wb.createSheet(name);
	Row row=sheet.createRow(0);
	List<Variable> variables=vc.getVariables();
	for(int i=0;i<variables.size();i++){
		sheet.setColumnWidth(i,4000);
		Cell cell=row.createCell(i);
		Variable var=variables.get(i);
		cell.setCellValue(var.getLabel());
		cell.setCellStyle(style);
	}
}
 
Example #13
Source File: ColorUtils.java    From Octopus with MIT License 5 votes vote down vote up
public static void setBorderColor(Workbook workbook, CellStyle cellStyle, Color[] color) {
    if (color == null) {
        return;
    }
    if (cellStyle instanceof XSSFCellStyle) {
        ((XSSFCellStyle) cellStyle).setTopBorderColor(new XSSFColor(color[0]));
        ((XSSFCellStyle) cellStyle).setRightBorderColor(new XSSFColor(color[1]));
        ((XSSFCellStyle) cellStyle).setBottomBorderColor(new XSSFColor(color[2]));
        ((XSSFCellStyle) cellStyle).setLeftBorderColor(new XSSFColor(color[3]));
    } else if (cellStyle instanceof HSSFCellStyle && workbook instanceof HSSFWorkbook) {
        cellStyle.setTopBorderColor(getSimilarColor((HSSFWorkbook) workbook, color[0]).getIndex());
        cellStyle.setRightBorderColor(getSimilarColor((HSSFWorkbook) workbook, color[1]).getIndex());
        cellStyle.setBottomBorderColor(getSimilarColor((HSSFWorkbook) workbook, color[2]).getIndex());
        cellStyle.setLeftBorderColor(getSimilarColor((HSSFWorkbook) workbook, color[3]).getIndex());
    } else {
        log.error("unknown font type");
    }
}
 
Example #14
Source File: BackgroundStyle.java    From myexcel with Apache License 2.0 5 votes vote down vote up
private static void setCustomColor(CellStyle style, int[] rgb, CustomColor customColor) {
    if (rgb == null) {
        return;
    }
    XSSFCellStyle xssfCellStyle = (XSSFCellStyle) style;
    xssfCellStyle.setFillForegroundColor(new XSSFColor(new Color(rgb[0], rgb[1], rgb[2]), customColor.getDefaultIndexedColorMap()));
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
 
Example #15
Source File: TitleStyleBuilder.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
private XSSFCellStyle createXSSFCellStyle(Workbook wb, int[] bgColor, int[] fontColor, int fontSize) {
	SXSSFWorkbook workbook = (SXSSFWorkbook) wb;
	XSSFFont titleFont = (XSSFFont) workbook.createFont();
	titleFont.setCharSet(HSSFFont.DEFAULT_CHARSET);
	titleFont.setFontName("宋体");

	XSSFColor color9 = new XSSFColor(new java.awt.Color(fontColor[0], fontColor[1], fontColor[2]));
	XSSFColor color10 = new XSSFColor(new java.awt.Color(bgColor[0], bgColor[1], bgColor[2]));
	
	if (!(fontColor[0] == 0 && fontColor[1] == 0 && fontColor[2] == 0)) {
		titleFont.setColor(color9);
	}
	titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
	titleFont.setFontHeightInPoints((short) fontSize);

	XSSFCellStyle titleStyle = (XSSFCellStyle) createBorderCellStyle(workbook, true);
	titleStyle.setFont(titleFont);
	titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
	titleStyle.setFillForegroundColor(color10);
	titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
	titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

	return titleStyle;
}
 
Example #16
Source File: ExcelUtil.java    From roncoo-jui-springboot with Apache License 2.0 5 votes vote down vote up
/**
 * 设置表头的单元格样式
 * 
 * @return
 */
public XSSFCellStyle getHeadStyle() {
	// 创建单元格样式
	XSSFCellStyle cellStyle = wb.createCellStyle();
	// 设置单元格的背景颜色为淡蓝色
	cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
	// 创建单元格内容显示不下时自动换行
	//cellStyle.setWrapText(true);
	// 设置单元格字体样式
	XSSFFont font = wb.createFont();
	// 设置字体加粗
	font.setFontName("宋体");
	font.setFontHeight((short) 200);
	cellStyle.setFont(font);
	return cellStyle;
}
 
Example #17
Source File: ExcelUtil.java    From roncoo-jui-springboot with Apache License 2.0 5 votes vote down vote up
/**
 * 设置表体的单元格样式
 * 
 * @return
 */
public XSSFCellStyle getBodyStyle() {
	// 创建单元格样式
	XSSFCellStyle cellStyle = wb.createCellStyle();
	// 创建单元格内容显示不下时自动换行
	//cellStyle.setWrapText(true);
	// 设置单元格字体样式
	XSSFFont font = wb.createFont();
	// 设置字体加粗
	font.setFontName("宋体");
	font.setFontHeight((short) 200);
	cellStyle.setFont(font);
	return cellStyle;
}
 
Example #18
Source File: FileUtil.java    From JavaWeb with Apache License 2.0 5 votes vote down vote up
public static XSSFCellStyle setStyle(XSSFWorkbook workbook) {  
    //设置字体;  
    XSSFFont font = workbook.createFont();  
    //设置字体大小;  
    font.setFontHeightInPoints((short) 20);  
    //设置字体名字;  
    font.setFontName("Courier New");  
    //font.setItalic(true);  
    //font.setStrikeout(true);  
    //设置样式;  
    XSSFCellStyle style = workbook.createCellStyle();  
    //设置底边框;  
    style.setBorderBottom(XSSFCellStyle.BORDER_THIN);  
    //设置底边框颜色;  
    style.setBottomBorderColor(new XSSFColor(Color.BLACK));  
    //设置左边框;  
    style.setBorderLeft(XSSFCellStyle.BORDER_THIN);  
    //设置左边框颜色;  
    style.setLeftBorderColor(new XSSFColor(Color.BLACK));  
    //设置右边框;  
    style.setBorderRight(XSSFCellStyle.BORDER_THIN);  
    //设置右边框颜色;  
    style.setRightBorderColor(new XSSFColor(Color.BLACK));  
    //设置顶边框;  
    style.setBorderTop(XSSFCellStyle.BORDER_THIN);  
    //设置顶边框颜色;  
    style.setTopBorderColor(new XSSFColor(Color.BLACK));  
    //在样式用应用设置的字体;  
    style.setFont(font);  
    //设置自动换行;  
    style.setWrapText(false);  
    //设置水平对齐的样式为居中对齐;  
    style.setAlignment(XSSFCellStyle.ALIGN_CENTER);  
    //设置垂直对齐的样式为居中对齐;  
    style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);  
    return style;  
}
 
Example #19
Source File: FileUtil.java    From JavaWeb with Apache License 2.0 5 votes vote down vote up
public static XSSFCellStyle setStyle(XSSFWorkbook workbook) {  
    //设置字体;  
    XSSFFont font = workbook.createFont();  
    //设置字体大小;  
    font.setFontHeightInPoints((short) 20);  
    //设置字体名字;  
    font.setFontName("Courier New");  
    //font.setItalic(true);  
    //font.setStrikeout(true);  
    //设置样式;  
    XSSFCellStyle style = workbook.createCellStyle();  
    //设置底边框;  
    style.setBorderBottom(XSSFCellStyle.BORDER_THIN);  
    //设置底边框颜色;  
    style.setBottomBorderColor(new XSSFColor(Color.BLACK));  
    //设置左边框;  
    style.setBorderLeft(XSSFCellStyle.BORDER_THIN);  
    //设置左边框颜色;  
    style.setLeftBorderColor(new XSSFColor(Color.BLACK));  
    //设置右边框;  
    style.setBorderRight(XSSFCellStyle.BORDER_THIN);  
    //设置右边框颜色;  
    style.setRightBorderColor(new XSSFColor(Color.BLACK));  
    //设置顶边框;  
    style.setBorderTop(XSSFCellStyle.BORDER_THIN);  
    //设置顶边框颜色;  
    style.setTopBorderColor(new XSSFColor(Color.BLACK));  
    //在样式用应用设置的字体;  
    style.setFont(font);  
    //设置自动换行;  
    style.setWrapText(false);  
    //设置水平对齐的样式为居中对齐;  
    style.setAlignment(XSSFCellStyle.ALIGN_CENTER);  
    //设置垂直对齐的样式为居中对齐;  
    style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);  
    return style;  
}
 
Example #20
Source File: ExcelNodeSerializer.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void startSerialize( RootNode rootNode, OutputStream outputStream ) throws Exception
{
    workbook = new XSSFWorkbook();
    sheet = workbook.createSheet( "Sheet1" );

    XSSFFont boldFont = workbook.createFont();
    boldFont.setBold( true );

    XSSFCellStyle boldCellStyle = workbook.createCellStyle();
    boldCellStyle.setFont( boldFont );

    // build schema
    for ( Node child : rootNode.getChildren() )
    {
        if ( child.isCollection() )
        {
            if ( !child.getChildren().isEmpty() )
            {
                Node node = child.getChildren().get( 0 );

                XSSFRow row = sheet.createRow( 0 );

                int cellIdx = 0;

                for ( Node property : node.getChildren() )
                {
                    if ( property.isSimple() )
                    {
                        XSSFCell cell = row.createCell( cellIdx++ );
                        cell.setCellValue( property.getName() );
                        cell.setCellStyle( boldCellStyle );
                    }
                }
            }
        }
    }
}
 
Example #21
Source File: PackageServletHandler.java    From urule with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void exportExcelTemplate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
	List<VariableCategory> variableCategories=(List<VariableCategory>)httpSessionKnowledgeCache.get(req, VCS_KEY);
	if(variableCategories==null){
		KnowledgeBase knowledgeBase=buildKnowledgeBase(req);
		variableCategories=knowledgeBase.getResourceLibrary().getVariableCategories();
	}
	SXSSFWorkbook wb = new SXSSFWorkbook();
	XSSFCellStyle style=(XSSFCellStyle)wb.createCellStyle();
	Color c=new Color(147,208,15);
	XSSFColor xssfColor=new XSSFColor(c);
	style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	style.setFillForegroundColor(xssfColor);
	for(VariableCategory vc:variableCategories){
		buildSheet(wb, vc,style);
	}
	resp.setContentType("application/x-xls");
	resp.setHeader("Content-Disposition","attachment; filename=urule-batch-test-template.xlsx");
	OutputStream outputStream=resp.getOutputStream();
	wb.write(outputStream);;
	outputStream.flush();
	outputStream.close();
}
 
Example #22
Source File: ColorUtils.java    From Octopus with MIT License 5 votes vote down vote up
public static void setForegroundColor(Workbook workbook, CellStyle cellStyle, Color color) {
    if (color == null) {
        return;
    }
    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    if (cellStyle instanceof XSSFCellStyle) {
        ((XSSFCellStyle) cellStyle).setFillForegroundColor(new XSSFColor(color));
    } else if (cellStyle instanceof HSSFCellStyle && workbook instanceof HSSFWorkbook) {
        cellStyle.setFillForegroundColor(getSimilarColor((HSSFWorkbook) workbook, color).getIndex());
    } else {
        log.error("unknown font type");
    }
}
 
Example #23
Source File: Scenario.java    From NoraUi with GNU Affero General Public License v3.0 5 votes vote down vote up
private void setHeaderStyleInXlsxFile(XSSFCellStyle noraUiColumnStyle, XSSFCellStyle noraUiResultColumnStyle, Object field, Cell cell) {
    if ("Result".equals(field)) {
        cell.setCellStyle(noraUiResultColumnStyle);
    } else {
        cell.setCellStyle(noraUiColumnStyle);
    }
}
 
Example #24
Source File: Scenario.java    From NoraUi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @param scenarioName
 *            name of scenario.
 * @param excelPath
 */
private void addXlsxFile(String scenarioName, String excelPath) {
    try (FileOutputStream outputStream = new FileOutputStream(excelPath); XSSFWorkbook workbook = new XSSFWorkbook()) {
        XSSFCellStyle noraUiColumnStyle = workbook.createCellStyle();
        XSSFFont noraUiColumnFont = workbook.createFont();
        noraUiColumnFont.setColor(IndexedColors.BLACK.getIndex());
        noraUiColumnFont.setBold(true);
        noraUiColumnStyle.setFont(noraUiColumnFont);
        noraUiColumnStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(0, 96, 88)));
        noraUiColumnStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        XSSFCellStyle noraUiResultColumnStyle = workbook.createCellStyle();
        XSSFFont noraUiResultColumnFont = workbook.createFont();
        noraUiResultColumnFont.setColor(IndexedColors.WHITE.getIndex());
        noraUiResultColumnFont.setBold(false);
        noraUiResultColumnStyle.setFont(noraUiResultColumnFont);
        noraUiResultColumnStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 128, 128)));
        noraUiResultColumnStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        XSSFSheet sheet = workbook.createSheet("NoraUi-" + scenarioName);
        Object[][] datas = { { "user", "password", "Result" }, { "user1", "password1" }, { "user2", "password2" } };
        int rowNum = 0;
        for (int i = 0; i < datas.length; i++) {
            Object[] data = datas[i];
            Row row = sheet.createRow(rowNum++);
            int colNum = 0;
            for (Object field : data) {
                Cell cell = row.createCell(colNum++);
                if (i == 0) {
                    setHeaderStyleInXlsxFile(noraUiColumnStyle, noraUiResultColumnStyle, field, cell);
                }
                setRowValueInXlsxFile(field, cell);
            }
        }
        workbook.write(outputStream);
    } catch (IOException e) {
        log.error("IOException {}", e.getMessage(), e);
    }
}
 
Example #25
Source File: StyleManagerXUtils.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void applyBorderStyle(Workbook workbook, CellStyle style, BorderSide side, CSSValue colour, CSSValue borderStyle, CSSValue width) {
	if( ( colour != null ) || ( borderStyle != null ) || ( width != null ) ) {
		String colourString = colour == null ? "rgb(0,0,0)" : colour.getCssText();
		String borderStyleString = borderStyle == null ? "solid" : borderStyle.getCssText();
		String widthString = width == null ? "medium" : width.getCssText();
		
		if( style instanceof XSSFCellStyle ) {
			XSSFCellStyle xStyle = (XSSFCellStyle)style;
			
			BorderStyle xBorderStyle = poiBorderStyleFromBirt(borderStyleString, widthString);
			XSSFColor xBorderColour = getXColour(colourString);
			if(xBorderStyle != BorderStyle.NONE) {
				switch( side ) {
				case TOP:
					xStyle.setBorderTop(xBorderStyle);
					xStyle.setTopBorderColor(xBorderColour);
					// log.debug( "Top border: " + xStyle.getBorderTop() + " / " + xStyle.getTopBorderXSSFColor().getARGBHex() );
					break;
				case LEFT:
					xStyle.setBorderLeft(xBorderStyle);
					xStyle.setLeftBorderColor(xBorderColour);
					// log.debug( "Left border: " + xStyle.getBorderLeft() + " / " + xStyle.getLeftBorderXSSFColor().getARGBHex() );
					break;
				case RIGHT:
					xStyle.setBorderRight(xBorderStyle);
					xStyle.setRightBorderColor(xBorderColour);
					// log.debug( "Right border: " + xStyle.getBorderRight() + " / " + xStyle.getRightBorderXSSFColor().getARGBHex() );
					break;
				case BOTTOM:
					xStyle.setBorderBottom(xBorderStyle);
					xStyle.setBottomBorderColor(xBorderColour);
					// log.debug( "Bottom border: " + xStyle.getBorderBottom() + " / " + xStyle.getBottomBorderXSSFColor().getARGBHex() );
					break;
				}
			}
		}
	}
}
 
Example #26
Source File: CellTagHandler.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Override
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) {
    XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
    xlsxReadSheetHolder.setColumnIndex(PositionUtils.getCol(attributes.getValue(ExcelXmlConstants.ATTRIBUTE_R),
        xlsxReadSheetHolder.getColumnIndex()));

    // t="s" ,it's means String
    // t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml'
    // t="inlineStr" ,it's means String
    // t="b" ,it's means Boolean
    // t="e" ,it's means Error
    // t="n" ,it's means Number
    // t is null ,it's means Empty or Number
    CellDataTypeEnum type = CellDataTypeEnum.buildFromCellType(attributes.getValue(ExcelXmlConstants.ATTRIBUTE_T));
    xlsxReadSheetHolder.setTempCellData(new CellData(type));
    xlsxReadSheetHolder.setTempData(new StringBuilder());

    // Put in data transformation information
    String dateFormatIndex = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_S);
    Integer dateFormatIndexInteger;
    if (StringUtils.isEmpty(dateFormatIndex)) {
        dateFormatIndexInteger = DEFAULT_FORMAT_INDEX;
    } else {
        dateFormatIndexInteger = Integer.parseInt(dateFormatIndex);
    }
    XSSFCellStyle xssfCellStyle =
        xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable().getStyleAt(dateFormatIndexInteger);
    int dataFormat = xssfCellStyle.getDataFormat();
    xlsxReadSheetHolder.getTempCellData().setDataFormat(dataFormat);
    xlsxReadSheetHolder.getTempCellData().setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat,
        xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale()));
}
 
Example #27
Source File: FontStyle.java    From excel-io with MIT License 5 votes vote down vote up
@Override
public void accept(final CellStyle style) {
    final Font font = ((XSSFCellStyle) style).getFont();
    this.props.forEach(
        prop -> prop.accept(font)
    );
    style.setFont(font);
}
 
Example #28
Source File: ExcelUtils.java    From mySpringBoot with Apache License 2.0 5 votes vote down vote up
/**
 * 设置边框
 *
 * @param style
 * @param border
 * @param color
 */
private static void setBorder(XSSFCellStyle style, BorderStyle border, XSSFColor color) {
    style.setBorderTop(border);
    style.setBorderLeft(border);
    style.setBorderRight(border);
    style.setBorderBottom(border);
    style.setBorderColor(BorderSide.TOP, color);
    style.setBorderColor(BorderSide.LEFT, color);
    style.setBorderColor(BorderSide.RIGHT, color);
    style.setBorderColor(BorderSide.BOTTOM, color);
}
 
Example #29
Source File: ExcelUtils.java    From mySpringBoot with Apache License 2.0 5 votes vote down vote up
/**
 * 设置内容
 *
 * @param wb
 * @param sheet
 * @param rows
 * @param rowIndex
 * @return
 */
private static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, List<List<Object>> rows, int rowIndex) {
    int colIndex;
    Font dataFont = wb.createFont();
    dataFont.setFontName("simsun");
    dataFont.setFontHeightInPoints((short) 14);
    dataFont.setColor(IndexedColors.BLACK.index);

    XSSFCellStyle dataStyle = wb.createCellStyle();
    dataStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
    dataStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
    dataStyle.setFont(dataFont);
    setBorder(dataStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
    for (List<Object> rowData : rows) {
        Row dataRow = sheet.createRow(rowIndex);
        dataRow.setHeightInPoints(25);
        colIndex = 0;
        for (Object cellData : rowData) {
            Cell cell = dataRow.createCell(colIndex);
            if (cellData != null) {
                cell.setCellValue(cellData.toString());
            } else {
                cell.setCellValue("");
            }
            cell.setCellStyle(dataStyle);
            colIndex++;
        }
        rowIndex++;
    }
    return rowIndex;
}
 
Example #30
Source File: ExcelUtils.java    From mySpringBoot with Apache License 2.0 5 votes vote down vote up
/**
 * 设置表头
 *
 * @param wb
 * @param sheet
 * @param titles
 * @return
 */
private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, List<String> titles) {
    int rowIndex = 0;
    int colIndex = 0;
    Font titleFont = wb.createFont();
    //设置字体
    titleFont.setFontName("simsun");
    //设置粗体
    titleFont.setBoldweight(Short.MAX_VALUE);
    //设置字号
    titleFont.setFontHeightInPoints((short) 14);
    //设置颜色
    titleFont.setColor(IndexedColors.BLACK.index);
    XSSFCellStyle titleStyle = wb.createCellStyle();
    //水平居中
    titleStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
    //垂直居中
    titleStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
    //设置图案颜色
    titleStyle.setFillForegroundColor(new XSSFColor(new Color(182, 184, 192)));
    //设置图案样式
    titleStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    titleStyle.setFont(titleFont);
    setBorder(titleStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
    Row titleRow = sheet.createRow(rowIndex);
    titleRow.setHeightInPoints(25);
    colIndex = 0;
    for (String field : titles) {
        Cell cell = titleRow.createCell(colIndex);
        cell.setCellValue(field);
        cell.setCellStyle(titleStyle);
        colIndex++;
    }
    rowIndex++;
    return rowIndex;
}