POI中可能会用到一些需要设置EXCEL单元格格式的操作小结:

先获取工作薄对象:

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheet = wb.createSheet();

HSSFCellStyle setBorder = wb.createCellStyle();

一、设置背景色:

setBorder.setFillForegroundColor((short) 13);// 设置背景色
setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

二、设置边框:

public static CellStyle getStyle(Workbook wb ){
CellStyle style = wb.createCellStyle();
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderTop(CellStyle.BORDER_THIN);

return style;
}

三、设置居中:

style.setAlignment(CellStyle.ALIGN_CENTER);//水平居中 
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直居中

四、设置字体:

HSSFFont font = wb.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 16);//设置字体大小

HSSFFont font2 = wb.createFont();
font2.setFontName("仿宋_GB2312");
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
font2.setFontHeightInPoints((short) 12);

setBorder.setFont(font);//选择需要用到的字体格式

五、设置列宽:

sheet.setColumnWidth(0, 3766); //第一个参数代表列id(从0开始),第2个参数代表宽度值

六、设置自动换行:

setBorder.setWrapText(true);//设置自动换行

七、合并单元格:

Region region1 = new Region(0, (short) 0, 0, (short) 6);

//参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号
sheet.addMergedRegion(region1);

八、加边框

HSSFCellStyle cellStyle= wookBook.createCellStyle();
  cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  cellStyle.setBorderBottom(HSSFCellStyle.BorderBORDER_MEDIUM);
  cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
  cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
  cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
  cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setTopBorderColor(HSSFColor.BLACK.index);

一个不完整的例子:

public static HSSFWorkbook generateExcelFiles(File file, StatisticQueryBean bean, List<WGLineBean> beans) {
HSSFWorkbook wb = null;
try {
InputStream inputStream = new FileInputStream(file);
wb = new HSSFWorkbook(inputStream);
HSSFSheet sheet = wb.getSheet("sheet1");
sheet.getRow(2).getCell(2).setCellValue(bean.getCarType());
sheet.getRow(2).getCell(4).setCellValue(bean.getArea());
sheet.getRow(2).getCell(6).setCellValue(bean.getStartTime() + "-" + bean.getEndTime());
CellStyle style = getStyle(wb);
int row = 4;
for (WGLineBean model : beans) {
HSSFRow rows = sheet.createRow(row);
int col = 0;
int quantity;
try {
quantity = model.getTotalQuantity();
} catch (Exception e) {
e.printStackTrace();
quantity = 0;
}
for (int i = 0; i < 15; i++) {
HSSFCell cell = rows.createCell(i);
cell.setCellStyle(style);
}
sheet.getRow(row).getCell(col++).setCellValue(model.getRecordTime());
sheet.getRow(row).getCell(col++).setCellValue(quantity - model.getSum_detection1());
sheet.getRow(row).getCell(col++).setCellValue(model.getSum_detection1());

sheet.getRow(row).getCell(col++).setCellValue(quantity - model.getSum_detection2());
sheet.getRow(row).getCell(col++).setCellValue(model.getSum_detection2());

sheet.getRow(row).getCell(col++).setCellValue(quantity - model.getSum_detection3());
sheet.getRow(row).getCell(col++).setCellValue(model.getSum_detection3());

sheet.getRow(row).getCell(col++).setCellValue(quantity - model.getSum_detection4());
sheet.getRow(row).getCell(col++).setCellValue(model.getSum_detection4());

sheet.getRow(row).getCell(col++).setCellValue(quantity - model.getSum_detection5());
sheet.getRow(row).getCell(col++).setCellValue(model.getSum_detection5());

sheet.getRow(row).getCell(col++).setCellValue(quantity - model.getSum_detection6());
sheet.getRow(row).getCell(col++).setCellValue(model.getSum_detection6());

sheet.getRow(row).getCell(col++).setCellValue(quantity - model.getSum_detection7());
sheet.getRow(row).getCell(col++).setCellValue(model.getSum_detection7());
row++;
}
} catch (Exception e) {
e.printStackTrace();
return wb;
}
return wb;
}

POI中excle样式怎么写的更多相关文章

  1. 嵌入式css样式,写在当前的文件中

    现在有一任务,把下面的“超酷的互联网”.“服务及时贴心”.“有趣易学”这三个短词文字字号修改为18px. 如果用内联式css样式的方法进行设置将是一件很头疼的事情(为每一个<span>标签 ...

  2. 如何javascript获取css中的样式

    obj.style.height只能获取行间样式,但是我们要怎么获取写在css文件中的样式呢? 首先我们要用一个新的方法currentStyle.这个方法由current和style两个单词组成意思是 ...

  3. Vue学习笔记七:Vue中的样式

    目录 两种样式 class样式 内联样式 两种样式 Vue中使用样式方式有两种,一种是class样式,一种是内联样式也就是style class样式 class样式使用的方式有5种,HTML如下 &l ...

  4. 防止vue文件中的样式出现‘污染’情况(html5 scoped特性)

    近期在项目中出现了vue样式污染的情况: 一个页面刚进去时样式不正常,刷新之后,样式才才达到预期那样 在vue中,如果把样式写在vue文件的 style中,可能会出现样式污染的情况,这是要把写样式的标 ...

  5. 3-5 Vue中的样式绑定

    Vue中的样式绑定: 本案例,简单设计一个<div>的点击绑定事件来改变div的样式效果 方法一:[class] ①(class和对象的绑定) //如上,运用class和一个对象的形式来解 ...

  6. Poi读取Excle报错 java.util.zip.ZipException: invalid stored block lengths

    一:Poi读取Excle报错  java.util.zip.ZipException: invalid stored block lengths 系统中需要导出excle签收单,excle模板是预设好 ...

  7. javascript总结40:DOM中操作样式的两种方式

    1 DOM中操作样式的两种方式 1 通过元素的style属性 注意: 通过style属性设置样式时,css中要写单位的属性,在js代码中也要加单位 //html <div id="bo ...

  8. 小程序中WXSS样式控制

    WXSS WXSS(WeiXin Style Sheets)是一套样式语言,用于描述 WXML 的组件样式. WXSS 用来决定 WXML 的组件应该怎么显示. 为了适应广大的前端开发者,WXSS 具 ...

  9. POI 设置Excel样式(转)

    POI 设置Excel样式 POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSh ...

随机推荐

  1. 【Python基础】lpthw - Exercise 41 学习面向对象术语

    一.专有词汇 类(class):告诉python创建新类型的东西. 对象(object):两个意思,即最基本的东西,或者某样东西的实例. 实例(instance):让python创建一个类时得到的东西 ...

  2. 前端开发-日常开发沉淀之git提交文件忽略

    .gitignore文件里添加需忽略的文件,或需要提交的文件 # Created by .ignore support plugin (hsz.mobi) ### VisualStudioCode t ...

  3. 用SharedPreference或文件的方式存储数据

    一.用SharedPreference存储数据 当程序有少量的数据需要保存,而这些数据的格式比较简单(例如一些配置信息),这个时候就可以使用SharedPreference来进行保存 下面例子将演示向 ...

  4. Operating Systems (COMP2006)

    Operating Systems (COMP2006) 1st Semester 2019Page 1, CRICOS Number: 00301JOperating Systems (COMP20 ...

  5. mysql数据库的查询,添加,删除,还原,备份

    18章数据mariadb数据库 1.setup 配置网卡centos6.52.nmtui 网卡图形配置界面3.yum install mariadb mariadb-server4.systemctl ...

  6. laravel 控制器类DB类操作

    例子:TrGo表(trgo_chip): laravel框架建立:TrGoModel <?php namespace TrChaos\Model; class TrGoModel extends ...

  7. oracle根据某个字段的值进行排序

    需求:按照颜色为蓝色.红色.黄色进行排序: order by  case                  when color = '蓝色' then                   1     ...

  8. node.js爬取ajax接口数据

    爬取页面数据与爬取接口数据,我还是觉得爬取接口数据更加简单一点,主要爬取一些分页的数据. 爬取步骤: 1.明确目标接口地址,举个例子 : https://www.vcg.com/api/common/ ...

  9. 关于windows 7 安装Django和基本使用命令

    一.安装 在安装前需注意Django 1.6以前的版本不支持python 3.×以上的版本. Django 2.×支持python 3.6 安装方法:打开cmd->输入pip install - ...

  10. shiro使用redis作为缓存,出现要清除缓存时报错 java.lang.Exception: Failed to deserialize at org.crazycake.shiro.SerializeUtils.deserialize(SerializeUtils.java:41) ~[shiro-redis-2.4.2.1-RELEASE.jar:na]

    shiro使用redis作为缓存,出现要清除缓存时报错 java.lang.Exception: Failed to deserialize at org.crazycake.shiro.Serial ...