• public ActionResult excelPrint() {
    HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件
    HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet
    sheet.createFreezePane(, );// 冻结
    // 设置列宽
    sheet.setColumnWidth(, );
    sheet.setColumnWidth(, );
    sheet.setColumnWidth(, );
    sheet.setColumnWidth(, );
    sheet.setColumnWidth(, );
    sheet.setColumnWidth(, );
    sheet.setColumnWidth(, );
    sheet.setColumnWidth(, );
    // 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 = ; i <= ; i++) {
    sheet.setDefaultColumnStyle((short) i, sheetStyle);
    }
    // 设置字体
    HSSFFont headfont = workbook.createFont();
    headfont.setFontName("黑体");
    headfont.setFontHeightInPoints((short) );// 字体大小
    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) );
    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) );// 边框的大小
    columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
    columnHeadStyle.setBorderRight((short) );// 边框的大小
    columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
    columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色
    // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
    columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index); HSSFFont font = workbook.createFont();
    font.setFontName("宋体");
    font.setFontHeightInPoints((short) );
    // 普通单元格样式
    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) );
    style.setRightBorderColor(HSSFColor.BLACK.index);
    style.setBorderRight((short) );
    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) );
    centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
    centerstyle.setBorderRight((short) );
    centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
    centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
    centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色. try {
    // 创建第一行
    HSSFRow row0 = sheet.createRow();
    // 设置行高
    row0.setHeight((short) );
    // 创建第一列
    HSSFCell cell0 = row0.createCell();
    cell0.setCellValue(new HSSFRichTextString("中非发展基金投资项目调度会工作落实情况对照表"));
    cell0.setCellStyle(headstyle);
    /**
    * 合并单元格
    * 第一个参数:第一个单元格的行数(从0开始)
    * 第二个参数:第二个单元格的行数(从0开始)
    * 第三个参数:第一个单元格的列数(从0开始)
    * 第四个参数:第二个单元格的列数(从0开始)
    */
    CellRangeAddress range = new CellRangeAddress(, , , );
    sheet.addMergedRegion(range);
    // 创建第二行
    HSSFRow row1 = sheet.createRow();
    HSSFCell cell1 = row1.createCell();
    cell1.setCellValue(new HSSFRichTextString("本次会议时间:2009年8月31日 前次会议时间:2009年8月24日"));
    cell1.setCellStyle(centerstyle);
    // 合并单元格
    range = new CellRangeAddress(, , , );
    sheet.addMergedRegion(range);
    // 第三行
    HSSFRow row2 = sheet.createRow();
    row2.setHeight((short) );
    HSSFCell cell = row2.createCell();
    cell.setCellValue(new HSSFRichTextString("责任者"));
    cell.setCellStyle(columnHeadStyle);
    cell = row2.createCell();
    cell.setCellValue(new HSSFRichTextString("成熟度排序"));
    cell.setCellStyle(columnHeadStyle);
    cell = row2.createCell();
    cell.setCellValue(new HSSFRichTextString("事项"));
    cell.setCellStyle(columnHeadStyle);
    cell = row2.createCell();
    cell.setCellValue(new HSSFRichTextString("前次会议要求\n/新项目的项目概要"));
    cell.setCellStyle(columnHeadStyle);
    cell = row2.createCell();
    cell.setCellValue(new HSSFRichTextString("上周工作进展"));
    cell.setCellStyle(columnHeadStyle);
    cell = row2.createCell();
    cell.setCellValue(new HSSFRichTextString("本周工作计划"));
    cell.setCellStyle(columnHeadStyle);
    cell = row2.createCell();
    cell.setCellValue(new HSSFRichTextString("问题和建议"));
    cell.setCellStyle(columnHeadStyle);
    cell = row2.createCell();
    cell.setCellValue(new HSSFRichTextString("备 注"));
    cell.setCellStyle(columnHeadStyle);
    // 访问数据库,得到数据集
    List<DeitelVO> deitelVOList = getEntityManager().queryDeitelVOList();
    int m = ;
    int k = ;
    for (int i = ; 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();
    cell.setCellValue(new HSSFRichTextString(dname));
    cell.setCellStyle(centerstyle);
    // 合并单元格
    range = new CellRangeAddress(m, m + workList.size() - , , );
    sheet.addMergedRegion(range);
    m = m + workList.size(); for (int j = ; 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();
    cell.setCellValue(w.getWnumber());
    cell.setCellStyle(centerstyle);
    cell = row.createCell();
    cell.setCellValue(new HSSFRichTextString(w.getWitem()));
    cell.setCellStyle(style);
    cell = row.createCell();
    cell.setCellValue(new HSSFRichTextString(w.getWmeting()));
    cell.setCellStyle(style);
    cell = row.createCell();
    cell.setCellValue(new HSSFRichTextString(w.getWbweek()));
    cell.setCellStyle(style);
    cell = row.createCell();
    cell.setCellValue(new HSSFRichTextString(w.getWtweek()));
    cell.setCellStyle(style);
    cell = row.createCell();
    cell.setCellValue(new HSSFRichTextString(w.getWproblem()));
    cell.setCellStyle(style);
    cell = row.createCell();
    cell.setCellValue(new HSSFRichTextString(w.getWremark()));
    cell.setCellStyle(style);
    }
    k = k + workList.size();
    }
    // 列尾
    int footRownumber = sheet.getLastRowNum();
    HSSFRow footRow = sheet.createRow(footRownumber + );
    HSSFCell footRowcell = footRow.createCell();
    footRowcell.setCellValue(new HSSFRichTextString(" 审 定:XXX 审 核:XXX 汇 总:XX"));
    footRowcell.setCellStyle(centerstyle);
    range = new CellRangeAddress(footRownumber + , footRownumber + , , );
    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;
    }

hssworkbook 用法案例的更多相关文章

  1. Java Me-List控件的用法案例

    /** * Java Me-List控件的用法案例 */package com.xushouwei.cn; import java.io.IOException;import javax.microe ...

  2. Scala进阶之路-Scala中的枚举用法案例展示

    Scala进阶之路-Scala中的枚举用法案例展示 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Scala中的枚举值和Java中的枚举值有点差别,不过使用起来也都差大同小异,我这 ...

  3. Hadoop基础-MapReduce的Partitioner用法案例

    Hadoop基础-MapReduce的Partitioner用法案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Partitioner关键代码剖析 1>.返回的分区号 ...

  4. Hadoop基础-MapReduce的Combiner用法案例

    Hadoop基础-MapReduce的Combiner用法案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编写年度最高气温统计 如上图说所示:有一个temp的文件,里面存放 ...

  5. jQuery中的ajax用法案例

    什么是 AJAX? AJAX = 异步 JavaScript 和 XML(Asynchronous JavaScript and XML). 简短地说,在不重载整个网页的情况下,AJAX 通过后台加载 ...

  6. gawk(awk)的用法案例

    gawk(awk)的用法案例 本文首先简单介绍一个gawk和awk的区别,然后是一点基本使用流程,最后是自己做的一个分析数据文件的脚本代码,供大家参考.另外想了解基本流程的入门知识的可以下载附件pdf ...

  7. Java8 新特性学习 Lambda表达式 和 Stream 用法案例

    Java8 新特性学习 Lambda表达式 和 Stream 用法案例 学习参考文章: https://www.cnblogs.com/coprince/p/8692972.html 1.使用lamb ...

  8. MVC4 jquery 样式 主题 用法(案例)

    MVC4已经自带了jquery,新建的项目,基本上什么都不用添加就可以运行,跑项目.(集成了那么多东西,jquery,modernizr,自带的默认权限,生成的模板,但是缺没有一个统一的文档或者什么去 ...

  9. atomic 原子自增工程用法案例

    案例 1 : 简单用法 atomic_int id; atomic_fetch_add(&id, 1) atomic_uint id; atomic_fetch_add(&id, 1) ...

随机推荐

  1. VC RichEdit中英文关键字标红

    最近需要做vc的RichEdit控件里的内容关键字标红,由于RichEdit的内容可能是中英文混合的,所以需要先转成Unicode,再用wcsstr函数找到关键字出现的位置,再用SetSel.SelS ...

  2. [How to] HBase的bulkload使用方法

    1.简介 将数据插入HBase表中的方法很多,我们可以通过TableOutputFormat以Mapreduce on HBase的方式将数据插入,也可以单纯的使用客户端API将数据插入.但是以上方法 ...

  3. highcharts自定义导出文件格式(csv) highcharts的一些使用心得

    highcharts是国外的一个图表插件,包括各种数据图形展示,柱形图,线性图等等,是手机端和pc端最好的图表插件之一,相比于百度的echarts更加轻便和易懂.链接http://www.hchart ...

  4. 使用extjs做的一个简单grid

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  5. Char 与 Byte

    var c: Char; b: Byte; begin c := 'A'; ShowMessage(c); //A b := ; ShowMessage(IntToStr(b)); c := Chr( ...

  6. HDU 3533 Escape(BFS+预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...

  7. JS中firstChild,lastChild,nodeValue属性

    childNodes 在JavaScript中,使用childNodes属性可以返回一个数组,这个数组包含给定元素节点的全体子节点. firstChild firstChild 这句代码等价于 目标元 ...

  8. [前端随笔][css] 弹性布局

    说在前面 弹性布局,顾名思义就是有弹性,能够根据屏幕/当前空间大小自由伸缩的.使用弹性布局可以很好的适应各种尺寸的客户端. 关键代码 display:flex; 设定元素为弹性布局 <文档传送门 ...

  9. springboot1.5.4 idea 自动保存编译更新

    maven dependencies增加 <dependency> <groupId>org.springframework.boot</groupId> < ...

  10. 【LOJ】#2131. 「NOI2015」寿司晚宴

    题解 怎么NOI2015D1--全是一眼秒的sb题--然后我代码全都写跪一遍= = 要是NOI2015是IOI赛制我就可以AK啦(大雾) 代码能力直线下降,NOI2018滚粗预定了啊TAT 我是不是要 ...