Java Code Examples for org.apache.poi.ss.usermodel.Row#setHeight()

The following examples show how to use org.apache.poi.ss.usermodel.Row#setHeight() . 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: ExcelUtil.java    From supplierShop with MIT License 6 votes vote down vote up
/**
 * 创建表格样式
 */
public void setDataValidation(Excel attr, Row row, int column)
{
    if (attr.name().indexOf("注:") >= 0)
    {
        sheet.setColumnWidth(column, 6000);
    }
    else
    {
        // 设置列宽
        sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
        row.setHeight((short) (attr.height() * 20));
    }
    // 如果设置了提示信息则鼠标放上去提示.
    if (StringUtils.isNotEmpty(attr.prompt()))
    {
        // 这里默认设了2-101列提示.
        setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
    }
    // 如果设置了combo属性则本列只能选择不能输入
    if (attr.combo().length > 0)
    {
        // 这里默认设了2-101列只能选择不能输入.
        setXSSFValidation(sheet, attr.combo(), 1, 100, column, column);
    }
}
 
Example 2
Source File: ExcelExportBase.java    From easypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 创建List之后的各个Cells
 * 
 * @param styles
 */
public void createListCells(Drawing patriarch, int index, int cellNum, Object obj,
                            List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook)
                                                                                                throws Exception {
    ExcelExportEntity entity;
    Row row;
    if (sheet.getRow(index) == null) {
        row = sheet.createRow(index);
        row.setHeight(getRowHeight(excelParams));
    } else {
        row = sheet.getRow(index);
    }
    for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
        entity = excelParams.get(k);
        Object value = getCellValue(entity, obj);
        if (entity.getType() == 1) {
            createStringCell(row, cellNum++, value == null ? "" : value.toString(),
                row.getRowNum() % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity),
                entity);
        } else {
            createImageCell(patriarch, entity, row, cellNum++,
                value == null ? "" : value.toString(), obj);
        }
    }
}
 
Example 3
Source File: GenerateDoc.java    From danyuan-application with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private static XSSFSheet copySheet(XSSFSheet sheetFrom, XSSFSheet sheetTo) {
	// 初期化
	CellRangeAddress region = null;
	Row rowFrom = null;
	Row rowTo = null;
	Cell cellFrom = null;
	Cell cellTo = null;
	// セル結合のコピー
	for (int i = 0; i < sheetFrom.getNumMergedRegions(); i++) {
		region = sheetFrom.getMergedRegion(i);
		
		if ((region.getFirstColumn() >= sheetFrom.getFirstRowNum()) && (region.getLastRow() <= sheetFrom.getLastRowNum())) {
			sheetTo.addMergedRegion(region);
		}
	}
	
	// セルのコピー
	for (int intRow = sheetFrom.getFirstRowNum(); intRow <= sheetFrom.getLastRowNum(); intRow++) {
		rowFrom = sheetFrom.getRow(intRow);
		rowTo = sheetTo.createRow(intRow);
		if (null == rowFrom) {
			continue;
		}
		rowTo.setHeight(rowFrom.getHeight());
		for (int intCol = 0; intCol < rowFrom.getLastCellNum(); intCol++) {
			// セル幅のコピー
			sheetTo.setDefaultColumnStyle(intCol, sheetFrom.getColumnStyle(intCol));
			sheetTo.setColumnWidth(intCol, sheetFrom.getColumnWidth(intCol));
			cellFrom = rowFrom.getCell(intCol);
			cellTo = rowTo.createCell(intCol);
			if (null == cellFrom) {
				continue;
			}
			// セルスタイルとタイプのコピー
			cellTo.setCellStyle(cellFrom.getCellStyle());
			cellTo.setCellType(cellFrom.getCellType());
			// タイトル内容のコピー
			// 不同数据类型处理
			int cellFromType = cellFrom.getCellType();
			cellTo.setCellType(cellFromType);
			if (cellFromType == HSSFCell.CELL_TYPE_NUMERIC) {
				if (HSSFDateUtil.isCellDateFormatted(cellFrom)) {
					cellTo.setCellValue(cellFrom.getDateCellValue());
				} else {
					cellTo.setCellValue(cellFrom.getNumericCellValue());
				}
			} else if (cellFromType == HSSFCell.CELL_TYPE_STRING) {
				cellTo.setCellValue(cellFrom.getRichStringCellValue());
			} else if (cellFromType == HSSFCell.CELL_TYPE_BLANK) {
				// nothing21
			} else if (cellFromType == HSSFCell.CELL_TYPE_BOOLEAN) {
				cellTo.setCellValue(cellFrom.getBooleanCellValue());
			} else if (cellFromType == HSSFCell.CELL_TYPE_ERROR) {
				cellTo.setCellErrorValue(cellFrom.getErrorCellValue());
			} else if (cellFromType == HSSFCell.CELL_TYPE_FORMULA) {
				cellTo.setCellFormula(cellFrom.getCellFormula());
			} else { // nothing29
			}
		}
	}
	
	// 枠線の設定
	sheetTo.setDisplayGridlines(false);
	// sheetTo.setDisplayGuts(true);
	// sheetTo.setDisplayRowColHeadings(true);
	// 剪切
	// sheetTo.shiftRows(13, 15, 31, false, false, false);
	// Excelのズーム設定
	sheetTo.setZoom(85, 100);
	
	// シートを戻る。
	return sheetTo;
}
 
Example 4
Source File: ExcelUtil.java    From RuoYi-Vue with MIT License 6 votes vote down vote up
/**
 * 创建表格样式
 */
public void setDataValidation(Excel attr, Row row, int column)
{
    if (attr.name().indexOf("注:") >= 0)
    {
        sheet.setColumnWidth(column, 6000);
    }
    else
    {
        // 设置列宽
        sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
        row.setHeight((short) (attr.height() * 20));
    }
    // 如果设置了提示信息则鼠标放上去提示.
    if (StringUtils.isNotEmpty(attr.prompt()))
    {
        // 这里默认设了2-101列提示.
        setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
    }
    // 如果设置了combo属性则本列只能选择不能输入
    if (attr.combo().length > 0)
    {
        // 这里默认设了2-101列只能选择不能输入.
        setXSSFValidation(sheet, attr.combo(), 1, 100, column, column);
    }
}
 
Example 5
Source File: ExcelExportUtil.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/**
 * 创建List之后的各个Cells
 * @param styles 
 */
private static void createListCells(Drawing patriarch, int index, int cellNum, Object obj,
		List<ExcelExportEntity> excelParams, Sheet sheet,
		HSSFWorkbook workbook, Map<String, HSSFCellStyle> styles) throws Exception {
	ExcelExportEntity entity;
	Row row;
	if(sheet.getRow(index)==null){
		row = sheet.createRow(index);
		row.setHeight((short) 350);
	}else{
		row = sheet.getRow(index);
	}
	for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
		entity = excelParams.get(k);
		Object value = getCellValue(entity,obj);
		if (entity.getType() == 1) {
			createStringCell(row, cellNum++,
					value == null ? "" : value.toString(),
							row.getRowNum() % 2 == 0 ?  getStyles(styles, false, entity.isWrap())
									: getStyles(styles,true, entity.isWrap()),entity);
		}else {
			createImageCell(patriarch,entity, row, cellNum++, value == null ? ""
					: value.toString(),obj);
		}
	}
}
 
Example 6
Source File: ExcelExportOfTemplateUtil.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/**
 * 创建List之后的各个Cells
 * 
 * @param styles
 */
private static void createListCells(Drawing patriarch, int index,
		int cellNum, Object obj, List<ExcelExportEntity> excelParams,
		Sheet sheet, Workbook workbook) throws Exception {
	ExcelExportEntity entity;
	Row row;
	if (sheet.getRow(index) == null) {
		row = sheet.createRow(index);
		row.setHeight((short) 350);
	} else {
		row = sheet.getRow(index);
	}
	for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
		entity = excelParams.get(k);
		Object value = getCellValue(entity, obj);
		if (entity.getType() != 2) {
			createStringCell(row, cellNum++,
					value == null ? "" : value.toString(), entity, workbook);
		} else {
			createImageCell(patriarch, entity, row, cellNum++,
					value == null ? "" : value.toString(), obj, workbook);
		}
	}
}
 
Example 7
Source File: ExcelExportBase.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 创建List之后的各个Cells
 * 
 * @param styles
 */
public void createListCells(Drawing patriarch, int index, int cellNum, Object obj, List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook) throws Exception {
	ExcelExportEntity entity;
	Row row;
	if (sheet.getRow(index) == null) {
		row = sheet.createRow(index);
		row.setHeight(getRowHeight(excelParams));
	} else {
		row = sheet.getRow(index);
	}
	for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
		entity = excelParams.get(k);
		Object value = getCellValue(entity, obj);
		if (entity.getType() == 1) {
			createStringCell(row, cellNum++, value == null ? "" : value.toString(), row.getRowNum() % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity);
		} else if (entity.getType() == 4){
			createNumericCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity);
		}  else{
			createImageCell(patriarch, entity, row, cellNum++, value == null ? "" : value.toString(), obj);
		}
	}
}
 
Example 8
Source File: ExcelUtil.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 添加单元格
 */
public Cell addCell(Excel attr, Row row, T vo, Field field, int column)
{
    Cell cell = null;
    try
    {
        // 设置行高
        row.setHeight((short) (attr.height() * 20));
        // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
        if (attr.isExport())
        {
            // 创建cell
            cell = row.createCell(column);
            cell.setCellStyle(styles.get("data"));

            // 用于读取对象中的属性
            Object value = getTargetValue(vo, field, attr);
            String dateFormat = attr.dateFormat();
            String readConverterExp = attr.readConverterExp();
            if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
            {
                cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
            }
            else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
            {
                cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp));
            }
            else
            {
                // 设置列类型
                setCellVo(value, attr, cell);
            }
        }
    }
    catch (Exception e)
    {
        log.error("导出Excel失败{}", e);
    }
    return cell;
}
 
Example 9
Source File: ExcelExportServer.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
/**
 * 创建表头
 * 
 * @param title
 * @param index
 */
private int createTitleRow(ExportParams title, Sheet sheet, Workbook workbook, int index, List<ExcelExportEntity> excelParams) {
	Row row = sheet.createRow(index);
	int rows = getRowNums(excelParams);
	row.setHeight((short) 450);
	Row listRow = null;
	if (rows == 2) {
		listRow = sheet.createRow(index + 1);
		listRow.setHeight((short) 450);
	}
	int cellIndex = 0;
	CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor());
	for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) {
		ExcelExportEntity entity = excelParams.get(i);
		if (StringUtils.isNotBlank(entity.getName())) {
			createStringCell(row, cellIndex, entity.getName(), titleStyle, entity);
		}
		if (entity.getList() != null) {
			List<ExcelExportEntity> sTitel = entity.getList();
			if (StringUtils.isNotBlank(entity.getName())) {
				sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex, cellIndex + sTitel.size() - 1));
			}
			for (int j = 0, size = sTitel.size(); j < size; j++) {
				createStringCell(rows == 2 ? listRow : row, cellIndex, sTitel.get(j).getName(), titleStyle, entity);
				cellIndex++;
			}
			cellIndex--;
		} else if (rows == 2) {
			createStringCell(listRow, cellIndex, "", titleStyle, entity);
			sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex, cellIndex));
		}
		cellIndex++;
	}
	return rows;

}
 
Example 10
Source File: ExcelExportUtil.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 创建表头
 * @param entity 
 * 
 * @param index
 */
private static void createTitleRow(ExcelTitle title, Sheet sheet, HSSFWorkbook workbook,
		int index, List<ExcelExportEntity> excelParams) {
	Row row = sheet.createRow(index);
	Row row1 = sheet.createRow(index + 1);
	row.setHeight((short) 450);
	int cellIndex = 0;
	CellStyle titleStyle = getTitleStyle(workbook,title);
	for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) {
		ExcelExportEntity entity = excelParams.get(i);
		createStringCell(row, cellIndex, entity.getName(), titleStyle,entity);
		if (entity.getList() != null) {
			List<ExcelExportEntity> sTitel = entity.getList();
			sheet.addMergedRegion(new CellRangeAddress(index, index,cellIndex,cellIndex
					+ sTitel.size() - 1));
			for (int j = 0, size = sTitel.size(); j < size; j++) {
				createStringCell(row1, cellIndex, sTitel.get(j).getName(),
						titleStyle,entity);
				cellIndex++;
			}
		} else {
			sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex,
					cellIndex));
			cellIndex++;
		}
	}

}
 
Example 11
Source File: ForeachRowDirective.java    From onetwo with Apache License 2.0 5 votes vote down vote up
protected Row generateRow(ForeachRowInfo repeateRow, final ExcelTemplateValueProvider provider, int rownumb){
	Sheet sheet = repeateRow.getRow().getSheet();
	Row newRow = sheet.createRow(rownumb);
	newRow.setHeight(repeateRow.getRow().getHeight());
	ExcelUtils.copyRow(sheet, newRow, repeateRow.getRow());
	return newRow;
}
 
Example 12
Source File: ExcelUtil.java    From LuckyFrameWeb with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * 填充excel数据
 * @param index 序号
 *
 */
public void fillExcelData(int index)
{
    int startNo = index * sheetSize;
    int endNo = Math.min(startNo + sheetSize, list.size());
    // 写入各条记录,每条记录对应excel表中的一行
    CellStyle cs = wb.createCellStyle();
    cs.setAlignment(HorizontalAlignment.CENTER);
    cs.setVerticalAlignment(VerticalAlignment.CENTER);
    for (int i = startNo; i < endNo; i++)
    {
        Row row = sheet.createRow(i + 1 - startNo);
        // 得到导出对象.
        T vo = list.get(i);
        for (int j = 0; j < fields.size(); j++)
        {
            // 获得field.
            Field field = fields.get(j);
            // 设置实体类私有属性可访问
            field.setAccessible(true);
            Excel attr = field.getAnnotation(Excel.class);
            try
            {
                // 设置行高
                row.setHeight((short) (attr.height() * 20));
                // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
                if (attr.isExport())
                {
                    // 创建cell
                    Cell cell = row.createCell(j);
                    cell.setCellStyle(cs);
                    if (vo == null)
                    {
                        // 如果数据存在就填入,不存在填入空格.
                        cell.setCellValue("");
                        continue;
                    }

                    // 用于读取对象中的属性
                    Object value = getTargetValue(vo, field, attr);
                    String dateFormat = attr.dateFormat();
                    String readConverterExp = attr.readConverterExp();
                    if (StringUtils.isNotEmpty(dateFormat))
                    {
                        cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
                    }
                    else if (StringUtils.isNotEmpty(readConverterExp))
                    {
                        cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp));
                    }
                    else
                    {
                        cell.setCellType(CellType.STRING);
                        // 如果数据存在就填入,不存在填入空格.
                        cell.setCellValue(StringUtils.isNull(value) ? attr.defaultValue() : value + attr.suffix());
                    }
                }
            }
            catch (Exception e)
            {
                log.error("导出Excel失败{}", e.getMessage());
            }
        }
    }
}
 
Example 13
Source File: Height.java    From excel-io with MIT License 4 votes vote down vote up
@Override
public void accept(final Row row) {
    row.setHeight(this.value);
}
 
Example 14
Source File: ExcelExportOfTemplateUtil.java    From jeewx with Apache License 2.0 4 votes vote down vote up
/**
 * 创建 最主要的 Cells
 * 
 * @param styles
 * @throws Exception
 */
private static int createCells(Drawing patriarch, int index, Object t,
		List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook)
		throws Exception {
	ExcelExportEntity entity;
	Row row = sheet.createRow(index);
	row.setHeight((short) 350);
	int maxHeight = 1, cellNum = 0;
	for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
		entity = excelParams.get(k);
		if (entity.getList() != null) {
			Collection<?> list = (Collection<?>) entity.getGetMethod()
					.invoke(t, new Object[] {});
			int listC = 0;
			for (Object obj : list) {
				createListCells(patriarch, index + listC, cellNum, obj,
						entity.getList(), sheet, workbook);
				listC++;
			}
			cellNum += entity.getList().size();
			if (list != null && list.size() > maxHeight) {
				maxHeight = list.size();
			}
		} else {
			Object value = getCellValue(entity, t);
			if (entity.getType() != 2) {
				createStringCell(row, cellNum++, value.toString(), entity,
						workbook);
			} else {
				createImageCell(patriarch, entity, row, cellNum++,
						value == null ? "" : value.toString(), t, workbook);
			}
		}
	}
	// 合并需要合并的单元格
	cellNum = 0;
	for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
		entity = excelParams.get(k);
		if (entity.getList() != null) {
			cellNum += entity.getList().size();
		} else if (entity.isNeedMerge()) {
			sheet.addMergedRegion(new CellRangeAddress(index, index
					+ maxHeight - 1, cellNum, cellNum));
			cellNum++;
		}
	}
	return maxHeight;

}
 
Example 15
Source File: ExcelExportBase.java    From jeasypoi with Apache License 2.0 4 votes vote down vote up
/**
 * 创建 最主要的 Cells
 * 
 * @param styles
 * @param rowHeight
 * @throws Exception
 */
public int createCells(Drawing patriarch, int index, Object t, List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook, short rowHeight) throws Exception {
	ExcelExportEntity entity;
	Row row = sheet.createRow(index);
	row.setHeight(rowHeight);
	int maxHeight = 1, cellNum = 0;
	int indexKey = createIndexCell(row, index, excelParams.get(0));
	cellNum += indexKey;
	for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) {
		entity = excelParams.get(k);
		if (entity.getList() != null) {
			Collection<?> list = getListCellValue(entity, t);
			int listC = 0;
			for (Object obj : list) {
				createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), sheet, workbook);
				listC++;
			}
			cellNum += entity.getList().size();
			if (list != null && list.size() > maxHeight) {
				maxHeight = list.size();
			}
		} else {
			Object value = getCellValue(entity, t);
			if (entity.getType() == 1) {
				createStringCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity);
			} else if (entity.getType() == 4){
				createNumericCell(row, cellNum++, value == null ? "" : value.toString(), index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity);
			} else {
				createImageCell(patriarch, entity, row, cellNum++, value == null ? "" : value.toString(), t);
			}
		}
	}
	// 合并需要合并的单元格
	cellNum = 0;
	for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) {
		entity = excelParams.get(k);
		if (entity.getList() != null) {
			cellNum += entity.getList().size();
		} else if (entity.isNeedMerge()) {
			for (int i = index + 1; i < index + maxHeight; i++) {
				sheet.getRow(i).createCell(cellNum);
				sheet.getRow(i).getCell(cellNum).setCellStyle(getStyles(false, entity));
			}
			sheet.addMergedRegion(new CellRangeAddress(index, index + maxHeight - 1, cellNum, cellNum));
			cellNum++;
		}
	}
	return maxHeight;

}
 
Example 16
Source File: ExcelExportServer.java    From easypoi with Apache License 2.0 4 votes vote down vote up
/**
 * 创建表头
 * 
 * @param title
 * @param index
 */
private int createTitleRow(ExportParams title, Sheet sheet, Workbook workbook, int index,
                           List<ExcelExportEntity> excelParams) {
    Row row = sheet.createRow(index);
    int rows = getRowNums(excelParams);
    row.setHeight((short) 450);
    Row listRow = null;
    if (rows == 2) {
        listRow = sheet.createRow(index + 1);
        listRow.setHeight((short) 450);
    }
    int cellIndex = 0;
    CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor());
    for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) {
        ExcelExportEntity entity = excelParams.get(i);
        if (StringUtils.isNotBlank(entity.getName())) {
            createStringCell(row, cellIndex, entity.getName(), titleStyle, entity);
        }
        if (entity.getList() != null) {
            List<ExcelExportEntity> sTitel = entity.getList();
            if (StringUtils.isNotBlank(entity.getName())) {
                sheet.addMergedRegion(new CellRangeAddress(index, index, cellIndex, cellIndex
                                                                                    + sTitel
                                                                                        .size()
                                                                                    - 1));
            }
            for (int j = 0, size = sTitel.size(); j < size; j++) {
                createStringCell(rows == 2 ? listRow : row, cellIndex, sTitel.get(j).getName(),
                    titleStyle, entity);
                cellIndex++;
            }
            cellIndex--;
        } else if (rows == 2) {
            createStringCell(listRow, cellIndex, "", titleStyle, entity);
            sheet.addMergedRegion(new CellRangeAddress(index, index + 1, cellIndex, cellIndex));
        }
        cellIndex++;
    }
    return rows;

}
 
Example 17
Source File: ExcelExportBase.java    From easypoi with Apache License 2.0 4 votes vote down vote up
/**
 * 创建 最主要的 Cells
 * 
 * @param styles
 * @param rowHeight
 * @throws Exception
 */
public int createCells(Drawing patriarch, int index, Object t,
                       List<ExcelExportEntity> excelParams, Sheet sheet, Workbook workbook,
                       short rowHeight) throws Exception {
    ExcelExportEntity entity;
    Row row = sheet.createRow(index);
    row.setHeight(rowHeight);
    int maxHeight = 1, cellNum = 0;
    int indexKey = createIndexCell(row, index, excelParams.get(0));
    cellNum += indexKey;
    for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) {
        entity = excelParams.get(k);
        if (entity.getList() != null) {
            Collection<?> list = getListCellValue(entity, t);
            int listC = 0;
            for (Object obj : list) {
                createListCells(patriarch, index + listC, cellNum, obj, entity.getList(),
                    sheet, workbook);
                listC++;
            }
            cellNum += entity.getList().size();
            if (list != null && list.size() > maxHeight) {
                maxHeight = list.size();
            }
        } else {
            Object value = getCellValue(entity, t);
            if (entity.getType() == 1) {
                createStringCell(row, cellNum++, value == null ? "" : value.toString(),
                    index % 2 == 0 ? getStyles(false, entity) : getStyles(true, entity), entity);
            } else {
                createImageCell(patriarch, entity, row, cellNum++,
                    value == null ? "" : value.toString(), t);
            }
        }
    }
    // 合并需要合并的单元格
    cellNum = 0;
    for (int k = indexKey, paramSize = excelParams.size(); k < paramSize; k++) {
        entity = excelParams.get(k);
        if (entity.getList() != null) {
            cellNum += entity.getList().size();
        } else if (entity.isNeedMerge()) {
            for (int i = index + 1; i < index + maxHeight; i++) {
                sheet.getRow(i).createCell(cellNum);
                sheet.getRow(i).getCell(cellNum).setCellStyle(getStyles(false, entity));
            }
            sheet.addMergedRegion(new CellRangeAddress(index, index + maxHeight - 1, cellNum,
                cellNum));
            cellNum++;
        }
    }
    return maxHeight;

}
 
Example 18
Source File: GenerateDoc.java    From danyuan-application with Apache License 2.0 4 votes vote down vote up
/**
 * @方法名 copySheet
 * @功能 复制sheet
 * @参数 @param sheetFrom
 * @参数 @param sheetTo
 * @参数 @return
 * @返回 HSSFSheet
 * @author Administrator
 * @throws
 */
@SuppressWarnings("deprecation")
private static HSSFSheet copySheet(HSSFSheet sheetFrom, HSSFSheet sheetTo) {
	
	// 初期化
	CellRangeAddress region = null;
	Row rowFrom = null;
	Row rowTo = null;
	Cell cellFrom = null;
	Cell cellTo = null;
	// セル結合のコピー
	for (int i = 0; i < sheetFrom.getNumMergedRegions(); i++) {
		region = sheetFrom.getMergedRegion(i);
		
		if ((region.getFirstColumn() >= sheetFrom.getFirstRowNum()) && (region.getLastRow() <= sheetFrom.getLastRowNum())) {
			sheetTo.addMergedRegion(region);
		}
	}
	
	// セルのコピー
	for (int intRow = sheetFrom.getFirstRowNum(); intRow <= sheetFrom.getLastRowNum(); intRow++) {
		rowFrom = sheetFrom.getRow(intRow);
		rowTo = sheetTo.createRow(intRow);
		if (null == rowFrom) {
			continue;
		}
		rowTo.setHeight(rowFrom.getHeight());
		for (int intCol = 0; intCol < rowFrom.getLastCellNum(); intCol++) {
			// セル幅のコピー
			sheetTo.setDefaultColumnStyle(intCol, sheetFrom.getColumnStyle(intCol));
			sheetTo.setColumnWidth(intCol, sheetFrom.getColumnWidth(intCol));
			cellFrom = rowFrom.getCell(intCol);
			cellTo = rowTo.createCell(intCol);
			if (null == cellFrom) {
				continue;
			}
			// セルスタイルとタイプのコピー
			cellTo.setCellStyle(cellFrom.getCellStyle());
			cellTo.setCellType(cellFrom.getCellType());
			// タイトル内容のコピー
			// 不同数据类型处理
			int cellFromType = cellFrom.getCellType();
			cellTo.setCellType(cellFromType);
			if (cellFromType == HSSFCell.CELL_TYPE_NUMERIC) {
				if (HSSFDateUtil.isCellDateFormatted(cellFrom)) {
					cellTo.setCellValue(cellFrom.getDateCellValue());
				} else {
					cellTo.setCellValue(cellFrom.getNumericCellValue());
				}
			} else if (cellFromType == HSSFCell.CELL_TYPE_STRING) {
				cellTo.setCellValue(cellFrom.getRichStringCellValue());
			} else if (cellFromType == HSSFCell.CELL_TYPE_BLANK) {
				// nothing21
			} else if (cellFromType == HSSFCell.CELL_TYPE_BOOLEAN) {
				cellTo.setCellValue(cellFrom.getBooleanCellValue());
			} else if (cellFromType == HSSFCell.CELL_TYPE_ERROR) {
				cellTo.setCellErrorValue(cellFrom.getErrorCellValue());
			} else if (cellFromType == HSSFCell.CELL_TYPE_FORMULA) {
				cellTo.setCellFormula(cellFrom.getCellFormula());
			} else { // nothing29
			}
		}
	}
	
	// 枠線の設定
	sheetTo.setDisplayGridlines(false);
	// sheetTo.setDisplayGuts(true);
	// sheetTo.setDisplayRowColHeadings(true);
	// 剪切
	// sheetTo.shiftRows(13, 15, 31, false, false, false);
	// Excelのズーム設定
	sheetTo.setZoom(85, 100);
	
	// シートを戻る。
	return sheetTo;
	
}
 
Example 19
Source File: UKExcelUtil.java    From youkefu with Apache License 2.0 4 votes vote down vote up
/**
	 * 构建副标题
	 */
	@SuppressWarnings("deprecation")
	private void createSubHead(){
		
		CellStyle cellStyle = wb.createCellStyle(); 
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐 
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐 
		cellStyle.setWrapText(true);// 指定单元格自动换行 

		// 设置单元格字体 
		Font font = wb.createFont(); 
		//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
		font.setFontName("宋体"); 
		font.setFontHeight((short) 180); 
		cellStyle.setFont(font); 
		
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		
		
		CellStyle leftStyle = wb.createCellStyle(); 
		leftStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 指定单元格居中对齐 
		leftStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐 
		leftStyle.setWrapText(true);// 指定单元格自动换行 
		leftStyle.setFont(font); 
		
		leftStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		leftStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		leftStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		leftStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		
		Row row1 = sheet.createRow(rowNum); 
		row1.setHeight((short) 440); 
		
		
		
		StringBuffer strb = new StringBuffer();
//		if(false){	//增加 过滤器
//			
//		}else{
//			
//		}
		strb.append("报表生成日期:").append(getNowDate()) ;
		
		Cell cell2 = row1.createCell(0); 
		cell2.setCellType(HSSFCell.ENCODING_UTF_16); 
		cell2.setCellValue(new XSSFRichTextString(strb.toString())); 
		cell2.setCellStyle(leftStyle);
		// 指定合并区域 
		if(rowNum>1) {
			sheet.addMergedRegion(new CellRangeAddress(rowNum,  rowNum, 0, (cellNumber-1)));
		}
		
		for(int i=1;i<this.cellNumber;i++){
			Cell cell3= row1.createCell(i); 
			cell3.setCellStyle(cellStyle);
		}
		rowNum ++;
	}
 
Example 20
Source File: UKExcelUtil.java    From youkefu with Apache License 2.0 4 votes vote down vote up
/**
 * 构建头部
 */
@SuppressWarnings("deprecation")
private void createHead(){
	Row row = sheet.createRow(rowNum); 

	// 设置第一行 
	Cell cell = row.createCell(0); 
	row.setHeight((short) 1100); 

	// 定义单元格为字符串类型 
	cell.setCellType(HSSFCell.ENCODING_UTF_16); 
	cell.setCellValue(new XSSFRichTextString(this.headTitle)); 

	// 指定合并区域 
	if(rowNum>0) {
		sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum,0,(cellNumber-1))); 
	}

	CellStyle cellStyle = wb.createCellStyle(); 
	
	cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐 
	cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐 
	cellStyle.setWrapText(true);// 指定单元格自动换行 

	// 设置单元格字体 
	Font font = wb.createFont(); 
	font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
	font.setFontName("宋体"); 
	font.setFontHeight((short) 400); 
	cellStyle.setFont(font); 
	cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
	cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
	cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
	cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
	cell.setCellStyle(cellStyle); 
	for(int i=1;i<this.cellNumber;i++){
		Cell cell3= row.createCell(i); 
		cell3.setCellStyle(cellStyle);
	}
	rowNum ++;
}