zoukankan      html  css  js  c++  java
  • java操作Excel之POI(2)

    一、设置单元格对齐方式:

     1 /**
     2      * 设置单元格对齐方式
     3      */
     4     public static void main(String[] args) throws Exception {
     5         Workbook wb = new HSSFWorkbook();    //定义一个新的工作簿
     6         Sheet sheet = wb.createSheet("第一个sheet页");
     7         Row row = sheet.createRow(2);                //创建第3行
     8         row.setHeightInPoints(30);                     //设置行高
     9         
    10         createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);
    11         createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER);
    12         createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);
    13         createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);
    14         
    15         FileOutputStream fileOut = new FileOutputStream("E:\工作簿.xls");
    16         wb.write(fileOut);
    17         fileOut.close();
    18     }
    19     /**
    20      * 创建一个单元格并为其设置指定的对齐方式
    21      * @param wb    工作簿
    22      * @param row   行
    23      * @param column  列
    24      * @param halign  水平方向对齐方式
    25      * @param valign  垂直方向对齐方式
    26      */
    27     @SuppressWarnings("unused")
    28     private static void createCell(Workbook wb, Row row, short column, short halign, short valign){
    29         Cell cell = row.createCell(column);                           //创建单元格
    30         cell.setCellValue(new HSSFRichTextString("Align It"));      //设置值
    31         CellStyle cellStyle = wb.createCellStyle();                    //创建单元格样式
    32         cellStyle.setAlignment(halign);                                //设置单元格水平方向对齐方式
    33         cellStyle.setVerticalAlignment(valign);                        //设置单元格垂直方向对齐方式
    34         cell.setCellStyle(cellStyle);
    35     }
    View Code

    二、设置单元格边框样式

     1 /**
     2      * 设置单元格边框样式
     3      */
     4     public static void main(String[] args) throws Exception {
     5         Workbook wb = new HSSFWorkbook();    
     6         Sheet sheet = wb.createSheet("sheet1");
     7         Row row = sheet.createRow(1);                
     8         Cell cell = row.createCell(1);                
     9         cell.setCellValue(4);
    10         
    11         CellStyle cellStyle = wb.createCellStyle();
    12         cellStyle.setBorderBottom(CellStyle.BORDER_THIN); //底部边框
    13         cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //底部边框颜色
    14         
    15         cellStyle.setBorderLeft(CellStyle.BORDER_THIN); //左边边框
    16         cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex()); //左边边框颜色
    17         
    18         cellStyle.setBorderRight(CellStyle.BORDER_THIN); 
    19         cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());  
    20         
    21         cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); 
    22         cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
    23         
    24         cell.setCellStyle(cellStyle);
    25         FileOutputStream fileOut = new FileOutputStream("E:\工作簿.xls");
    26         wb.write(fileOut);
    27         fileOut.close();
    28     }
    View Code

    三、设置单元格填充色和颜色操作

     1 /**
     2      * 设置单元格填充色和颜色操作
     3      */
     4     public static void main(String[] args) throws Exception {
     5         Workbook wb = new HSSFWorkbook();    
     6         Sheet sheet = wb.createSheet("sheet1");
     7         Row row = sheet.createRow(1);                
     8         
     9         Cell cell=row.createCell(1);
    10         cell.setCellValue("XX");
    11         CellStyle cellStyle=wb.createCellStyle();
    12         cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色
    13         cellStyle.setFillPattern(CellStyle.BIG_SPOTS);  
    14         cell.setCellStyle(cellStyle);
    15         
    16         
    17         Cell cell2=row.createCell(2);
    18         cell2.setCellValue("YYY");
    19         CellStyle cellStyle2=wb.createCellStyle();
    20         cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); //前景色
    21         cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);  
    22         cell2.setCellStyle(cellStyle2);
    23         
    24         FileOutputStream fileOut = new FileOutputStream("E:\工作簿.xls");
    25         wb.write(fileOut);
    26         fileOut.close();
    27     }
    View Code

    四、设置单元格合并

     1     /**
     2      * 设置单元格合并
     3      */
     4     public static void main(String[] args) throws Exception {
     5         Workbook wb = new HSSFWorkbook();    
     6         Sheet sheet = wb.createSheet("sheet1");
     7         Row row = sheet.createRow(1);                
     8         
     9         Cell cell=row.createCell(1);
    10         cell.setCellValue("单元格合并测试");
    11         
    12         sheet.addMergedRegion(new CellRangeAddress(
    13                 1, // 起始行
    14                 2, //结束行
    15                 1, //起始列
    16                 2  //结束列
    17         ));
    18         
    19         FileOutputStream fileOut = new FileOutputStream("E:\工作簿.xls");
    20         wb.write(fileOut);
    21         fileOut.close();
    22     }
    View Code

  • 相关阅读:
    nginx 配置https 负载均衡
    MyCAT+MySQL搭建高可用企业级数据库集群视频课程
    Java数字签名算法--RSA
    bootstrap在iframe框架中实现由子页面在顶级页面打开模态框(modal)
    bootstrap-treeview 自定义实现双击事件
    Java多线程之内存可见性
    Java实现责任链模式
    JVM(HotSpot) 7种垃圾收集器的特点及使用场景
    jQuery的noConflict以及插件扩展
    JavaScript事件漫谈
  • 原文地址:https://www.cnblogs.com/tenWood/p/6422214.html
Copyright © 2011-2022 走看看