POI 设置Excel样式(转)
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); // 水平居中
setBorder.setVerticalAlignment(HSSFCellStyle.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);
另附:完整小例子一个
开发环境:IntelliJ IDEA 10.0.2
@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 设置Excel样式(转)的更多相关文章
- 【JAVA】POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写
POI设置EXCEL单元格格式为文本.小数.百分比.货币.日期.科学计数法和中文大写 博客分类:,本文内容转自 http://javacrazyer.iteye.com/blog/894850 FIL ...
- POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写
再读本篇文章之前,请先看我的前一篇文章,前一篇文章中有重点讲到POI设置EXCEL单元格格式为文本格式,剩下的设置小数.百分比.货币.日期.科学计数法和中文大写这些将在下面一一写出 以下将要介绍的每一 ...
- Poi设置列样式
最近做的项目中用到Poi导出Excel文件做模板,其中有的列需要设置为文本格式,查资料发现都是给单元格设置样式,由于是模板单元格都没内容,所以不能通过设置单元格式样式的方式操作,网上有说法是不能设置列 ...
- asp.net将页面内容按需导入Excel,并设置excel样式,下载文件(解决打开格式与扩展名指定的格式不统一的问题)
//请求一个excel类 Microsoft.Office.Interop.Excel.ApplicationClass excel = null; //创建 Workbook对象 Microsoft ...
- poi设置excel表格边框、字体等
POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...
- C# worksheet设置Excel样式
1.例子导出Excel的样式 样式代码 public void Exportdatagridviewtoexcel(string Textname) { SaveFileDialog savedial ...
- C# worksheet设置Excel样式(转载)
1.例子导出Excel的样式public void Exportdatagridviewtoexcel(string Textname) { SaveFileDialog savedialog = n ...
- Python设置Excel样式
前面已经详细讲解过使用Python对Excel表格进行读.写操作,本文主要讲解下使用Python设置Excel表格的样式. 深入学习请参考openpyxl官方文档: https://openpyxl. ...
- POI设置excel添加列下拉框
POI在生成excel模板时需要为列添加下拉框,我写了两个方法. @ 方法一:适用任何情况,不受下拉框值数量限制.但是需要通过引用其它列值. 思路大概如下: 1.创建一个隐藏的sheet页,用于存放下 ...
随机推荐
- 怎么让Intellj Idea 把数据库的表映射成hibernate的domain对象
步骤如下: 第一步:连接数据源: 点击:idea右边的database.如下图所示: 或者你依次点击:view-->Tool windows--->database 然后你将看在如下点击下 ...
- SpringMVC学习 -- IDEA 创建 HelloWorld
SpringMVC 概述: Spring 为展现层提供的基于 MVC 实际理念的优秀 Web 框架 , 是目前最主流的 MVC 框架之一. 自 Spring3.0 发布及 2013 年 Struts ...
- event loop 小记
水平不够,只能整理一下知乎大神的回答,勉强度日这样子 在一个事件循环里,会有两个主要的队列:task queue 和 micro-task quene. 其中 task 包括: script(整体代码 ...
- sencha touch 模仿tabpanel导航栏TabBar的实现代码
这篇文章介绍了sencha touch 模仿tabpanel导航栏TabBar的实例代码,有需要的朋友可以参考一下 基于sencha touch 2.2所写 效果图: 代码: /* *模仿tabpan ...
- 创建ipadWEB应用程序到主屏幕
1.webkit内核中的一些私有的meta标签,这些 meta标签在开发webapp时起到非常重要的作用 (1)<meta content="width=device-width; i ...
- 【BZOJ2818】Gcd [莫比乌斯反演]
Gcd Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1<=x,y&l ...
- HDU 1798 Tell me the area (数学)
题目链接 Problem Description There are two circles in the plane (shown in the below picture), there ...
- [bzoj2038][2009国家集训队]小Z的袜子(hose)——莫队算法
Brief Description 给定一个序列,您需要处理m个询问,每个询问形如[l,r],您需要回答在区间[l,r]中任意选取两个数相同的概率. Algorithm Design 莫队算法入门题目 ...
- wikioi 1245最小的N个和
2013-09-08 10:12 LRJ的算法竞赛入门经典训练指南里有类似的题,原题要难很多,p189页 读入A,B两组中的所有数后,建立N个有序表: A1+B1<A2+B1<A3+B1& ...
- 关于gsl库出现access violation 0X00000005问题的解决方法
gsl即GNU SCIENCE LIBRARY是一个强大c/c++的数值计算函数库. 在使用这一库出现access violation 0X00000005问题,尝试方法一在project->C ...