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); // 设置单元格样式 49 } 50 51 52 }
效果显示:
二、我的边框
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 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:\\Border.xls"); 41 wb.write(fileOut); 42 fileOut.close(); 43 } 44 }
效果显示:
三、我的背景
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 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 }
效果显示:
四、合并单元格
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 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 }
效果显示:
学习这些知识,尝试做一个艺术气息的Excel吧。美化你的Excel,让数据“悦耳”起来。
转载于:https://www.cnblogs.com/jyh317/p/3817171.html
POI简易帮助文档系列--给Excel设置样式相关推荐
- jeecg导出excel设置样式XLS(03)与XLSX(07)
jeecg版本依赖 <modelVersion>4.0.0</modelVersion><groupId>org.jeecgframework.boot</g ...
- POI生成word文档,包括标题,段落,表格,统计图(非图片格式)
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能.POI为"P ...
- poi中文api文档
POI中文API文档 一. POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 二. HS ...
- poi动态创建文档_POI创建的文档具有不同条件的灵活样式
poi动态创建文档 介绍 这篇文章解释了基于各种标准将样式应用于文档的困难并提供了解决方案. Java编程中的常见任务之一是根据存储在数据库中的数据创建Excel报告. 在这些情况下,Java程序员使 ...
- poi操作word文档总结
POI分段落生成纯Word动态模板并导入数据 导出数据,可以用word另存为xml格式的ftl文件,变量用${变量名}表示,然后在类中通过 freemarker去替换变量. 但是怎么导入word数据. ...
- POI生成word文档完整案例及讲解
一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...
- 使用poi操作word文档实现套打功能
使用poi操作word文档实现套打功能 本文目的是为了分享一个实现套打功能,但是不同于简单的word的文本替换而是采用poi对word的文本框就行操作实现的功能: poi中各种jar的说明 套打的实现 ...
- POI生成word文档,图片显示为空白或不显示
我想要用java,通过poi实现word文档中插入文字和图片来发送邮箱附件.但是发现在对word操作中,图片是白的,size如果设置小了直接没有图片. 经过百度 参考解决 Java poi 3.8 ...
- JAVA 利用poi EXCLE模板文档导出数据
JAVA 利用poi EXCLE模板文档导出数据 1.导入jar包 下载地址:添加链接描述 提取码:xqkg 2.EXCLE模板 3.代码示例 package utill;import java.io ...
最新文章
- 资料分享:送你一本《数据结构(C语言版)》电子书!
- RecyclerView点击事件监听
- 2020-10-27(补码10000000为什么可以表示-128?)
- oracle10g的silent,Linux 静默(Silent) 安装Oracle 10g
- PL/SQL 存储过程学习2 条件语句
- 查看表扫描次数,并对比索引对表查询的作用
- hadoop集群崩溃恢复记录
- 非线性降维-核主成分分析KPCA
- libiconv android,iconv库 android ndk可运行
- Spring Boot mybatis HashMap +layui 通用分页
- “七”待已久,“夕”望是你,“快”来学习,“乐”在其中!
- 桌面cpu与服务器cpu天梯,桌面CPU性能排行 桌面CPU天梯图2017年6月最新版
- HiveSql常用的时间维度计算方法(月初、月末、周几)及时间维度 表生成
- 信息摘要技术 - SHA系列
- android media rw,Android中的“/ storage/udisk/sda4 /”和“/ mnt/media_rw/udisk/sda4 /”有什么区别?...
- Capture One 21 Pro v14.3.0.185 飞思顶级图像后期处理编辑软件
- custom的短语_custom的用法总结大全
- http中的204和205
- attention retain_饮人不知道的外卖店铺设计技巧,还不赶紧来看看?
- 基于51单片机超声波的停车场车位管理系统proteus仿真原理图PCB