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);
另附:完整小例子一个
开发环境: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导出样式设计
JS 基于ligerui excel 导出,采用poi方式,可实现冻结行列,图片,原样式等功能导出。
Java Poi 导出excel(支持各种设置字体、颜色、垂直居中)
POI操作Excel生成水印,该水印其实就是一个图片,该代码是将文字生成图片,再将图片变成水印的。
poi提供了excel转html的代码,但是吧,有好多问题。当有合并单元格时边框获取不到,单元格内的字体样式也转换不了,现在对poi提供的Tohtml.java做了一些修改,修复了这些问题,记录下,以备之后用到
poi 将多个excel复制到新的excel 的多个sheet页中 并复制所有的样式 包括字体的样式 背景颜色 单元格宽度 等
这个程序不光可以复制Excel的指定行数的内容而其还可以把其表格的样式复制。jar包也是齐全的,如果用的满意,还请多多支持
POI操作Excel 合并单元格 边框问题解决方法,这个方法是我亲自测试,并运用于项目中的,可以放心使用,还一并写出了很多注释,其中有写poi的bug的解决方法
NULL 博文链接:https://yxhcquedu.iteye.com/blog/828707
使用poi读取写入复杂excel内容包括样式,工具类
Java POI下载Excel模板 Excel带有下拉框的模板,并有详细注释,生成exlcel的下拉框,必填项标红,数字栏位防止科学计数法,并设置Excel标题样式
三、 POI EXCEL文档结构类 HSSFWorkbook excel文档对象 HSSFSheet excel的sheet HSSFRow excel的行 HSSFCell excel的单元格 HSSFFont excel字体 HSSFName 名称 HSSFDataFormat 日期格式 HSSFHeader sheet头...
关于java EXCEL poi 合并后无法显示或无法设置边框问题 【本人亲测】 excel poi 新手! 坑了1.5天!
JAVA开发中需要将表格中数据导出成EXCEL文件,其中的某一项又要设置为HTML格式,并支持点击链接,使用POI的开源方式导出时可以参考到此文件的内容!
看到现在网上excel转pdf的代码很少,在csdn上找到一个还不能用,只能做简单的转换,只好自己写了一个,代码是一个maven工程,用eclipse创建,支持单元格合并等复杂的excel,同时能同步单元格样式到pdf中。...
本POI实战为本人在业余时间写成,从浅开始,整个文档处于初到中级水平,包含大部分项目开发中会用到的常用功能,由于本人精力与能力有限,如有错误欢迎指正。...5.9 Excel样式实例 48 6.总结 52 7.附录 53
通过poi导出数据,可设置多种样式,例如:字体格式,单元格样式,列宽等,附:maven依赖jar。用法简单便于操作。
详解JAVA_POI导出EXCEL报表的操作(包括各种格式及样式的实现),最全面的JAVA_POI导出EXCEL报表操作。
利用poi对excel模板的新追加指定的行数,可以复制模板的某一行,包含样式。获取数据,往excel模板写入。包括各种对excel复制的操作,改变样式等等。集合很多对excel的操作的代码。都是工程实践用的。
该文件中包含了所有java项目中针对excel导入导出所需的jar包集合。 -------------------------------------- 将该zip解压后,将jar...应用场景:如 excel格式对账单导出,将数据库数据导出到excel中(带样式字体等)。