java POI Excel 单元格样式
正如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 }
效果显示:

java POI Excel 单元格样式的更多相关文章
- 转:Java修改Excel单元格的数据及格式
https://blog.csdn.net/aking21alinjuju/article/details/6001153?locationNum=2 继前两节的Java读取.写入Excel后,本期将 ...
- poi 升级至4.x 的问题总结(POI Excel 单元格内容类型判断并取值)
POI Excel 单元格内容类型判断并取值 以前用 cell.getCachedFormulaResultType() 得到 type 升级到4后获取不到了 换为:cell.getCellType( ...
- java poi 合并单元格
java poi 合并单元格 2017年03月29日 16:39:01 翠烟你懊恼 阅读数:26561 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.n ...
- java poi 合并单元格后边框问题
在项目中用poi合并单元格,但发现边框会有不显示的问题. 在网上搜集了答案,来记录一下. 解决方法: 将每个没用到的单元格都设空值. 例如: HSSFCell cell = row.createCel ...
- java POI excel 导出复合样式(一个单元格两个字体)
前言:java poi 导出 excel 时,需要设置一个单元格有多个字体样式,有点类似于富文本. 想要达到的效果(一个单元格里): 我使用的 poi 版本是 <dependency> & ...
- POI HSSFCellStyle 设置 Excel 单元格样式
POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...
- Java 在Excel单元格中应用一种/多种字体样式
在Excel表格中,设置单元格字体样式时,可以对单元格内的所有字符应用同一样式,即获取指定单元,应用样式即可:另外也可以对单元格内的不同字符内容应用不同字体样式,即获取单元格中的字符位置,应用样式:本 ...
- java poi 获取单元格值时间
完整帮助类:JAVA poi 帮助类 /* * poi特殊日期格式:数字格式化成-yyyy年MM月dd日,格式 * */ private static ArrayList<String> ...
- JAVA poi设置单元格背景颜色
import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Ce ...
随机推荐
- 关于人人开源renren-fast-vue 中npm install各种报错的解决方案
首先吐槽一下,因为这个问题我整了好几天,把报错信息复制百度,试遍了各种方法,node.js我是卸载了安装,安装了卸载,甚至renren-fast-vue我也删了再下,然后再删,无限循环.然而没有什么软 ...
- Ansys Student 2020R2中Fluent编译UDF简介
使用内建编译器 在Ansys Fluent中编译UDF一般都需要额外安装相应版本的Visual Studio编译器,VS的缺点是体量大,占空间,安装后还需要额外进行相关设置才能正常使用.而新版本的An ...
- Machine Learning-特征工程之特征选择
特征工程之特征选择 目录 简介 1 Filter(过滤式选择) 1.1 移除低方差特征(variance threshold) 1.2 信息增益(information gain) 1.3 单变量特征 ...
- Python编程学习第三课之编程从Hello World开始
在搞定了前几节课的情况下,大家是否有一种想要跃跃欲试的赶脚,接下来就是我们开始练手的实战时刻. 每个编程人员入门编程的第一课都是向我们马上要进入的编程世界问好,"你好,世界"英文说 ...
- C++赋值兼容原则理解
–赋值兼容原则(派生类对象是基类对象,反之不成立)–基类指针强制转换成派生类指针–派生类中重定义基类成员(同名覆盖) 假设, 一个基类 "普通人", 一个派生类 "超人& ...
- P4715 【深基16.例1】淘汰赛
P4715 [深基16.例1]淘汰赛 题目描述 有 2^n(n≤7) 个国家参加世界杯决赛圈且进入淘汰赛环节.我经知道各个国家的能力值,且都不相等.能力值高的国家和能力值低的国家踢比赛时高者获胜.1 ...
- 【学习笔记/题解】树上启发式合并/CF600E Lomsat gelral
题目戳我 \(\text{Solution:}\) 树上启发式合并,是对普通暴力的一种优化. 考虑本题,最暴力的做法显然是暴力统计每一次的子树,为了避免其他子树影响,每次统计完子树都需要清空其信息. ...
- RHSA-2018:1200-重要: patch 安全更新(代码执行)
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 修复命令: 使用root账号登陆She ...
- git 本地回滚到上一个版本
linux下是 git reset --hard HEAD^ windows下是 git reset --hard HEAD"^"
- 多测师讲解pthon _函数__return_高级讲师肖sir
#函数中的返回的作用(return) 案例: #函数中的返回的作用:def fun(): #定义的一个函数 num =100 a=num/2 #print(a) #50.0 return a # pr ...