java poi excel 单元格样式_java poi批量导出excel 设置单元格样式
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个参数代表宽度值
//或者这个方式也可
sheet.setColumnWidth(0, 30 * 256);
六、设置自动换行:
setBorder.setWrapText(true);//设置自动换行
七、合并单元格:
Region region1 = new Region(0, (short) 0, 0, (short) 6);
//参数1:行号 参数2:起始列号 参数3:行号 参数4:终止列号
sheet.addMergedRegion(region1);
//或者这种方式(亲测好使)
//合并addMergedRegion方法四个属性分别是(开始行,结束行,开始列,结束列)
sheet.addMergedRegion(new CellRangeAddress(firstrow, lastrow, firstcol, lastcol));
八、加边框
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);
//如果设置行高
highRow.setHeight((short) (19*20));
例子:
@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);
}
java poi excel 单元格样式_java poi批量导出excel 设置单元格样式相关推荐
- java excel data 导入数据_java实现导入导出excel数据
项目需要,要实现一个导入导出excel的功能,于是,任务驱动着我学习到了POI和JXL这2个java操作Excel的插件. 一.POI和JXL介绍 1.POI:是对所有office资源进行读写的一套工 ...
- java导出excel 打不开_java – 无法使用AbstractExcelView导出Excel工作表
这是我的ExcelController.java public ModelAndView generateExcel(HttpServletRequest request, HttpServletRe ...
- POI批量导出Excel ZIP打包下载
POI批量导出Excel ZIP打包下载 1.公共抽象导出Excel类 需要自己实现两个抽象方法: getColumValueForColunmName : 扩展方法:根据名称判断来做值得转换 比如: ...
- 使用POI批量导出Excel文件(SSM)
文章目录 前言 如何使用POI批量导出Excel文件(SSM) 一.什么是POI? 模块 二.使用步骤 1.引入依赖 2.mapper层代码 包括Mapper接口.Mapper SQL代码 Mappe ...
- VBA应用笔记 -- 批量导出excel工作表中的图片
业务场景: 日常工作中,我们可能会遇到需要批量导出excel表中的图片的情况,按照网友的做法,批量导出excel中的图片主要有几种方法: 解压缩方法:可以通过将excel文件转成rar压缩文件,解压后 ...
- 如何用java POI在excel中画线_java poi对excel的操作详解
一. POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 二. HSSF概况 HSSF 是 ...
- java excel 多列排序_java poi处理excel多sheet并实现排序
需求:有一个数据字典全量汇总表,其中第一个sheet为目录,包括编号和表名,第二个以后为表的明细.其中sheet名就是表名但无序,sheet内字段序号无序有空行 现在要求将其中101,104,107, ...
- ApachePOI导出exce,设置单元格风格的属性和设置字体风格的属性的两个工具类,POI宽度和excel 像素转换
设置单元格风格的属性和设置字体风格的属性的两个工具类,POI宽度和excel 像素转换 /*** 功能:设置单元格风格的属性* */public static HSSFCellStyle SetCel ...
- java excel row遍历空_Java poi读取,写入Excel,处理row和cell可能为空的情况
首先需要导入包 import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.NP ...
最新文章
- java filter 失效_为何java中的过滤器filter不起作用
- Mysql使用trigger触发器说明
- Spring学习总结(18)——Spring整合Mysql数据库一主多从、多主多从配置
- Arduino作为服务器显示温度,基于Arduino 带LCD显示的电子温度计
- homelede软路由设置方法_斐讯无线路由器怎么设置 斐讯无线路由器设置方法【详解】...
- 许晨阳:平衡 成长 识别——数学竞赛与数学研究
- 我从Kaggle机器学习竞赛中获得的经验
- html5制作心路历程,一个真正0基础小白学习前端开发的心路历程
- android中倒计时控件CountDownTimer分析
- RotateMenu简单地旋转菜单控件《IT蓝豹》
- 二牛频道-收集分享各类不花钱的各类绿色优质软件-互联网资源分享
- 关于VMware ESX与VMware ESXi区别
- 数据库系列7:事务与锁的实现原理
- Linux系统编程之捕捉SIGCHLD
- 2.2 线性微分方程与常数变易法
- APK瘦身记,如何实现高达53%的压缩效果
- JavaScript前台判空
- Java模拟鼠标键盘输入事件 --- Robot 类
- ElasticSearch的_cat命令说明和其JavaAPI
- 领域驱动设计——柔性设计