POI设置excel样式

2 篇文章 0 订阅
1 篇文章 0 订阅

 

//创建HSSFWorkbook对象(excel的文档对象)  
            HSSFWorkbook wb = new HSSFWorkbook();
            // 建立新的sheet对象(excel的表单)
            HSSFSheet sheet = wb.createSheet("sheet1");
            
            //设置表头样式
            HSSFCellStyle styleTitle = wb.createCellStyle();
            //水平居中
            styleTitle.setAlignment(HorizontalAlignment.CENTER);
            //垂直居中
            styleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
            //设置背景色
            styleTitle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
            styleTitle.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
            styleTitle.setFillPattern(FillPatternType.LEAST_DOTS);
            //设置边框
            styleTitle.setBorderBottom(BorderStyle.THIN);
            styleTitle.setBottomBorderColor(IndexedColors.BLUE.index);
            styleTitle.setBorderLeft(BorderStyle.THIN);
            styleTitle.setLeftBorderColor(IndexedColors.BLUE.index);
            styleTitle.setBorderRight(BorderStyle.THIN);
            styleTitle.setRightBorderColor(IndexedColors.BLUE.index);
            styleTitle.setBorderTop(BorderStyle.THIN);
            styleTitle.setTopBorderColor(IndexedColors.BLUE.index);
            
            //设置字体
            HSSFFont font = wb.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 11);//设置字体大小
            font.setBold(true);//粗体显示;
            
            styleTitle.setFont(font);
            
            //创建第一行
            HSSFRow row=sheet.createRow(0);

            row.setHeight((short)500);//设置行高
            HSSFCell cell=null;
            //插入第一行数据的表头
            for(int i=0;i<title.length;i++){
                sheet.setColumnWidth(i, 5000); //第一个参数代表列id(从0开始),第2个参数代表宽度值
                if(i==3) {
                    sheet.setColumnWidth(i, 10000); //第一个参数代表列id(从0开始),第2个参数代表宽度值
                }
                cell=row.createCell(i);
                cell.setCellValue(title[i]);
                cell.setCellStyle(styleTitle);
            }

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值