项目中用到java 对excel表格,在网上看到该段代码,感觉比较齐全,收藏备用
public ActionResult excelPrint() { HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件 HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet sheet.createFreezePane(1, 3);// 冻结 // 设置列宽 sheet.setColumnWidth(0, 1000); sheet.setColumnWidth(1, 3500); sheet.setColumnWidth(2, 3500); sheet.setColumnWidth(3, 6500); sheet.setColumnWidth(4, 6500); sheet.setColumnWidth(5, 6500); sheet.setColumnWidth(6, 6500); sheet.setColumnWidth(7, 2500); // Sheet样式 HSSFCellStyle sheetStyle = workbook.createCellStyle(); // 背景色的设定 sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index); // 前景色的设定 sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); // 填充模式 sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS); // 设置列的样式 for (int i = 0; i <= 14; i++) { sheet.setDefaultColumnStyle((short) i, sheetStyle); } // 设置字体 HSSFFont headfont = workbook.createFont(); headfont.setFontName("黑体"); headfont.setFontHeightInPoints((short) 22);// 字体大小 headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗 // 另一个样式 HSSFCellStyle headstyle = workbook.createCellStyle(); headstyle.setFont(headfont); headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 headstyle.setLocked(true); headstyle.setWrapText(true);// 自动换行 // 另一个字体样式 HSSFFont columnHeadFont = workbook.createFont(); columnHeadFont.setFontName("宋体"); columnHeadFont.setFontHeightInPoints((short) 10); columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 列头的样式 HSSFCellStyle columnHeadStyle = workbook.createCellStyle(); columnHeadStyle.setFont(columnHeadFont); columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 columnHeadStyle.setLocked(true); columnHeadStyle.setWrapText(true); columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色 columnHeadStyle.setBorderLeft((short) 1);// 边框的大小 columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色 columnHeadStyle.setBorderRight((short) 1);// 边框的大小 columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体 columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色 // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式) columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index); HSSFFont font = workbook.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 10); // 普通单元格样式 HSSFCellStyle style = workbook.createCellStyle(); style.setFont(font); style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中 style.setWrapText(true); style.setLeftBorderColor(HSSFColor.BLACK.index); style.setBorderLeft((short) 1); style.setRightBorderColor(HSSFColor.BLACK.index); style.setBorderRight((short) 1); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体 style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色. style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色. // 另一个样式 HSSFCellStyle centerstyle = workbook.createCellStyle(); centerstyle.setFont(font); centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中 centerstyle.setWrapText(true); centerstyle.setLeftBorderColor(HSSFColor.BLACK.index); centerstyle.setBorderLeft((short) 1); centerstyle.setRightBorderColor(HSSFColor.BLACK.index); centerstyle.setBorderRight((short) 1); centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体 centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色. centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色. try { // 创建第一行 HSSFRow row0 = sheet.createRow(0); // 设置行高 row0.setHeight((short) 900); // 创建第一列 HSSFCell cell0 = row0.createCell(0); cell0.setCellValue(new HSSFRichTextString("中非发展基金投资项目调度会工作落实情况对照表")); cell0.setCellStyle(headstyle); /** * 合并单元格 * 第一个参数:第一个单元格的行数(从0开始) * 第二个参数:第二个单元格的行数(从0开始) * 第三个参数:第一个单元格的列数(从0开始) * 第四个参数:第二个单元格的列数(从0开始) */ CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7); sheet.addMergedRegion(range); // 创建第二行 HSSFRow row1 = sheet.createRow(1); HSSFCell cell1 = row1.createCell(0); cell1.setCellValue(new HSSFRichTextString("本次会议时间:2009年8月31日 前次会议时间:2009年8月24日")); cell1.setCellStyle(centerstyle); // 合并单元格 range = new CellRangeAddress(1, 2, 0, 7); sheet.addMergedRegion(range); // 第三行 HSSFRow row2 = sheet.createRow(3); row2.setHeight((short) 750); HSSFCell cell = row2.createCell(0); cell.setCellValue(new HSSFRichTextString("责任者")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(1); cell.setCellValue(new HSSFRichTextString("成熟度排序")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(2); cell.setCellValue(new HSSFRichTextString("事项")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(3); cell.setCellValue(new HSSFRichTextString("前次会议要求\n/新项目的项目概要")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(4); cell.setCellValue(new HSSFRichTextString("上周工作进展")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(5); cell.setCellValue(new HSSFRichTextString("本周工作计划")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(6); cell.setCellValue(new HSSFRichTextString("问题和建议")); cell.setCellStyle(columnHeadStyle); cell = row2.createCell(7); cell.setCellValue(new HSSFRichTextString("备 注")); cell.setCellStyle(columnHeadStyle); // 访问数据库,得到数据集 List<DeitelVO> deitelVOList = getEntityManager().queryDeitelVOList(); int m = 4; int k = 4; for (int i = 0; i < deitelVOList.size(); i++) { DeitelVO vo = deitelVOList.get(i); String dname = vo.getDname(); List<Workinfo> workList = vo.getWorkInfoList(); HSSFRow row = sheet.createRow(m); cell = row.createCell(0); cell.setCellValue(new HSSFRichTextString(dname)); cell.setCellStyle(centerstyle); // 合并单元格 range = new CellRangeAddress(m, m + workList.size() - 1, 0, 0); sheet.addMergedRegion(range); m = m + workList.size(); for (int j = 0; j < workList.size(); j++) { Workinfo w = workList.get(j); // 遍历数据集创建Excel的行 row = sheet.getRow(k + j); if (null == row) { row = sheet.createRow(k + j); } cell = row.createCell(1); cell.setCellValue(w.getWnumber()); cell.setCellStyle(centerstyle); cell = row.createCell(2); cell.setCellValue(new HSSFRichTextString(w.getWitem())); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue(new HSSFRichTextString(w.getWmeting())); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue(new HSSFRichTextString(w.getWbweek())); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue(new HSSFRichTextString(w.getWtweek())); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue(new HSSFRichTextString(w.getWproblem())); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue(new HSSFRichTextString(w.getWremark())); cell.setCellStyle(style); } k = k + workList.size(); } // 列尾 int footRownumber = sheet.getLastRowNum(); HSSFRow footRow = sheet.createRow(footRownumber + 1); HSSFCell footRowcell = footRow.createCell(0); footRowcell.setCellValue(new HSSFRichTextString(" 审 定:XXX 审 核:XXX 汇 总:XX")); footRowcell.setCellStyle(centerstyle); range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0, 7); sheet.addMergedRegion(range); HttpServletResponse response = getResponse(); HttpServletRequest request = getRequest(); String filename = "未命名.xls";//设置下载时客户端Excel的名称 // 请见:http://zmx.iteye.com/blog/622529 filename = Util.encodeFilename(filename, request); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + filename); OutputStream ouputStream = response.getOutputStream(); workbook.write(ouputStream); ouputStream.flush(); ouputStream.close(); } catch (Exception e) { e.printStackTrace(); } return null; }
发表评论
-
HTTP协议详解
2014-03-26 21:15 550转自:http://www.cnblogs.com/li08 ... -
Spring mvc 使用context:exclude-filter 与 context:include-filter解决事物失效问题
2013-04-21 17:13 46851 在主容器中(applicationContext.xm ... -
java中的堆(heap)和栈(stack)
2013-03-31 23:16 722----对这两个概念的不明好久,终于找到一篇好文,拿来共享 ... -
java System.getproperty() 参数大全
2013-03-22 12:26 962System.getProperty()参数大全 ... -
rpc 方式调用webservice接口参数说明
2012-09-28 10:21 4478最经在项目中用到 webservice接口调用问题:需要使用 ... -
Java事务具体是什么东西
2012-06-20 12:55 985在学习spring的时候,看到事务管理,感觉事务只和数据库操作 ... -
java中静态导入
2012-05-04 21:33 726静态导入就是导入某一个类下边的一个或者所有的静态方法,如下: ... -
java基础学习
2012-05-03 18:41 6261、对于switch语句以前只知道匹配上了 执行相对应case ... -
java中基本数据类型默认值问题
2012-05-03 16:38 888以前经常看到java中int类型和Integet类型的变量初始 ... -
java程序中方法的形参使用final修饰分析
2012-05-02 22:56 2002final修饰的内容不可更改,但是在方法的形参中使用final ... -
switch(type)语句type类型
2012-05-02 22:24 1103之前经常见到面试题上问 switch(type)中type类型 ... -
java基本数据类型
2012-05-01 16:38 704大家都知道,java编程语言提供了基本数据类型,基本数据类型有 ... -
单体类生成实例方式分析
2012-04-30 22:49 729最近在看传智播客多线程部分线程间共享的时候看到了一个单体的使用 ... -
Overload和Override的区别。Overloaded的方法是否可以改变返回值的类型?
2012-04-30 19:49 913Overload和Override的区别。 ... -
线程安全分析
2012-04-28 22:54 739java内存模型分析: 我们都知道计算机有高速缓存的存在, ... -
java内部类
2012-04-28 22:24 684这段时间看了传智播客 ... -
多线程创建和运行---学习随笔
2012-04-28 21:46 860我们都知道多线程有两种创建方式: 1、通过thread子类创建 ...
相关推荐
HSSFWorkbook 模版使用的jar包,下载后导入项目的LIB中即可,相关操作见博文https://www.cnblogs.com/july7/p/11434058.html
HSSFWorkBooK用法
NPOI导出Excel的三种方式HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook
导出Excel 所 需 要 的 HSSFWorkbook的 jar包 简 单 便 捷
java excel HSSFWorkbook的jar包,具体的例子请看我的博客,读取多个sheet和单个sheet
java导出excel,简单实用。HSSFWorkbook的jar包。很好的解决java版到处excel。实现方式简单,请放心使用。
这个压缩包里有hssfworkbook的相关jar包,需要的话可以下载,设置1积分呢,是想攒点积分,取下别的东西,加我好友直接发你也行。
HSSFWorkbook C#web版excel,包含了.net 2.0和.net 4.0的dll程序集
NULL 博文链接:https://snow-163-com.iteye.com/blog/1470510
JFinal HSSFWorkbook导入Excel 并解析内容,将解析结果存入数据库
用HSSFWorkbook做的关于excel表格的导出,里面有详细的表格的格式设置。
HSSFWorkbook,XSSFWorkbook所需全部jar,包括poi-3.9-20121203.jar ,poi-ooxml-3.9.jar,poi-ooxml-schemas-4.0.0.jar,dom4j-1.6.1.jar ,xmlbeans-2.6.0.jar
HSSFWorkbook XSSFWorkbook poi-ooxml-3.10.1-20140818.jar
EXCEL2010 VBA常用代码 实战大全 示例文件 Wordbook(工作簿)对象
本文实例讲述了C#基于NPOI生成具有精确列宽行高的Excel文件的方法,是非常具有实用价值的技巧分享给大家供大家参考。具体方法如下:。 一、前言 NPOI是操作Excel的神器,导出导入快如闪电, 但是SetColumnWidth函数...
能稳定运行,windows 平台,ActiveXComponent技术
poi生成excel及合并单元格示例,详见文章http://blog.csdn.net/geloin/article/details/17219885
解释:该方法相当于新建了一个excel文件,HSSFWorkbook : excel的文档对象。 2.//创建第一个sheet(页),命名为 page1 Sheet sheet = wb.createSheet("page1"); 解释:该方法相当于在excel文件里新建了一个工作页...
POI操作Excel生成水印,该水印其实就是一个图片,该代码是将文字生成图片,再将图片变成水印的。