• (三)JAVA使用POI操作excel


    1,单元格对齐方式

     Demo8.java

     1 package com.wishwzp.poi;
     2 
     3 import java.io.FileOutputStream;
     4 import java.util.Date;
     5 
     6 import org.apache.poi.hssf.usermodel.HSSFCell;
     7 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
     8 import org.apache.poi.hssf.usermodel.HSSFRichTextString;
     9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    10 import org.apache.poi.ss.usermodel.Cell;
    11 import org.apache.poi.ss.usermodel.CellStyle;
    12 import org.apache.poi.ss.usermodel.Row;
    13 import org.apache.poi.ss.usermodel.Sheet;
    14 import org.apache.poi.ss.usermodel.Workbook;
    15 
    16 public class Demo8 {
    17 
    18     public static void main(String[] args) throws Exception{
    19         Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
    20         Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
    21         Row row=sheet.createRow(2); // 创建一个行
    22         row.setHeightInPoints(30);//设置这一行的高度
    23         
    24         createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);//既在中间又在下边
    25         createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER);//要充满屏幕又要中间
    26         createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);//既在右边又在上边
    27         createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);//既在右边又在上边
    28         
    29         FileOutputStream fileOut=new FileOutputStream("d:\工作簿.xls");
    30         wb.write(fileOut);
    31         fileOut.close();
    32     }
    33     
    34     /**
    35      * 创建一个单元格并为其设定指定的对其方式
    36      * @param wb 工作簿
    37      * @param row 行
    38      * @param column  列
    39      * @param halign  水平方向对其方式
    40      * @param valign  垂直方向对其方式
    41      */
    42     private static void createCell(Workbook wb,Row row,short column,short halign,short valign){
    43         Cell cell=row.createCell(column);  // 创建单元格
    44         cell.setCellValue(new HSSFRichTextString("Align It"));  // 设置值
    45         
    46         CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式
    47         
    48         cellStyle.setAlignment(halign);  // 设置单元格水平方向对其方式
    49         cellStyle.setVerticalAlignment(valign); // 设置单元格垂直方向对其方式
    50         
    51         cell.setCellStyle(cellStyle); // 设置单元格样式
    52     }
    53     
    54 
    55 }

    结果显示:

     

     

    2,单元格边框处理

      Demo9.java

     1 package com.wishwzp.poi;
     2 
     3 import java.io.FileOutputStream;
     4 import java.util.Calendar;
     5 import java.util.Date;
     6 
     7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     8 import org.apache.poi.ss.usermodel.Cell;
     9 import org.apache.poi.ss.usermodel.CellStyle;
    10 import org.apache.poi.ss.usermodel.CreationHelper;
    11 import org.apache.poi.ss.usermodel.IndexedColors;
    12 import org.apache.poi.ss.usermodel.Row;
    13 import org.apache.poi.ss.usermodel.Sheet;
    14 import org.apache.poi.ss.usermodel.Workbook;
    15 
    16 public class Demo9 {
    17 
    18     public static void main(String[] args) throws Exception{
    19         Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
    20         Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
    21         Row row=sheet.createRow(1); // 创建一个行
    22         
    23         Cell cell=row.createCell(1); // 创建一个单元格
    24         cell.setCellValue(4);
    25         
    26         CellStyle cellStyle=wb.createCellStyle(); 
    27         cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部边框
    28         cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部边框颜色
    29         
    30         cellStyle.setBorderLeft(CellStyle.BORDER_THIN);  // 左边边框
    31         cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); // 左边边框颜色
    32         
    33         cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右边边框
    34         cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());  // 右边边框颜色
    35         
    36         cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框
    37         cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());  // 上边边框颜色
    38         
    39         cell.setCellStyle(cellStyle);
    40         FileOutputStream fileOut=new FileOutputStream("d:\工作簿.xls");
    41         wb.write(fileOut);
    42         fileOut.close();
    43     }
    44 }

    结果显示:

     

     

     


    3,单元格填充色和颜色操作

      Demo10.java

     1 package com.wishwzp.poi;
     2 
     3 import java.io.FileOutputStream;
     4 import java.util.Calendar;
     5 import java.util.Date;
     6 
     7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     8 import org.apache.poi.ss.usermodel.Cell;
     9 import org.apache.poi.ss.usermodel.CellStyle;
    10 import org.apache.poi.ss.usermodel.CreationHelper;
    11 import org.apache.poi.ss.usermodel.IndexedColors;
    12 import org.apache.poi.ss.usermodel.Row;
    13 import org.apache.poi.ss.usermodel.Sheet;
    14 import org.apache.poi.ss.usermodel.Workbook;
    15 
    16 public class Demo10 {
    17 
    18     public static void main(String[] args) throws Exception{
    19         Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
    20         Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
    21         Row row=sheet.createRow(1); // 创建一个行
    22         
    23         Cell cell=row.createCell(1);
    24         cell.setCellValue("XX");
    25         CellStyle cellStyle=wb.createCellStyle();
    26         cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色
    27         cellStyle.setFillPattern(CellStyle.BIG_SPOTS);  
    28         cell.setCellStyle(cellStyle);
    29         
    30         
    31         Cell cell2=row.createCell(2);
    32         cell2.setCellValue("YYY");
    33         CellStyle cellStyle2=wb.createCellStyle();
    34         cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色
    35         cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);  
    36         cell2.setCellStyle(cellStyle2);
    37         
    38         FileOutputStream fileOut=new FileOutputStream("d:\工作簿.xls");
    39         wb.write(fileOut);
    40         fileOut.close();
    41     }
    42 }

    结果显示:

     

     

     

    4,单元格合并

      Demo11.java

     1 package com.wishwzp.poi;
     2 
     3 import java.io.FileOutputStream;
     4 import java.util.Calendar;
     5 import java.util.Date;
     6 
     7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     8 import org.apache.poi.ss.usermodel.Cell;
     9 import org.apache.poi.ss.usermodel.CellStyle;
    10 import org.apache.poi.ss.usermodel.CreationHelper;
    11 import org.apache.poi.ss.usermodel.IndexedColors;
    12 import org.apache.poi.ss.usermodel.Row;
    13 import org.apache.poi.ss.usermodel.Sheet;
    14 import org.apache.poi.ss.usermodel.Workbook;
    15 import org.apache.poi.ss.util.CellRangeAddress;
    16 
    17 public class Demo11 {
    18 
    19     public static void main(String[] args) throws Exception{
    20         Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
    21         Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
    22         Row row=sheet.createRow(1); // 创建一个行
    23         
    24         Cell cell=row.createCell(1);
    25         cell.setCellValue("单元格合并测试");
    26         
    27         sheet.addMergedRegion(new CellRangeAddress(
    28                 1, // 起始行
    29                 2, // 结束行
    30                 1, // 其实列
    31                 2  // 结束列
    32         ));
    33         
    34         
    35         FileOutputStream fileOut=new FileOutputStream("d:\工作簿.xls");
    36         wb.write(fileOut);
    37         fileOut.close();
    38     }
    39 }

    结果显示:

  • 相关阅读:
    二维数组排序
    php-快速排序
    sql优化相关
    全页面静态化缓存
    php--1-100相加之和
    php--阶乘
    socket
    posix_getpid 函数win下失效问题
    水仙花数
    常用的魔术方法
  • 原文地址:https://www.cnblogs.com/wishwzp/p/5494346.html
Copyright © 2020-2023  润新知