Java Code Examples for org.apache.poi.ss.usermodel.Workbook#createCellStyle()

The following examples show how to use org.apache.poi.ss.usermodel.Workbook#createCellStyle() . 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: ExcelExportStylerColorImpl.java    From autopoi with Apache License 2.0 6 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 2
Source File: ExcelUtils.java    From seezoon-framework-all with Apache License 2.0 6 votes vote down vote up
/**
 * 导出
 * 
 * @param templatePath
 *            模板地址,自定义比较好看 classPath下的路径
 * @param colums
 *            导出的数据列 反射用
 * @param clazz
 * @param data
 * @param out
 * @param startRow
 *            从几行开始写数据
 * @return
 * @throws IOException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
public static <T> void doExport(String templatePath, String[] columns, Class<T> clazz, List<T> data,
		OutputStream out, int startRow) throws IOException, IllegalArgumentException, IllegalAccessException {
	ClassPathResource classPathResource = new ClassPathResource(templatePath);
	InputStream inputStream = classPathResource.getInputStream();
	Workbook workbook = new XSSFWorkbook(inputStream);
	Sheet sheet = workbook.getSheetAt(0);
	int size = data.size() + startRow;
	int cellNum = sheet.getRow(0).getPhysicalNumberOfCells();
	for (int i = startRow; i < size; i++) {
		Row row = sheet.createRow(i);
		for (int j = 0; j < cellNum; j++) {
			Cell cell = row.createCell(j);
			Field field = ReflectionUtils.findField(clazz, columns[j]);
			if (null == field) {
				throw new ServiceException(columns[j] + " 配置错误");
			}
			ReflectionUtils.makeAccessible(field);
			Object obj = data.get(i - startRow);
			if (field.getType().getSimpleName().equalsIgnoreCase("double")) {
				cell.setCellValue((Double) field.get(obj));
			} else if (field.getType().getSimpleName().equalsIgnoreCase("date")) {
				CellStyle style = workbook.createCellStyle();
				style.setDataFormat(workbook.createDataFormat().getFormat("yyyy-mm-dd hh:mm:ss"));
				cell.setCellStyle(style);
				cell.setCellValue((Date) field.get(obj));
			} else {
				cell.setCellValue(field.get(obj) == null ? "" : "" + field.get(obj));
			}
		}
	}
	workbook.write(out);
	workbook.close();
}
 
Example 3
Source File: DefaultXlsTableExporter.java    From olat with Apache License 2.0 5 votes vote down vote up
private CellStyle getHeaderCellStyle(final Workbook wb) {
    CellStyle cellStyle = wb.createCellStyle();
    Font boldFont = wb.createFont();
    boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    cellStyle.setFont(boldFont);
    return cellStyle;
}
 
Example 4
Source File: Exporter.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
private static CellStyle createBorderedStyle(Workbook wb) {
    CellStyle style = wb.createCellStyle();
    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;
}
 
Example 5
Source File: AbstractSheet.java    From tools with Apache License 2.0 5 votes vote down vote up
public static CellStyle createHeaderStyle(Workbook wb) {
	CellStyle headerStyle = wb.createCellStyle();
	headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
	headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	Font headerFont = wb.createFont();
	headerFont.setFontName("Arial");
	headerFont.setFontHeight(FONT_SIZE);
	headerFont.setBold(true);
	headerStyle.setFont(headerFont);
	headerStyle.setAlignment(HorizontalAlignment.CENTER);
	headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	headerStyle.setWrapText(true);
	return headerStyle;
}
 
Example 6
Source File: CellUtil.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>This method attempts to find an existing CellStyle that matches the <code>cell</code>'s 
 * current style plus styles properties in <code>properties</code>. A new style is created if the
 * workbook does not contain a matching style.</p>
 * 
 * <p>Modifies the cell style of <code>cell</code> without affecting other cells that use the
 * same style.</p>
 * 
 * <p>This is necessary because Excel has an upper limit on the number of styles that it supports.</p>
 * 
 * <p>This function is more efficient than multiple calls to
 * {@link #setCellStyleProperty(org.apache.poi.ss.usermodel.Cell, String, Object)}
 * if adding multiple cell styles.</p>
 * 
 * <p>For performance reasons, if this is the only cell in a workbook that uses a cell style,
 * this method does NOT remove the old style from the workbook.
 * <!-- NOT IMPLEMENTED: Unused styles should be
 * pruned from the workbook with [@link #removeUnusedCellStyles(Workbook)] or
 * [@link #removeStyleFromWorkbookIfUnused(CellStyle, Workbook)]. -->
 * </p>
 *
 * @param cell The cell to change the style of
 * @param properties The properties to be added to a cell style, as {propertyName: propertyValue}.
 * @since POI 3.14 beta 2
 */
public static void setCellStyleProperties(Cell cell, Map<String, Object> properties) {
    Workbook workbook = cell.getSheet().getWorkbook();
    CellStyle originalStyle = cell.getCellStyle();
    CellStyle newStyle = null;
    Map<String, Object> values = getFormatProperties(originalStyle);
    putAll(properties, values);

    // index seems like what index the cellstyle is in the list of styles for a workbook.
    // not good to compare on!
    int numberCellStyles = workbook.getNumCellStyles();

    for (int i = 0; i < numberCellStyles; i++) {
        CellStyle wbStyle = workbook.getCellStyleAt(i);
        Map<String, Object> wbStyleMap = getFormatProperties(wbStyle);

        // the desired style already exists in the workbook. Use the existing style.
        if (wbStyleMap.equals(values)) {
            newStyle = wbStyle;
            break;
        }
    }

    // the desired style does not exist in the workbook. Create a new style with desired properties.
    if (newStyle == null) {
        newStyle = workbook.createCellStyle();
        setFormatProperties(newStyle, workbook, values);
    }

    cell.setCellStyle(newStyle);
}
 
Example 7
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 8
Source File: ExcelTestHelper.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
ExcelTestHelper(final String parent, boolean generateXls) throws Exception {
  this.xls = generateXls;

  // Create a test Excel sheet with all types of supported data
  Workbook wb = generateXls ? new HSSFWorkbook() : new XSSFWorkbook();

  CreationHelper creationHelper = wb.getCreationHelper();
  DataFormat dataFormat = creationHelper.createDataFormat();
  short fmt = dataFormat.getFormat("yyyy-mm-dd hh:mm:ss");
  CellStyle style = wb.createCellStyle();
  style.setDataFormat(fmt);

  Sheet sheetWithHeader = wb.createSheet("Sheet 1");

  // Create header row
  Row headerRow = sheetWithHeader.createRow((short) 0);
  headerRow.createCell(0).setCellValue("Number");
  headerRow.createCell(1).setCellValue("String1");
  headerRow.createCell(2).setCellValue("String2");
  headerRow.createCell(3).setCellValue("MyTime");
  headerRow.createCell(4).setCellValue("Formula");
  headerRow.createCell(5).setCellValue("Boolean");
  headerRow.createCell(6).setCellValue("Error");
  generateSheetData(sheetWithHeader, style, (short)1);

  Sheet sheetWithoutHeader = wb.createSheet("Sheet 2");
  generateSheetData(sheetWithoutHeader, style, (short)0);

  testFilePath = new File(parent, "excelTestFile").getPath();

  // Write the output to a file
  FileOutputStream fileOut = new FileOutputStream(testFilePath);
  wb.write(fileOut);
  fileOut.close();
}
 
Example 9
Source File: ExcelExportStylerDefaultImpl.java    From autopoi with 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 10
Source File: DefaultXlsTableExporter.java    From olat with Apache License 2.0 5 votes vote down vote up
private CellStyle getHeaderCellStyle(final Workbook wb) {
    CellStyle cellStyle = wb.createCellStyle();
    Font boldFont = wb.createFont();
    boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    cellStyle.setFont(boldFont);
    return cellStyle;
}
 
Example 11
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 12
Source File: AbstractSheet.java    From tools with Apache License 2.0 4 votes vote down vote up
public static CellStyle createLeftWrapStyle(Workbook wb) {
	CellStyle wrapStyle = wb.createCellStyle();
	wrapStyle.setWrapText(true);
	wrapStyle.setAlignment(CellStyle.ALIGN_LEFT);
	return wrapStyle;
}
 
Example 13
Source File: SpreadsheetDataFileWriterXlsx.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
private Workbook getAsWorkbook(List<List<Object>> spreadsheetData) {
	Workbook wb = new SXSSFWorkbook();
	Sheet sheet = wb.createSheet();
	CellStyle headerCs = wb.createCellStyle();
	Iterator<List<Object>> dataIter = spreadsheetData.iterator();
	
	// Set the header style
	headerCs.setBorderBottom(BorderStyle.THICK);
	headerCs.setFillBackgroundColor(IndexedColors.BLUE_GREY.getIndex());


	// Set the font
	CellStyle cellStyle = null;
	String fontName = ServerConfigurationService.getString("spreadsheet.font");
	if (fontName != null) {
		Font font = wb.createFont();
		font.setFontName(fontName);
		headerCs.setFont(font);
		cellStyle = wb.createCellStyle();
		cellStyle.setFont(font);
	}

	// By convention, the first list in the list contains column headers.
	Row headerRow = sheet.createRow((short)0);
	List<Object> headerList = dataIter.next();
	for (short i = 0; i < headerList.size(); i++) {
		Cell headerCell = createCell(headerRow, i);
		headerCell.setCellValue((String)headerList.get(i));
		headerCell.setCellStyle(headerCs);
		//TODO
		//sheet.autoSizeColumn(i);
	}
	
	short rowPos = 1;
	while (dataIter.hasNext()) {
		List<Object> rowData = dataIter.next();
		Row row = sheet.createRow(rowPos++);
		for (short i = 0; i < rowData.size(); i++) {
			Cell cell = createCell(row, i);
			Object data = rowData.get(i);
			if (data != null) {
				if (data instanceof Double) {
					cell.setCellValue(((Double)data).doubleValue());
				} else {
					cell.setCellValue(data.toString());
				}
				if (cellStyle != null) {
					cell.setCellStyle(cellStyle);
				}
			}
		}
	}
	
	return wb;
}
 
Example 14
Source File: ExcelUtils.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public static void copyRow(Sheet worksheet, Row newRow, Row sourceRow) {
Workbook workbook = worksheet.getWorkbook();
      for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
          Cell oldCell = sourceRow.getCell(i);
          Cell newCell = newRow.createCell(i);

          if (oldCell == null) {
              newCell = null;
              continue;
          }

          CellStyle newCellStyle = workbook.createCellStyle();
          newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
          newCell.setCellStyle(newCellStyle);

          if (oldCell.getCellComment() != null) {
              newCell.setCellComment(oldCell.getCellComment());
          }

          if (oldCell.getHyperlink() != null) {
              newCell.setHyperlink(oldCell.getHyperlink());
          }

          newCell.setCellType(oldCell.getCellType());

          switch (oldCell.getCellType()) {
              case Cell.CELL_TYPE_BLANK:
                  newCell.setCellValue(oldCell.getStringCellValue());
                  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());
                  break;
              case Cell.CELL_TYPE_NUMERIC:
                  newCell.setCellValue(oldCell.getNumericCellValue());
                  break;
              case Cell.CELL_TYPE_STRING:
                  newCell.setCellValue(oldCell.getRichStringCellValue());
                  break;
          }
      }

      for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
          CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
          if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
              CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
                      (newRow.getRowNum() +
                              (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow()
                                      )),
                      cellRangeAddress.getFirstColumn(),
                      cellRangeAddress.getLastColumn());
              worksheet.addMergedRegion(newCellRangeAddress);
          }
      }
  }
 
Example 15
Source File: AbstractSheet.java    From tools with Apache License 2.0 4 votes vote down vote up
public static CellStyle createCenterStyle(Workbook wb) {
	CellStyle centerStyle = wb.createCellStyle();
	centerStyle.setWrapText(false);
	centerStyle.setAlignment(HorizontalAlignment.CENTER);
	return centerStyle;
}
 
Example 16
Source File: ExcelPOIHelper.java    From tutorials with MIT License 4 votes vote down vote up
public void writeExcel() throws IOException {
    Workbook workbook = new XSSFWorkbook();

    try {
        Sheet sheet = workbook.createSheet("Persons");
        sheet.setColumnWidth(0, 6000);
        sheet.setColumnWidth(1, 4000);

        Row header = sheet.createRow(0);

        CellStyle headerStyle = workbook.createCellStyle();

        headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        XSSFFont font = ((XSSFWorkbook) workbook).createFont();
        font.setFontName("Arial");
        font.setFontHeightInPoints((short) 16);
        font.setBold(true);
        headerStyle.setFont(font);

        Cell headerCell = header.createCell(0);
        headerCell.setCellValue("Name");
        headerCell.setCellStyle(headerStyle);

        headerCell = header.createCell(1);
        headerCell.setCellValue("Age");
        headerCell.setCellStyle(headerStyle);

        CellStyle style = workbook.createCellStyle();
        style.setWrapText(true);

        Row row = sheet.createRow(2);
        Cell cell = row.createCell(0);
        cell.setCellValue("John Smith");
        cell.setCellStyle(style);

        cell = row.createCell(1);
        cell.setCellValue(20);
        cell.setCellStyle(style);

        File currDir = new File(".");
        String path = currDir.getAbsolutePath();
        String fileLocation = path.substring(0, path.length() - 1) + "temp.xlsx";

        FileOutputStream outputStream = new FileOutputStream(fileLocation);
        workbook.write(outputStream);
    } finally {
        if (workbook != null) {
           
                workbook.close();
           
        }
    }
}
 
Example 17
Source File: ExcelView.java    From Spring-MVC-Blueprints with MIT License 4 votes vote down vote up
@Override
protected void buildExcelDocument(Map<String, Object> model,
		Workbook workbook, HttpServletRequest request,
		HttpServletResponse response) throws Exception {
	
	Sheet sheet = workbook.createSheet("sheet 1");
	
	List<HrmsLogin> users = (List<HrmsLogin>) model.get("allUsers");
	
	Row row = null;
	Cell cell = null;
	int r = 0;
	int c = 0;
	
	//Style for header cell
	CellStyle style = workbook.createCellStyle();
	style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.index);
	style.setFillPattern(CellStyle.SOLID_FOREGROUND);
	style.setAlignment(CellStyle.ALIGN_CENTER);
	
	//Create header cells
	row = sheet.createRow(r++);
	
	cell = row.createCell(c++);
	cell.setCellStyle(style);
	cell.setCellValue("Employee ID");
	
	cell = row.createCell(c++);
	cell.setCellStyle(style);
	cell.setCellValue("Username");
	
	cell = row.createCell(c++);
	cell.setCellStyle(style);
	cell.setCellValue("Password");
	
	cell = row.createCell(c++);
	cell.setCellStyle(style);
	cell.setCellValue("Role");
	
	
	//Create data cell
	for(HrmsLogin user: users){
		row = sheet.createRow(r++);
		c = 0;
		row.createCell(c++).setCellValue(user.getHrmsEmployeeDetails().getEmpId());
		row.createCell(c++).setCellValue(user.getUsername());
		row.createCell(c++).setCellValue(user.getPassword());
		row.createCell(c++).setCellValue(user.getRole());

	}
	for(int i = 0 ; i < 4; i++)
		sheet.autoSizeColumn(i, true);
	
}
 
Example 18
Source File: PoiExport.java    From hrms with Apache License 2.0 4 votes vote down vote up
public InputStream exportExcel(String fileName, String[] headTitle,
		List<String> list) throws IOException {

	Workbook wb = new HSSFWorkbook();
	//FileOutputStream fileOut = new FileOutputStream(fileName);
	HSSFCellStyle style = (HSSFCellStyle) wb.createCellStyle();
	
	// 设置这些样式
	style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
	style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
	style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
	style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
	style.setBorderRight(HSSFCellStyle.BORDER_THIN);
	style.setBorderTop(HSSFCellStyle.BORDER_THIN);
	style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

	// 生成一个字体
	HSSFFont font = (HSSFFont) wb.createFont();
	font.setColor(HSSFColor.VIOLET.index);
	font.setFontHeightInPoints((short) 12);
	font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

	// 把字体应用到当前的样式
	style.setFont(font);
	// 创建工作簿
	Sheet sheet = wb.createSheet("sheet1");
	// sheet.setDefaultColumnWidth((short)15);

	// 创建头部
	Row row = sheet.createRow((short) 0);
	for (int i = 0; i < headTitle.length; i++) {
		row.createCell(i).setCellValue(headTitle[i]);
	}
	// 填充数据
	for (int i = 0; i < list.size(); i++) {
		Row r = sheet.createRow((short) (i + 1));
		String[] strArray = list.get(i).split(",");
		for (int j = 0; j < headTitle.length; j++) {
			r.createCell(j).setCellValue(strArray[j]);
			if (strArray[j].length()>3) {
				sheet.setColumnWidth((short)j, 5000);
			}
		}
		
	}
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	try {
		wb.write(baos);
	} catch (IOException e) {
		e.printStackTrace();
	}
	byte[] ba = baos.toByteArray();
	ByteArrayInputStream excelStream = new ByteArrayInputStream(ba);
	return excelStream;

}
 
Example 19
Source File: ExcelExport.java    From frpMgr with MIT License 4 votes vote down vote up
/**
	 * 创建表格样式
	 * @param wb 工作薄对象
	 * @return 样式列表
	 */
	private Map<String, CellStyle> createStyles(Workbook wb) {
		Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
		
		CellStyle style = wb.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		Font titleFont = wb.createFont();
		titleFont.setFontName("Arial");
		titleFont.setFontHeightInPoints((short) 16);
		titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		style.setFont(titleFont);
		styles.put("title", style);

		style = wb.createCellStyle();
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		style.setBorderRight(CellStyle.BORDER_THIN);
		style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
		style.setBorderLeft(CellStyle.BORDER_THIN);
		style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
		style.setBorderTop(CellStyle.BORDER_THIN);
		style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
		style.setBorderBottom(CellStyle.BORDER_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(CellStyle.ALIGN_LEFT);
		styles.put("data1", style);

		style = wb.createCellStyle();
		style.cloneStyleFrom(styles.get("data"));
		style.setAlignment(CellStyle.ALIGN_CENTER);
		styles.put("data2", style);

		style = wb.createCellStyle();
		style.cloneStyleFrom(styles.get("data"));
		style.setAlignment(CellStyle.ALIGN_RIGHT);
		styles.put("data3", style);
		
		style = wb.createCellStyle();
		style.cloneStyleFrom(styles.get("data"));
//		style.setWrapText(true);
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		Font headerFont = wb.createFont();
		headerFont.setFontName("Arial");
		headerFont.setFontHeightInPoints((short) 10);
		headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
		headerFont.setColor(IndexedColors.WHITE.getIndex());
		style.setFont(headerFont);
		styles.put("header", style);
		
		return styles;
	}
 
Example 20
Source File: DataSetExportServicesImpl.java    From dashbuilder with Apache License 2.0 4 votes vote down vote up
private Map<String, CellStyle> createStyles(Workbook wb){
    Map<String, CellStyle> styles = new HashMap<>();
    CellStyle style;

    Font titleFont = wb.createFont();
    titleFont.setFontHeightInPoints((short)12);
    titleFont.setBold(true);
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFillForegroundColor( IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setFont(titleFont);
    style.setWrapText(false);
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
    styles.put("header", style);

    Font cellFont = wb.createFont();
    cellFont.setFontHeightInPoints((short)10);
    cellFont.setBold(true);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setVerticalAlignment(VerticalAlignment.BOTTOM);
    style.setFont(cellFont);
    style.setWrapText(false);
    style.setDataFormat(wb.createDataFormat().getFormat( BuiltinFormats.getBuiltinFormat( 3 )));
    styles.put("integer_number_cell", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.RIGHT);
    style.setVerticalAlignment(VerticalAlignment.BOTTOM);
    style.setFont(cellFont);
    style.setWrapText(false);
    style.setDataFormat(wb.createDataFormat().getFormat(BuiltinFormats.getBuiltinFormat(4)));
    styles.put("decimal_number_cell", style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.LEFT);
    style.setVerticalAlignment(VerticalAlignment.BOTTOM);
    style.setFont(cellFont);
    style.setWrapText(false);
    style.setDataFormat( (short) BuiltinFormats.getBuiltinFormat("text") );
    styles.put(TEXT_CELL, style);

    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.BOTTOM);
    style.setFont(cellFont);
    style.setWrapText(false);
    style.setDataFormat(wb.createDataFormat().getFormat( DateFormatConverter.convert( Locale.getDefault(), dateFormatPattern )));
    styles.put("date_cell", style);
    return styles;
}