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

The following examples show how to use org.apache.poi.ss.usermodel.HorizontalAlignment. 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: ExcelExportSuper.java    From phone with Apache License 2.0 7 votes vote down vote up
/**
 * 表头条件
 * @param sheet
 * @param t
 * @param cellCount
 * @return
 */
void writeCondtions(HSSFSheet sheet){
	T t = getConditions();
	if (t!=null) {
		HSSFRow row = sheet.createRow(getRowNumber());
		row.setHeight((short) 500);
		CellRangeAddress cra = new CellRangeAddress(getRowNumber(), getRowNumber(), 0, getColumnJson().size());
		sheet.addMergedRegion(cra);
		HSSFCell cell = row.createCell(0);
		HSSFCellStyle style = cell.getCellStyle();
		style.setAlignment(HorizontalAlignment.CENTER);
		style.setVerticalAlignment(VerticalAlignment.CENTER);
		style.setWrapText(true);
		cell.setCellStyle(style);
		setCellValue(cell, formatCondition(t));
		addRowNumber();
	}
}
 
Example #3
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 #4
Source File: WorkbookContext.java    From Octopus with MIT License 6 votes vote down vote up
public CellStyle getHeaderStyle(Field field) {
    CellStyle style = headerStyleMap.get(field);
    if (style == null) {
        style = book.createCellStyle();
        Font font = book.createFont();
        FieldCellStyle fieldCellStyle = field.getHeaderFieldCellStyle();


        font.setFontHeightInPoints(fieldCellStyle.getFontSize());
        font.setBold(fieldCellStyle.isBold());
        ColorUtils.setColor(book, font, fieldCellStyle.getColor());
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        ColorUtils.setForegroundColor(book, style, fieldCellStyle.getForegroundColor());

        setStyleBorder(style, fieldCellStyle.getBorder());
        ColorUtils.setBorderColor(book, style, fieldCellStyle.getBorderColor());
        cellStyleMap.put(field, style);
        return style;
    }
    return style;
}
 
Example #5
Source File: WorkbookContext.java    From Octopus with 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 File: ExportTimetableXLS.java    From unitime with Apache License 2.0 6 votes vote down vote up
protected CellStyle getHeaderIntervalStyle(P p) {
	String styleId = "header-interval";
	CellStyle style = iStyles.get(styleId);
	if (style == null) {
		style = iWorkbook.createCellStyle();
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
        if (p.getWidth() == 1) {
        	style.setBorderRight(BorderStyle.THIN);
        	style.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
        }
        style.setFont(getFont(p));
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.TOP);
        style.setWrapText(true);
        iStyles.put(styleId, style);
	}
	return style;
}
 
Example #7
Source File: ExportTimetableXLS.java    From unitime with Apache License 2.0 6 votes vote down vote up
protected CellStyle getGridNameStyle(P p) {
	String styleId = "grid-name";
	CellStyle style = iStyles.get(styleId);
	if (style == null) {
		style = iWorkbook.createCellStyle();
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setFont(getFont(p));
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.TOP);
        style.setWrapText(true);
        iStyles.put(styleId, style);
	}
	return style;
}
 
Example #8
Source File: XLSPrinter.java    From unitime with Apache License 2.0 6 votes vote down vote up
protected CellStyle getStyle(A f, boolean dashed, String format) {
	String styleId = (dashed ? "D" : "")
			+ (f.has(F.BOLD) ? "b" : "") + (f.has(F.ITALIC) ? "i" : "") + (f.has(F.UNDERLINE) ? "u" : "")
			+ (f.has(F.RIGHT) ? "R" : f.has(F.CENTER) ? "C" : "L")
			+ (f.hasColor() ? "#" + Integer.toHexString(f.getColor().getRGB()) : "")
			+ (format == null ? "" : "|" + format);
	CellStyle style = iStyles.get(styleId);
	if (style == null) {
		style = iWorkbook.createCellStyle();
		if (dashed) {
			style.setBorderTop(BorderStyle.DASHED);
	        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
		}
		style.setAlignment(f.has(F.RIGHT) ? HorizontalAlignment.RIGHT : f.has(F.CENTER) ? HorizontalAlignment.CENTER : HorizontalAlignment.LEFT);
		style.setVerticalAlignment(VerticalAlignment.TOP);
		style.setFont(getFont(f.has(F.BOLD), f.has(F.ITALIC), f.has(F.UNDERLINE), f.getColor()));
       	style.setWrapText(true);
       	if (format != null)
       		style.setDataFormat(iWorkbook.createDataFormat().getFormat(format));
       	iStyles.put(styleId, style);
	}
	return style;
}
 
Example #9
Source File: Over23CandidacyProcessDA.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 #10
Source File: Over23CandidacyProcessDA.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 #11
Source File: SpreadsheetCellTest.java    From taro with MIT License 6 votes vote down vote up
@Test
public void cellsStyle_IsNullUntilSet() {
    SpreadsheetCell cell = getCell();
    
    assertThat(cell.getStyle())
            .isNull();

    SpreadsheetCellStyle cellStyle = new SpreadsheetCellStyle().withAlign(HorizontalAlignment.CENTER);
    cell.setStyle(cellStyle);

    assertThat(cell.getStyle())
            .isNotNull();

    assertThat(cell.getStyle())
            .isEqualTo(cellStyle);
}
 
Example #12
Source File: ExcelCellStyleBuilderTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testElementStyleSet() {
  when( workbook.createCellStyle() ).thenReturn( xlsStyle );
  ExcelCellStyleBuilder builder = new ExcelCellStyleBuilder( workbook );
  when( workbook.createCellStyle() ).thenReturn( xlsStyle );

  when( styleKey.getHorizontalAlignment() ).thenReturn( HorizontalAlignment.CENTER );
  when( styleKey.getVerticalAlignment() ).thenReturn( VerticalAlignment.CENTER );
  when( styleKey.isWrapText() ).thenReturn( true );
  when( workbook.getFontAt( anyShort() ) ).thenReturn( font );
  when( styleKey.getIndention() ).thenReturn( (short) 15 );

  when( styleKey.getDataStyle() ).thenReturn( (short) -1 );

  builder.withElementStyle( mock( StyleSheet.class ), styleKey );

  verify( xlsStyle, times( 1 ) ).setAlignment( eq( HorizontalAlignment.CENTER ) );
  verify( xlsStyle, times( 1 ) ).setVerticalAlignment( eq( VerticalAlignment.CENTER ) );
  verify( xlsStyle, times( 1 ) ).setFont( any() );
  verify( xlsStyle, times( 1 ) ).setWrapText( eq( true ) );
  verify( xlsStyle, times( 1 ) ).setIndention( eq( (short) 15 ) );
  verify( xlsStyle, times( 0 ) ).setDataFormat( anyShort() );
}
 
Example #13
Source File: SpreadsheetTabTest.java    From taro with MIT License 6 votes vote down vote up
@Test
public void setValue_SetsBothValueAndStyle() {
    SpreadsheetTab tab = getSpreadsheetTab();

    tab.setValue(0, 0, "one");

    assertThat(tab.getCell(0, 0).getValue())
            .isEqualTo("one");

    tab.setValue("B2", "two");

    assertThat(tab.getCell("B2").getValue())
            .isEqualTo("two");

    tab.setValue(2, 2, "three", CENTER);
    assertThat(tab.getCell(2, 2).getValue())
            .isEqualTo("three");

    assertThat(tab.getCell(2, 2).getPoiCell().getCellStyle().getAlignmentEnum())
            .isEqualTo(HorizontalAlignment.CENTER);
}
 
Example #14
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 #15
Source File: Style.java    From java-master with Apache License 2.0 6 votes vote down vote up
Style(String fontName, int fontColor, boolean fontBold, int fontUnderline, boolean fontItalic, int fontHeightInPoints,
      int foregroundColor, int backgroundColor, boolean needLeftBorder, boolean needRightBorder, boolean needTopBorder,
      boolean needBottomBorder, int borderColor, BorderStyle borderWeight, boolean autoLineFeed, HorizontalAlignment alignment,
      VerticalAlignment verticalAlignment, String datePattern, String decimalPattern) {
    this.fontName = fontName;
    this.fontColor = fontColor;
    this.fontBold = fontBold;
    this.fontUnderline = fontUnderline;
    this.fontItalic = fontItalic;
    this.fontHeightInPoints = fontHeightInPoints;
    this.foregroundColor = foregroundColor;
    this.backgroundColor = backgroundColor;
    this.needLeftBorder = needLeftBorder;
    this.needRightBorder = needRightBorder;
    this.needTopBorder = needTopBorder;
    this.needBottomBorder = needBottomBorder;
    this.borderColor = borderColor;
    this.borderWeight = borderWeight;
    this.autoLineFeed = autoLineFeed;
    this.alignment = alignment;
    this.verticalAlignment = verticalAlignment;
    this.datePattern = datePattern;
    this.decimalPattern = decimalPattern;
}
 
Example #16
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 #17
Source File: SpreadsheetCellStyleTest.java    From taro with MIT License 5 votes vote down vote up
@Test
public void SpreadsheetCellStyle_IsImmutable() {
    SpreadsheetCellStyle cellStyle = new SpreadsheetCellStyle();
    assertThat(cellStyle.withBold(true), is(not(cellStyle)));
    assertThat(cellStyle.withDoubleUnderline(true), is(not(cellStyle)));
    assertThat(cellStyle.withFontName("Courier"), is(not(cellStyle)));
    assertThat(cellStyle.withFontOffset(1), is(not(cellStyle)));
    assertThat(cellStyle.withFontSizeInPoints(14), is(not(cellStyle)));
    assertThat(cellStyle.withItalic(true), is(not(cellStyle)));
    assertThat(cellStyle.withStrikeout(true), is(not(cellStyle)));
    assertThat(cellStyle.withUnderline(true), is(not(cellStyle)));

    assertThat(cellStyle.withAlign(HorizontalAlignment.CENTER), is(not(cellStyle)));
    assertThat(cellStyle.withBackgroundColor(Color.BLUE), is(not(cellStyle)));
    assertThat(cellStyle.withBottomBorder(BorderStyle.MEDIUM), is(not(cellStyle)));
    assertThat(cellStyle.withBottomBorderColor(Color.BLUE), is(not(cellStyle)));
    assertThat(cellStyle.withLeftBorder(BorderStyle.MEDIUM), is(not(cellStyle)));
    assertThat(cellStyle.withLeftBorderColor(Color.BLUE), is(not(cellStyle)));
    assertThat(cellStyle.withTopBorder(BorderStyle.MEDIUM), is(not(cellStyle)));
    assertThat(cellStyle.withTopBorderColor(Color.BLUE), is(not(cellStyle)));
    assertThat(cellStyle.withRightBorder(BorderStyle.MEDIUM), is(not(cellStyle)));
    assertThat(cellStyle.withRightBorderColor(Color.BLUE), is(not(cellStyle)));
    assertThat(cellStyle.withDataFormatString("0.00"), is(not(cellStyle)));
    assertThat(cellStyle.withIndention(1), is(not(cellStyle)));
    assertThat(cellStyle.withLocked(true), is(not(cellStyle)));
    assertThat(cellStyle.withRotation(1), is(not(cellStyle)));
    assertThat(cellStyle.withVerticalAlign(VerticalAlignment.TOP), is(not(cellStyle)));
    assertThat(cellStyle.withWrapText(true), is(not(cellStyle)));

    assertThat(cellStyle.copy(), not(sameInstance(cellStyle)));
    assertThat(cellStyle.apply(new SpreadsheetCellStyle()), not(sameInstance(cellStyle)));
}
 
Example #18
Source File: HSSFCellStyleProducer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Converts the given element alignment into one of the HorizontalAlignment-constants.
 *
 * @param e the JFreeReport element alignment.
 * @return the HorizontalAlignment-Alignment.
 * @throws IllegalArgumentException if an Unknown JFreeReport alignment is given.
 */
protected static HorizontalAlignment convertHorizontalAlignment( final ElementAlignment e ) {
  if ( ElementAlignment.LEFT.equals( e ) ) {
    return HorizontalAlignment.LEFT;
  } else if ( ElementAlignment.RIGHT.equals( e ) ) {
    return HorizontalAlignment.RIGHT;
  } else if ( ElementAlignment.JUSTIFY.equals( e ) ) {
    return HorizontalAlignment.JUSTIFY;
  } else if ( ElementAlignment.CENTER.equals( e ) ) {
    return HorizontalAlignment.CENTER;
  }

  throw new IllegalArgumentException( "Invalid alignment" );
}
 
Example #19
Source File: ExcelCellStyleBuilderTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void testNullElementSttle() {
  ExcelCellStyleBuilder builder = new ExcelCellStyleBuilder( workbook );

  when( workbook.createCellStyle() ).thenReturn( xlsStyle );
  builder.withElementStyle( null, styleKey );

  verify( xlsStyle, times( 0 ) ).setAlignment( any( HorizontalAlignment.class ) );
  verify( xlsStyle, times( 0 ) ).setVerticalAlignment( any( VerticalAlignment.class ) );
  verify( xlsStyle, times( 0 ) ).setFont( any() );
  verify( xlsStyle, times( 0 ) ).setWrapText( anyBoolean() );
  verify( xlsStyle, times( 0 ) ).setIndention( anyShort() );
  verify( xlsStyle, times( 0 ) ).setDataFormat( anyShort() );
}
 
Example #20
Source File: SpreadsheetCellStyleTest.java    From taro with MIT License 5 votes vote down vote up
@Test
public void equals_IsFalseIfAnyPropertyIsDifferent() {
    SpreadsheetCellStyle one = new SpreadsheetCellStyle().withBold(true).withFontName("Courier")
            .withWrapText(true).withDataFormatString("0.00").withAlign(HorizontalAlignment.CENTER);
    SpreadsheetCellStyle two = new SpreadsheetCellStyle().withBold(true).withFontName("Courier")
            .withWrapText(true).withDataFormatString("#,##0").withAlign(HorizontalAlignment.CENTER);
    // one and two differ only in data format string

    assertThat(one, not(sameInstance(two)));
    assertThat(one.equals(two), is(false));
}
 
Example #21
Source File: SpreadsheetCellStyleTest.java    From taro with MIT License 5 votes vote down vote up
@Test
public void equals_IsTrueWhenDifferentFontsHaveTheSameProperties() {
    SpreadsheetCellStyle one = new SpreadsheetCellStyle().withBold(true).withFontName("Courier")
            .withWrapText(true).withDataFormatString("0.00").withAlign(HorizontalAlignment.CENTER);
    SpreadsheetCellStyle two = new SpreadsheetCellStyle().withBold(true).withFontName("Courier")
            .withWrapText(true).withDataFormatString("0.00").withAlign(HorizontalAlignment.CENTER);

    assertThat(one, not(sameInstance(two)));
    assertThat(one.equals(two), is(true));
}
 
Example #22
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 #23
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 #24
Source File: Demo.java    From ExcelReads with Apache License 2.0 5 votes vote down vote up
@Test
public void Test_02() throws Exception {
    List<A> aa = new ArrayList<>();
    aa.add(new A("小明", 15, LocalDateTime.now()));
    aa.add(new A("小绿", 13, LocalDateTime.now()));
    aa.add(new A("唐山", 18, LocalDateTime.now()));
    aa.add(new A("狗东", 15, LocalDateTime.now()));
    aa.add(new A("百毒", 12, LocalDateTime.now()));
    ExcelFactory.saveExcel(aa, System.getProperty("user.dir").concat("/seven.xlsx"))
            //.Filter(a -> a.getA().equals("唐山"))
            //.Process(a -> a.setA(a.getA().concat("_seven")))
            .Sort(Comparator.comparing(A::getA))
            .Flush();


    List<Map<String, String>> m = new ArrayList<>();
    Map<String, String> mm = new HashMap<>();
    mm.put("姓名", "w");
    mm.put("年龄", "1");
    mm.put("性别", "w3");
    Map<String, String> mmm = new HashMap<>();
    mmm.put("姓名", "23");
    mmm.put("年龄", "w3asf2");
    mmm.put("性别", "w二3");
    m.add(mm);
    m.add(mmm);
    ExcelFactory.saveExcel(m, System.getProperty("user.dir").concat("/SaveMap_.xlsx"))
            .SetCellStyle("A", cellStyle -> cellStyle.setFillPattern(FillPatternType.DIAMONDS)
                    .setAlignment(HorizontalAlignment.RIGHT))
            .Flush();

}
 
Example #25
Source File: JRXlsMetadataExporter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
public StyleInfo(
	FillPatternType mode,
	short backcolor,
	HorizontalAlignment horizontalAlignment,
	VerticalAlignment verticalAlignment,
	short rotation,
	HSSFFont font,
	BoxStyle box,
	boolean wrapText,
	boolean cellLocked,
	boolean cellHidden,
	boolean shrinkToFit
	) 
{
	this.mode = mode;
	this.backcolor = backcolor;
	this.horizontalAlignment = horizontalAlignment;
	this.verticalAlignment = verticalAlignment;
	this.rotation = rotation;
	this.font = font;
	
	this.box = box;
	this.lcWrapText = shrinkToFit ? false : wrapText;
	this.lcCellLocked = cellLocked;
	this.lcCellHidden = cellHidden;
	this.lcShrinkToFit = shrinkToFit;
	hashCode = computeHash();
}
 
Example #26
Source File: JRXlsMetadataExporter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
private HorizontalAlignment getHorizontalAlignment(TextAlignHolder alignment) {
	switch (alignment.horizontalAlignment) 
	{
		case RIGHT:
			return HorizontalAlignment.RIGHT;
		case CENTER:
			return HorizontalAlignment.CENTER;
		case JUSTIFIED:
			return HorizontalAlignment.JUSTIFY;
		case LEFT:
		default:
			return HorizontalAlignment.LEFT;
	}
}
 
Example #27
Source File: SpreadsheetWorkbookTest.java    From taro with MIT License 5 votes vote down vote up
@Test
public void registerStyle_CreatesNewStyle_IfNotAlreadyRegistered() {
    SpreadsheetWorkbook workbook = getSpreadsheetWorkbook();

    SpreadsheetCellStyle taroStyleOne = new SpreadsheetCellStyle().withAlign(HorizontalAlignment.CENTER).withWrapText(true);
    workbook.registerStyle(taroStyleOne);
    Map<SpreadsheetCellStyle, CellStyle> styles = workbook.getCellStyles();

    assertThat(styles.size())
            .isEqualTo(1);

    CellStyle poiStyleOne = styles.get(taroStyleOne);

    assertThat(poiStyleOne)
            .isNotNull();

    SpreadsheetCellStyle taroStyleTwo = new SpreadsheetCellStyle().withAlign(HorizontalAlignment.LEFT).withWrapText(false);
    workbook.registerStyle(taroStyleTwo);
    styles = workbook.getCellStyles();

    assertThat(styles.size())
            .isEqualTo(2);

    CellStyle poiStyleTwo = styles.get(taroStyleTwo);

    assertThat(poiStyleTwo)
            .isNotNull();

    assertThat(poiStyleOne)
            .isNotSameAs(poiStyleTwo);
}
 
Example #28
Source File: JRXlsMetadataExporter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void exportRectangle(JRPrintGraphicElement element) throws JRException {
	String currentColumnName = element.getPropertiesMap().getProperty(JRXlsAbstractMetadataExporter.PROPERTY_COLUMN_NAME);
	
	if (currentColumnName != null && currentColumnName.length() > 0) {
		boolean repeatValue = getPropertiesUtil().getBooleanProperty(element, JRXlsAbstractMetadataExporter.PROPERTY_REPEAT_VALUE, false);
		
		setColumnName(currentColumnName);
		adjustColumnWidth(currentColumnName, element.getWidth(), ((JRXlsExporterNature)nature).getColumnAutoFit(element));
		adjustRowHeight(element.getHeight(), ((JRXlsExporterNature)nature).getRowAutoFit(element));
		
		short forecolor = getWorkbookColor(element.getLinePen().getLineColor()).getIndex();

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

		HSSFCellStyle cellStyle =
			getLoadedCellStyle(
				mode,
				backcolor,
				HorizontalAlignment.LEFT,
				VerticalAlignment.TOP,
				(short)0,
				getLoadedFont(getDefaultFont(), forecolor, null, getLocale()),
				new BoxStyle(element),
				isCellLocked(element),
				isCellHidden(element),
				isShrinkToFit(element)
				);
		addBlankElement(cellStyle, repeatValue, currentColumnName);
	}
}
 
Example #29
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 #30
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);
}