博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java POI Excel 单元格样式
阅读量:6938 次
发布时间:2019-06-27

本文共 6890 字,大约阅读时间需要 22 分钟。

正如Html需要CSS一样,我们的POI生成的Excel同样需要样式才能更完美的表现我们的数据。下面还是从简单的例子出发,学习和了解POI的样式设计。

  一、我的位置。

1 package com.myjava.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 ExcelStyle {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("我在这"));  // 设置值45         CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式46         cellStyle.setAlignment(halign);  // 设置单元格水平方向对其方式47         cellStyle.setVerticalAlignment(valign); // 设置单元格垂直方向对其方式48         cell.setCellStyle(cellStyle); // 设置单元格样式     }  }

 

  

  

  二、我的边框

 

1 package com.myjava.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 Border {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         cellStyle.setBorderLeft(CellStyle.BORDER_THIN);  // 左边边框30          cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); // 左边边框颜色31         32          cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右边边框33          cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());  // 右边边框颜色         34          cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框35          cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());  // 上边边框颜色36          37         cell.setCellStyle(cellStyle);38          FileOutputStream fileOut=new FileOutputStream("D:\\Border.xls");39          wb.write(fileOut);40          fileOut.close();41      }42 }

 

 

  效果显示:

  

  三、我的背景

  

package com.myjava.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 Bg {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("看不清我");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("我的前景色与众不同");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:\\bg.xls");39         wb.write(fileOut);40         fileOut.close();41     }42 }

  效果显示:

  

  四、合并单元格

  

package com.myjava.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 GetTogether {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:\\Together.xls");36         wb.write(fileOut);37         fileOut.close();38     }39 }

 

  效果显示:

  

转载于:https://www.cnblogs.com/yanjie-java/p/8329276.html

你可能感兴趣的文章
MySQL 通配符学习小结
查看>>
Java框架----SSH整合回顾
查看>>
我的学习笔记_Windows_HOOK编程 2009-12-03 11:19
查看>>
POJ 1845 (约数和+二分等比数列求和)
查看>>
Android tab_Host页面跳转,传值,刷新等问题汇总
查看>>
[kmp+dp] hdu 4628 Pieces
查看>>
反射简介—类型反射和晚期绑定
查看>>
Lintcode: Binary Tree Serialization (Serialization and Deserialization Of Binary Tree)
查看>>
[设计模式] 9 装饰者模式 Decorator
查看>>
beetle.express针对websocket的高性能处理
查看>>
bat批处理设置Java JDK系统环境变量文件
查看>>
Javascript的setTimeOut()和setInterval()的定时器用法
查看>>
HDU 4819 Mosaic D区段树
查看>>
商务部
查看>>
ASP.Net MVC开发基础学习笔记(5):区域、模板页与WebAPI初步
查看>>
python静态方法和类方法
查看>>
iOS实现地图半翻页效果--老代码备用参考
查看>>
走过电竞之路的程序员
查看>>
JQ 获取地址栏参数
查看>>
关于AFNetworking访问网络超时的设置
查看>>