POI:单元格处理(对齐方式、边框、填充色、合并)

public static void main(String[] args) throws Exception{
        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
        Row row=sheet.createRow(2); // 创建一个行
        row.setHeightInPoints(30);
        
        createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);
        createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER);
        createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);
        createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);
        
        FileOutputStream fileOut=new FileOutputStream("c:\\工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }
    
    /**
     * 创建一个单元格并为其设定指定的对其方式
     * @param wb 工作簿
     * @param row 行
     * @param column  列
     * @param halign  水平方向对其方式
     * @param valign  垂直方向对其方式
     */
    private static void createCell(Workbook wb,Row row,short column,short halign,short valign){
        Cell cell=row.createCell(column);  // 创建单元格
        cell.setCellValue(new HSSFRichTextString("Align It"));  // 设置值
        CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式
        cellStyle.setAlignment(halign);  // 设置单元格水平方向对其方式
        cellStyle.setVerticalAlignment(valign); // 设置单元格垂直方向对其方式
        cell.setCellStyle(cellStyle); // 设置单元格样式

    }





public static void main(String[] args) throws Exception{
        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
        Row row=sheet.createRow(1); // 创建一个行
        
        Cell cell=row.createCell(1); // 创建一个单元格
        cell.setCellValue(4);
        
        CellStyle cellStyle=wb.createCellStyle();
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部边框
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部边框颜色
        
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);  // 左边边框
        cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex()); // 左边边框颜色
        
        cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右边边框
        cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());  // 右边边框颜色
        
        cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框
        cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());  // 上边边框颜色

        
        cell.setCellStyle(cellStyle);
        FileOutputStream fileOut=new FileOutputStream("c:\\工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }





public static void main(String[] args) throws Exception{
        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
        Row row=sheet.createRow(1); // 创建一个行
        
        Cell cell=row.createCell(1);
        cell.setCellValue("XX");
        CellStyle cellStyle=wb.createCellStyle();
        cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色
        cellStyle.setFillPattern(CellStyle.BIG_SPOTS);  
        cell.setCellStyle(cellStyle);

        
        
        Cell cell2=row.createCell(2);
        cell2.setCellValue("YYY");
        CellStyle cellStyle2=wb.createCellStyle();
        cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色
        cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);  

        cell2.setCellStyle(cellStyle2);
        
        FileOutputStream fileOut=new FileOutputStream("c:\\工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }






public static void main(String[] args) throws Exception{
        Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
        Row row=sheet.createRow(1); // 创建一个行
        
        Cell cell=row.createCell(1);
        cell.setCellValue("单元格合并测试");
        
        sheet.addMergedRegion(new CellRangeAddress(
                1, // 起始行
                2, // 结束行
                1, // 其实列
                2  // 结束列
        ));

        
        
        FileOutputStream fileOut=new FileOutputStream("c:\\工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值