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

先获取工作薄对象:

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheet = wb.createSheet();

HSSFCellStyle setBorder = wb.createCellStyle();

一、设置背景色:

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

二、设置边框:

setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

三、设置居中:

setBorder.setAlignment(HSSFCellStyle.ALIGN_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);

例子:

@ResponseBody

@RequestMapping(value = "/reportForms/joinStocktaking/exportStorage.api")

public AjaxResponse exportStorage(@RequestBody StorageModel model) throws Exception {

if (logger.isDebugEnabled())

logger.debug("tmpdir is, {}", System.getProperty(JAVA_IO_TMPDIR));

int row = 1;

HSSFWorkbook workbook = new HSSFWorkbook();

HSSFSheet hssfSheet = workbook.createSheet();

HSSFCellStyle style = workbook.createCellStyle();

style.setFillBackgroundColor(HSSFCellStyle.LEAST_DOTS);

style.setFillPattern(HSSFCellStyle.LEAST_DOTS);

//设置Excel中的边框(表头的边框)

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);

style.setBottomBorderColor(HSSFColor.BLACK.index);

style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);

style.setLeftBorderColor(HSSFColor.BLACK.index);

style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);

style.setRightBorderColor(HSSFColor.BLACK.index);

style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);

style.setTopBorderColor(HSSFColor.BLACK.index);

//设置字体

HSSFFont font = workbook.createFont();

font.setFontHeightInPoints((short) 14); // 字体高度

font.setFontName(" 黑体 "); // 字体

style.setFont(font);

HSSFRow firstRow = hssfSheet.createRow((short) 0);

HSSFCell firstCell = firstRow.createCell(0);

firstRow.setHeight((short) 400);

//设置Excel中的背景

style.setFillForegroundColor(HSSFColor.GREEN.index);

style.setFillBackgroundColor(HSSFColor.GREEN.index);

firstCell.setCellValue(new HSSFRichTextString("库房"));

firstCell.setCellStyle(style);

HSSFCell secondCell = firstRow.createCell(1);

firstRow.setHeight((short) 400);

style.setFillForegroundColor(HSSFColor.GREEN.index);

style.setFillBackgroundColor(HSSFColor.GREEN.index);

secondCell.setCellValue(new HSSFRichTextString("库区"));

secondCell.setCellStyle(style);

HSSFCell threeCell = firstRow.createCell(2);

firstRow.setHeight((short) 400);

style.setFillForegroundColor(HSSFColor.GREEN.index);

style.setFillBackgroundColor(HSSFColor.GREEN.index);

threeCell.setCellValue(new HSSFRichTextString("物料编号"));

threeCell.setCellStyle(style);

HSSFCell fourCell = firstRow.createCell(3);

firstRow.setHeight((short) 400);

style.setFillForegroundColor(HSSFColor.GREEN.index);

style.setFillBackgroundColor(HSSFColor.GREEN.index);

fourCell.setCellValue(new HSSFRichTextString("物料名称"));

fourCell.setCellStyle(style);

HSSFCell fiveCell = firstRow.createCell(4);

firstRow.setHeight((short) 400);

style.setFillForegroundColor(HSSFColor.GREEN.index);

style.setFillBackgroundColor(HSSFColor.GREEN.index);

fiveCell.setCellValue(new HSSFRichTextString("在库数量"));

fiveCell.setCellStyle(style);

HSSFCell sixCell = firstRow.createCell(5);

firstRow.setHeight((short) 400);

style.setFillForegroundColor(HSSFColor.GREEN.index);

style.setFillBackgroundColor(HSSFColor.GREEN.index);

sixCell.setCellValue(new HSSFRichTextString("锁定数量"));

sixCell.setCellStyle(style);

//设置列宽

hssfSheet.setColumnWidth(0, 7000);

hssfSheet.setColumnWidth(1, 8000);

hssfSheet.setColumnWidth(2, 4000);

hssfSheet.setColumnWidth(3, 6000);

hssfSheet.setColumnWidth(4, 4000);

hssfSheet.setColumnWidth(5, 4000);

List list = joinStocktackingService.findjoinStorageByTerm(model.getWareHouse(), model.getStockArea(), model.getMaterialCode(), model.getMaterialName());

for (Object object : list) {

Object[] objects = (Object[]) object;

Storage storage = (Storage) objects[0];

Warehouse warehouse = (Warehouse) objects[1];

StockArea stockArea = (StockArea) objects[2];

Material material = (Material) objects[3];

//设置Excel中的边框

HSSFCellStyle cellStyle = workbook.createCellStyle();

cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cellStyle.setBorderBottom(HSSFCellStyle.BORDER_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);

HSSFRow hssfRow = hssfSheet.createRow((short) row);

HSSFCell firstHssfCell = hssfRow.createCell(0);//库房

firstHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

firstHssfCell.setCellValue(new HSSFRichTextString(warehouse.getName()));

firstHssfCell.setCellStyle(cellStyle);//设置单元格的样式

HSSFCell secondHssfCell = hssfRow.createCell(1);

secondHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

secondHssfCell.setCellValue(new HSSFRichTextString(stockArea.getName()));

secondHssfCell.setCellStyle(cellStyle);//设置单元格的样式

HSSFCell threeHssfCell = hssfRow.createCell(2);

threeHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

threeHssfCell.setCellValue(new HSSFRichTextString(material.getCode()));

threeHssfCell.setCellStyle(cellStyle);//设置单元格的样式

HSSFCell fourHssfCell = hssfRow.createCell(3);

fourHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

fourHssfCell.setCellValue(new HSSFRichTextString(material.getName()));

fourHssfCell.setCellStyle(cellStyle);//设置单元格的样式

HSSFCell fiveHssfCell = hssfRow.createCell(4);

fiveHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

fiveHssfCell.setCellValue(new HSSFRichTextString(String.valueOf(storage.getQty())));

fiveHssfCell.setCellStyle(cellStyle);//设置单元格的样式

HSSFCell sixHssfCell = hssfRow.createCell(5);

sixHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

sixHssfCell.setCellValue(new HSSFRichTextString(String.valueOf(storage.getQtyLocked())));

sixHssfCell.setCellStyle(cellStyle);//设置单元格的样式

row++;

}

String newFileName = String.format("%s.%s", "joinStocktaking-" + (new Date()).getTime(), "xls");

String uploadPath = FileUtils.contractPath(System.getProperty(JAVA_IO_TMPDIR), newFileName);

FileOutputStream fOut = new FileOutputStream(uploadPath);

workbook.write(fOut);

fOut.flush();

fOut.close();

return AjaxResponse.createSuccess(newFileName);

}

POI HSSFCellStyle 设置 Excel 单元格样式的更多相关文章

  1. POI中设置Excel单元格格式

    引用:http://apps.hi.baidu.com/share/detail/17249059 POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWork ...

  2. NPOI2.2.0.0实例详解(十)—设置EXCEL单元格【文本格式】 NPOI 单元格 格式设为文本 HSSFDataFormat

    NPOI2.2.0.0实例详解(十)—设置EXCEL单元格[文本格式] 2015年12月10日 09:55:17 阅读数:3150 using System; using System.Collect ...

  3. java POI Excel 单元格样式

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

  4. 【POI xlsx】使用POI对xlsx的单元格样式进行设置 / 使用POI对xlsx的字体进行设置

    涉及到的样式都在代码中有说明: package com.it.poiTest; import java.io.FileNotFoundException; import java.io.FileOut ...

  5. POI如何自动调整Excel单元格中字体的大小

    问题 目的是要将Excel中的文字全部显示出来,可以设置对齐格式为[缩小字体填充],但是这样的话只能展示出一行数据,字体会变得很小.还有一种办法,设置对齐格式为[自动换行],然后让单元格中的字体自动调 ...

  6. C#中的Excel操作【1】——设置Excel单元格的内容,打开Excel文件的一种方式

    前言 作为项目管理大队中的一员,在公司里面接触最多的就是Excel文件了,所以一开始就想从Excel入手,学习简单的二次开发,开始自己的编程之路! 程序界面 功能说明 打开文件按钮,可以由使用者指定要 ...

  7. POI设置excle单元格样式

    Java利用POI生成Excel强制换行 使用POI创建一个简单的   myXls.xls   文件       常用的包为   org.apache.poi.hssf.usermodel.*;    ...

  8. POI中设置Excel单元格格式样式(居中,字体,边框等)

    创建sheet什么的就不多说了,直接进入正题 HSSFCellStyle cellStyle = wb.createCellStyle();   一.设置背景色: cellStyle.setFillF ...

  9. POI 设置Excel单元格背景色(setFillForegroundColor)

    背景介绍:使用Java开发信息系统项目,项目中往往会涉及到报表管理部分,而Excel表格首当其冲称为最合适的选择,但是对单元格操作时对于设置单元格的背景颜色却很少提及,本文旨在方便单元格背景颜色设计. ...

随机推荐

  1. webstrom里面的html页面设置

    代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8 ...

  2. php中的接口interface

    * 接口 * 1.使用关键字:interface * 2.类是对象的模板,接口是类的模板 * 3.接口看作是一个特殊的类 * 4.接口中的方法,只声明不实现,与抽象类一样 * 5.接口中的方法必须是p ...

  3. cf1108e 线段树区间更新+扫描线

    /* 有点像扫描线 思路:从左到右枚举每个点,枚举到点i时,把所有以i为起点的区间的影响删去 再加上以i-1为结尾的区间的影响 */ #include<bits/stdc++.h> usi ...

  4. poj2279 线性dp

    #include<iostream> #include<cstdio> #include<cstring> #define ll long long using n ...

  5. Fiddler抓包10-会话框添加查看get与post请求类型

    前言 在使用fiddler抓包的时候,查看请求类型get和post每次只有点开该请求,在Inspectors才能查看get和post请求,不太方便.于是可以在会话框直接添加请求方式. 一.添加会话框菜 ...

  6. python+selenium+unittest 实现自动化测试

    示例代码: baidu.py import csv #导入csv模块 from itertools import islice #从itertools导入islice,后边让其默认跳过第一行使用 fr ...

  7. python 全栈开发,Day92(编程式的导航,vue页面布局,marked包的使用)

    昨日内容回顾 1. 组件间的传值 1. bus --> 空Vue对象 通过向bus对象抛出自定义事件的方式在组件间传递信息 2. 注意事项: 1. bus.$on()应该在组件mounted(挂 ...

  8. HDU1711 Number Sequence(KMP模板题)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. C/S权限系统得到拼音和五笔的自定义函数(二)

    得到五笔: CREATE FUNCTION [dbo].[fun_getWB](@Str VARCHAR(2000)) RETURNS VARCHAR(2000) AS BEGIN DECLARE @ ...

  10. PHP服务端支付宝支付及回调

    支付宝支付 (由app端自行调起支付宝/微信) 1.下载PHP版SDK 1 <?php 2 3 define('IN_ECS', true); 4 5 /*App支付 PHP服务端*/ 6 /* ...