poi 设置excel 单元格样式

版权声明: https://blog.csdn.net/weixin_39823527/article/details/82146777
 /**
     *  创建Excel
     * @param filePath  文件路径
     * @param fileName  文件名
     * @return    创建成功返回 true  否则false
     */
    private static boolean CreateExcelFile02(String filePath, String fileName){

        //创建Excel工作簿对象
        HSSFWorkbook workbook =new HSSFWorkbook();
        //在工作簿中创建工作表对象
        HSSFSheet sheet = workbook.createSheet();
        //设置工作表名称
        workbook.setSheetName(0,"测试01");
        //在工作表中创建行对象
        HSSFRow row = sheet.createRow(0);
        //在第一行创建单元格对象
        HSSFCell cell = row.createCell(0);
        //单元格赋值
        cell.setCellValue("姓名");
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        //设置水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        //设置垂直居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置下边框
        cellStyle.setBorderBottom(BorderStyle.THIN);
        //设置上边框
        cellStyle.setBorderTop(BorderStyle.THIN);
        //设置走边框
        cellStyle.setBorderLeft(BorderStyle.THIN);
        //设置右边框
        cellStyle.setBorderRight(BorderStyle.THIN);
        //设置字体
        HSSFFont font = workbook.createFont();
        font.setFontName("华文行楷");//设置字体名称
        font.setFontHeightInPoints((short)28);//设置字号
        font.setItalic(false);//设置是否为斜体
        font.setBold(true);//设置是否加粗
        font.setColor(IndexedColors.RED.index);//设置字体颜色
        cellStyle.setFont(font);
        //设置背景
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        //设置宽度和高度
        row.setHeightInPoints(30);//设置行的高度
        sheet.setColumnWidth(0, 20 * 256);//设置列的宽度
        //渲染单元格
        cell.setCellStyle(cellStyle);

        File file = new File(filePath,fileName);
        //创建文件输出流对象
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            workbook.write(outputStream);
            outputStream.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

    }

猜你喜欢

转载自blog.csdn.net/weixin_39823527/article/details/82146777