EXCEL generates forget-me-nots (font color, alignment, size, merge cells)

public static void writeExcel(ExcelParam excelParam) throws IOException { 
    //Create a workbook 
    Workbook wb = new HSSFWorkbook(); 
    //Create a sheet 
    Sheet sheet = wb.createSheet("sheet1"); 

    // Set font 
    Font font = wb .createFont(); 
    font.setColor(HSSFFont.COLOR_NORMAL); //font color 
    font.setFontName("Microsoft Yahei"); //font 
    font.setFontHeightInPoints((short)18); 
    // font.setItalic(true1) ; //Whether to use italics 
    // font.setStrikeout(true); //Whether to use 
    strokes 
    // Set cell style CellStyle cellStyle = wb.createCellStyle(); 
    cellStyle.setBorderBottom(BorderStyle.THIN); //Bottom border 
    cellStyle .setBorderLeft(BorderStyle.THIN);//Left border
    cellStyle.setBorderTop(BorderStyle.THIN);//Upper border 
    cellStyle.setBorderRight(BorderStyle.THIN);//Right border 
    cellStyle.setFont(font); 
    cellStyle.setAlignment(HorizontalAlignment.CENTER); //Horizontal Layout: Centered 
    // Set the vertical 
    alignment of the cell content to the center 
    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); 
    // cellStyle.setWrapText(true); // auto wrap 
    // create a row of header Row rowHead = sheet.createRow(0); 
    //fill Title content 
    //Fill data to the header of the table 
    Font fh = wb.createFont(); 
    fh.setColor(HSSFFont.COLOR_RED); //Font color 
    fh.setFontName("黑体"); //Font 
    fh.setBold(true); 
    fh.setFontHeightInPoints((short)22); 
    CellStyle sh = wb.createCellStyle();
    sh.setBorderBottom(BorderStyle.THIN); //bottom border 
    } 
    sh.setBorderLeft(BorderStyle.THIN);//Left border
    sh.setBorderTop(BorderStyle.THIN);//Top border 
    sh.setBorderRight(BorderStyle.THIN);//Right border 
    sh.setFont(fh); 
    sh.setAlignment(HorizontalAlignment.CENTER); //Horizontal Layout: Centered 
    // Set the cell content to be aligned vertically 
    sh.setVerticalAlignment(VerticalAlignment.CENTER); 
    for (int i = 0; i <excelParam.getHead().size(); i++) { 
        // merge the first row of the cell 
        sheet .addMergedRegion(new CellRangeAddress(0,0,0,excelParam.getData().get(0).size()-1)); 
        Cell cell = rowHead.createCell(i); 
        cell.setCellValue(excelParam.getHead() .get(i)); 
        cell.setCellStyle(sh); 
        cell.setCellType(STRING); 
    //Table body analysis list 
    //Table content
    for (int i = 0; i <excelParam.getData().size(); i++) {// 
        Row row = sheet.createRow(i + 1); 
        for (int j = 0; j <excelParam.getData ().get(i).size(); j++) {//Number of columns 
            // Columns automatically adapt 
            sheet.autoSizeColumn(j); 
            Cell cell = row.createCell(j); 
            cell.setCellValue(excelParam.getData() .get(i).get(j)); 
            cell.setCellStyle(cellStyle); 
            cell.setCellType(STRING); 
        } 
    } 

    //Determine whether a directory exists. 
    File file = new File(excelParam.getFilePath()); 
    if ( !file.getParentFile().exists()) { 
        // Create if it does not exist 
        file.mkdirs(); 
    }
    // Output Excel file 1   
    FileOutputStream output = new FileOutputStream(file); 
    wb.write(output);//write to disk   
    output.close(); 
}

Guess you like

Origin blog.csdn.net/a1_HelloWord/article/details/84729197