【工具类】Excel导出那些事儿(五)

接上一篇博客,继续介绍双标题导出.

/**
 * 双标题excel导出
 * @param response
 * @param fileName 文件名
 * @param sheetName sheet页名
 * @param firstTitle 第一行表头标题 
 * @param title  第二行标题
 * @param content 每一列对应值
 * @param list 单元格下拉选项(可根据具体需求去留)
 */
public static void exportForTwoTitle(HttpServletResponse response, String fileName, String sheetName, String firstTitle, String[] title, String[][] content, List list) {
    XSSFWorkbook wb = new XSSFWorkbook();
    OutputStream os = null;
    try {
        wb = exportXlsForTwoTitle(sheetName, firstTitle,title, content, wb,list);
        ExcelUtil.setResponse(response, fileName);
        os = response.getOutputStream();
        wb.write(os);
        os.flush();
    } catch (Exception e) {
        logger.error("io error");
    } finally {
        try {
            wb.close();
            if (os != null) {
                os.close();
            }
        } catch (Exception e) {
            logger.error("close io error");
        }
    }
}

/**
 * 导出Excel(双标题)
 *
 * @param sheetName sheet名称
 * @param title     标题
 * @param values    内容
 * @param wb        HSSFWorkbook对象
 * @return
 */
public static XSSFWorkbook exportXlsForTwoTitle(String sheetName, String firstTitle, String[] title, String[][] values, XSSFWorkbook wb,List list) {

    // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
    if (wb == null) {
        wb = new XSSFWorkbook();
    }

    // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
    XSSFSheet sheet = wb.createSheet(sheetName);
    //设置默认列宽
    sheet.setDefaultColumnWidth(20);
    // 第四步,创建单元格,并设置值表头 设置表头居中
    XSSFCellStyle style = wb.createCellStyle();
    // 创建一个居中格式
    style.setAlignment(HorizontalAlignment.CENTER);
    //设置文本格式
    XSSFDataFormat format = wb.createDataFormat();
    style.setDataFormat(format.getFormat("@"));
    // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
    XSSFRow row0 = sheet.createRow(0);
    row0.setHeightInPoints(175);
    //sheet.autoSizeColumn(1, true);
    XSSFCellStyle style0 = wb.createCellStyle();
    // 创建一个居中格式
    style0.setAlignment(HorizontalAlignment.LEFT);
    // 自动换行
    style0.setWrapText(true);
    // 生成一个字体
    XSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 11);
    font.setColor(HSSFColor.ROYAL_BLUE.index);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
    font.setFontName("仿宋_GB2312");

    // 把字体 应用到当前样式
    style0.setFont(font);
    XSSFCell cell0 = row0.createCell(0);
    // 单元格合并
    CellRangeAddress region = new CellRangeAddress(0,0, 0,9 );
    sheet.addMergedRegion(region);
    cell0.setCellValue(firstTitle);
    cell0.setCellStyle(style0);
    XSSFRow row = sheet.createRow(1);
    XSSFCell cell;
    //创建标题
    for (int i = 0; i < title.length; i++) {
        cell = row.createCell(i);
        cell.setCellValue(title[i]);
        cell.setCellStyle(style);
    }
	//设置某一列字段的下拉框值选项
    String[] typeList = Type.getAllEnum().toArray(new String[]{});
    XSSFDataValidation dataValidationType = setColumnList(sheet,typeList,1);
    if (dataValidationType != null) {
        sheet.addValidationData(dataValidationType);
    }

    //********************************************************
    //创建内容
    if(values != null && values.length >0){
        //创建内容
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 2);
            for (int j = 0; j < values[i].length; j++) {
                //将内容按顺序赋给对应的列对象
                cell = row.createCell(j);
                cell.setCellStyle(style);
                cell.setCellValue(values[i][j]);
                cell.setCellType(XSSFCell.CELL_TYPE_STRING);
            }
        }
    }
    return wb;
}

/**
 * 设置指定字段下拉选项
 * @param sheet 当前sheet
 * @param textList 下拉选项
 * @param colNum 第几列
 * @return
 */
public static XSSFDataValidation setColumnList(XSSFSheet sheet, String[] textList, int colNum) {
    XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
    XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper
            .createExplicitListConstraint(textList);
    CellRangeAddressList addres = new CellRangeAddressList(1,65536,colNum,colNum);
    XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(
            dvConstraint, addres);
    validation.setSuppressDropDownArrow(true);
    return validation;
}

 

你可能感兴趣的:(Java)