使用apach-poi的一些随笔

        友情提示:本人使用的jar版本如下。
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>
        
        //创建一个excel文件
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = hssfWorkbook.createSheet("测试");
        //样式
        HSSFCellStyle style = hssfWorkbook.createCellStyle();
        
        //单元格
        style.setAlignment(HorizontalAlignment.CENTER);              //设置水平对齐的样式为居中对齐;
        style.setVerticalAlignment(VerticalAlignment.CENTER);     //设置垂直对齐的样式为居中对齐;   
        style.setBorderBottom(BorderStyle.THIN);     //下边框       
        style.setBorderLeft(BorderStyle.THIN);         //左边框      
        style.setBorderRight(BorderStyle.THIN);         //右边框
        style.setBorderTop(BorderStyle.THIN);         //上边框    

        //字体
        HSSFFont font = wb.createFont();            // 生成一个字体       
        font.setFontHeightInPoints((short) 16);        //字体大小    
        font.setBold(true);                             //加粗   
        font.setItalic(true);                        //倾斜
        font.setFontName("宋体");                    //字体
        font.setUnderline((byte) 2);                //下划线,1、单下划线,2、双下划线
        font.setColor((short) 4);                    //数字对应颜色,具体自测!  
        // 把字体 应用到当前样式
        style.setFont(font);
        
        HSSFRow row = sheet.createRow(0);            //创建一行单元格
        HSSFCell cell = row.createCell("行数");        //哪一个单元格
        cell.setCellStyle(style);
        cell.setCellValue(object.toString());

        //合并单元格,居中设置边框
        HSSFCell cell = row.createCell("行数");
        HSSFCellStyle style = hssfWorkbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);              //设置水平对齐的样式为居中对齐;
        style.setVerticalAlignment(VerticalAlignment.CENTER);     //设置垂直对齐的样式为居中对齐;   
        cell.setCellStyle(style);
        cell.setCellValue("设值");
        CellRangeAddress cellAddresses = new CellRangeAddress(起始行,结束行 , 起始列, 结束列);
        //设置合并后的单元格边框
        RegionUtil.setBorderLeft(BorderStyle.THIN,cellAddresses, sheet );       //左边框
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellAddresses, sheet );    //下边框
        RegionUtil.setBorderRight(BorderStyle.THIN, cellAddresses, sheet );      //右边框
        RegionUtil.setBorderTop(BorderStyle.THIN, cellAddresses, sheet );       //下边框

 

posted @ 2020-03-17 17:30  _不正  阅读(142)  评论(0编辑  收藏  举报