在报表中设置的页眉页脚在页面中是看不到的,如下图:

页面中的效果:

在打印的时候,可以看到页眉页脚的效果:

那么,如果将页眉页脚导入到导出的excel中呢.我们可以通过API来进行设置:

<%@ pageimport="com.runqian.report4.model.*"%>

<%@ page import="com.runqian.report4.usermodel.*"%>

<%@ page import="com.runqian.report4.view.excel.*" %>

<%@ page import="com.runqian.report4.util.*" %>

<jsp:directive.pageimport="org.apache.poi2.hssf.usermodel.HSSFWorkbook"/>

<jsp:directive.pageimport="org.apache.poi2.hssf.usermodel.HSSFSheet"/>

<jsp:directive.page import="org.apache.poi2.hssf.usermodel.HSSFHeader"/>

<jsp:directive.pageimport="org.apache.poi2.hssf.usermodel.HSSFFooter"/>

<%

 //设置所有report对象的值为GBK转码

 request.setCharacterEncoding("GBK");

 

 String reportFile = request.getParameter("report");

 if( reportFile==null ){

  reportFile = "report_4.raq";

 }

 

 //第一步,读取报表模板

 InputStreamfis=application.getResourceAsStream("/reportFiles/"+reportFile);

 ReportDefine rd = (ReportDefine)ReportUtils.read( fis );

 

 //第二步,设置报表授权文件,运算报表

 Context context = new Context();

 //计算前,设置参数与宏

 ParamMetaData pmd = rd.getParamMetaData();

 String paramOrMocrName = "";

 if(pmd != null){

  for(int i = 0;i <pmd.getParamCount(); i ++){

   paramOrMocrName = pmd.getParam(i).getParamName();

   context.setParamValue(paramOrMocrName,request.getParameter(paramOrMocrName));

  }

 }

 MacroMetaData mmd = rd.getMacroMetaData();

 if( mmd != null ){

  for(int i = 0; i < mmd.getMacroCount(); i ++){

   paramOrMocrName = mmd.getMacro(i).getMacroName();

   context.setMacroValue(paramOrMocrName,request.getParameter(paramOrMocrName));

  }

 }

Engine enging = new Engine( rd, context);

 IReport iReport = enging.calc();

 

 //生成Excel文件,因为jsp里不能直接用response的out.print()方法,所以我们先在服务器上生成这个文件

   StringsaveFile=request.getRealPath("/reportFiles/")+"/"+reportFile+".xls";

  

   ExcelReport eReoprt = new ExcelReport();

   eReoprt.export(iReport);

 

   FileOutputStream fos = new FileOutputStream( saveFile );

    eReoprt.saveTo(fos);

   fos.close();

  

   HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(saveFile));

   wb.setSheetName(0,"表1");

   HSSFSheet  s=wb.getSheetAt(0);

   HSSFHeader hss = s.getHeader();

   //hss.setCenter("aaaa");

   StringBuffer stringBuffer = new StringBuffer();

   StringBuffer sb = new StringBuffer();

   int rowNum = iReport.getRowCount();

   int colNum = iReport.getColCount();

   sb.append("行数:"+rowNum+";列数:"+colNum);

  

   //部分(左中右),

   int centerStartCol =colNum/3+1;      //中间部分开始列

   int rightStartCol = colNum/3*2+colNum%3+1;   //右边部分开始列

   sb.append(";中间开始部分:"+centerStartCol+";右边开始部分:"+rightStartCol);

   //页眉左边部分

   

    for(int i=1,j=rowNum;i<=j;i++){

      IRowCell iRowCell=iReport.getRowCell(i);

    if( iRowCell.getRowType()==iRowCell.TYPE_PAGE_HEADER ){

     

      if(iRowCell.getRowVisible()!=false ){

       for(int m=1,n=centerStartCol;m<=n;m++){

        

        INormalCell cell =iReport.getCell(i,(short)m);

        sb.append(";左边部分:"+cell.isMerged());

        

        Object obj=cell.getValue();

        if(obj!=null){

        stringBuffer.append(obj.toString());

        HSSFHeader hh=s.getHeader();

        hh.setLeft(stringBuffer.toString());

       }

       }

       stringBuffer.append("\r");

      }

     }

    } 

  

  stringBuffer.delete(0,stringBuffer.length());

  //页眉中间部分

  System.out.println(sb.toString());

 

    for(int i=1,j=rowNum;i<=j;i++){

      IRowCell iRowCell=iReport.getRowCell(i);

    if( iRowCell.getRowType()==iRowCell.TYPE_PAGE_HEADER ){

     if(iRowCell.getRowVisible()!=false ){

      for(intm=rightStartCol,n=rightStartCol;m<=n;m++){

       

       INormalCell cell = iReport.getCell(i,(short)m);

       sb.append(";中间部分:"+cell.isMerged());

       

       Object obj=cell.getValue();

       if(obj!=null){

        stringBuffer.append(obj.toString());

       HSSFHeader hh=s.getHeader();

       hh.setCenter(stringBuffer.toString());

      }

      }

      stringBuffer.append("\r");

     }

    } 

   } 

 

  stringBuffer.delete(0,stringBuffer.length());

  System.out.println(sb.toString());

  //页眉右边部分

  

    for(int i=1,j=rowNum;i<=j;i++){

      IRowCell iRowCell=iReport.getRowCell(i);

    if( iRowCell.getRowType()==iRowCell.TYPE_PAGE_HEADER ){

     if(iRowCell.getRowVisible()!=false ){

      for(int m=centerStartCol,n=colNum;m<=n;m++){

       INormalCell cell =iReport.getCell(i,(short)m);

       sb.append(";右边部分:"+cell.isMerged());

       

       Object obj=cell.getValue();

       if(obj!=null){

        stringBuffer.append(obj.toString());

       HSSFHeader hh=s.getHeader();

       hh.setCenter(stringBuffer.toString());

      }

      }

      stringBuffer.append("\r");

     }

    } 

   }    

    for(int i=1,j=iReport.getColCount();i<=j;i++){

     IColCell irc=iReport.getColCell((short)i);

     

     if(irc.getBreakPage()){

     s.setColumnBreak((short)(i-1));

     }

    }

   //结束分页

   //页眉开始

  

   HSSFHeader hh=s.getHeader();

   System.out.println("日期&D:"+HSSFHeader.date());

   System.out.println("当前页&P:"+HSSFHeader.page());

   System.out.println("页数&N:"+HSSFHeader.numPages());

   System.out.println("时间&T:"+HSSFHeader.time());

   System.out.println("日期&D:"+HSSFHeader.date());

//页眉结束

   //设置页脚

   HSSFFooter footer = s.getFooter();

   footer.setCenter( "第 " + HSSFFooter.page() +"   
共 " + HSSFFooter.numPages());

   //页脚结束

   //标题行

  

    System.out.println("========="+stringBuffer);

    System.out.println("第一行第五列:"+iReport.getCell(1,(short)5).getValue().toString());

 //转跳到我们刚才生成的文件

 wb.write(new FileOutputStream(saveFile));

 System.out.println("Excel文件生成结束,文件保存在:"+saveFile);

 response.sendRedirect(request.getContextPath()+"/reportFiles/"+reportFile+".xls");

 for(int i=1;i<=iReport.getColCount();i++){

  System.out.println(iReport.getCell(1,(short)i).getValue());

 }

%>

如何利用API导出带有页眉页脚的excel的更多相关文章

  1. iText + Freemarker实现pdf的导出,支持中文、css以及图片,页眉页脚,页眉添加图片

    本文为了记录自己的实现过程,具体的实现步骤是参考博文 https://www.cnblogs.com/youzhibing/p/7692366.html 来实现的,只是在他的基础上添加了页眉页脚及页眉 ...

  2. itext 生成pdf文件添加页眉页脚

    原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpd ...

  3. LaTeX 页眉页脚的设置

    Latex中页眉页脚的设置 1. 首先要加页眉页脚的话,需要启动宏: 我通常用fancyhdr宏包来设置页眉和页脚. \usepackage{fancyhdr} 我们在 LaTeX 中先把 page ...

  4. 【Itext】7步制作Itext5页眉页脚pdf实现第几页共几页

    itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...

  5. 在C#中实现Word页眉页脚的全部功能

    页眉页脚经常使用于文章排版,在Word工具栏里.我们能够加入页眉,页脚,页码,日期和时间.图片等信息和内容.页眉/页脚有两个额外选项:首页不同,奇偶页不同.有时在不同的节(section)里插入不同的 ...

  6. 关于JavaScript打印去掉页眉页脚

    因为这个问题,Google和百度都查了个遍,网上主要解决方案都是这一个代码: <script language="JavaScript"> var hkey_root, ...

  7. 使用C#在word中插入页眉页脚

    //插入页脚 public void InsertFooter(string footer) { if (ActiveWindow.ActivePane.View.Type == WdViewType ...

  8. 【itext】7步制作兼容各种文档格式的Itext5页眉页脚 实现page x pf y

    itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...

  9. C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化

    转:http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html 1.新建Word文档 #region 新建Word文档/// &l ...

随机推荐

  1. 远程连接MySQL提示 Host is not allowed to connect to this MySQL server

    进入连接的主机修改系统数据库MySQL 下面的user表,把User= root(这里可能是其他你所需要连接的用户名)的这行数据的Host从localhost改为% 如下图: 修改完之后一定要重启My ...

  2. (转)Linux ldconfig 与 ldd指令

    原文:https://blog.csdn.net/iamzhangzhuping/article/details/49203981 一.ldconfig ldconfig是一个动态链接库管理命令,为了 ...

  3. python中map()函数

    map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. map()是 Python 内 ...

  4. Android 开发服务类 04_ServletForPOSTMethod

    ServletForPOSTMethod 业务类 package com.wangjialin.internet.servlet; import java.io.IOException; import ...

  5. Android 开发工具类 06_NetUtils

    跟网络相关的工具类: 1.判断网络是否连接: 2.判断是否是 wifi 连接: 3.打开网络设置界面: import android.app.Activity; import android.cont ...

  6. 如何删除Eclipse里某个工作空间?

    很多时候,一个Eclipse中或多或少的都会有那么几个工作空间(workspace),但是久而久之你会发现有些工作空间你觉得不再需要了或者觉得碍眼,怎么办? 其实很简单,方法有两种. 1.打开你的Ec ...

  7. 如何在虚拟机安装的Win10系统里快速打开【此电脑】图标?(图文详解)

    不多说,直接上干货! 为什么要写写这篇博客? 技多不压身,很多小技巧很重要,方便自己. 比如,对于这样的工具,个人来讲,玩过试用期,意味着你若不找点法子,是不行的.否则你没得玩了. 全网最详细的Tab ...

  8. Gradle-5.3:依赖-管理依赖的版本(传递(transitive)\排除(exclude)\强制(force)\动态版本(+))

    什么是传递依赖 在Maven仓库中,构件通过POM(一种XML文件)来描述相关信息以及传递性依赖.Gradle 可以通过分析该文件获取获取所以依赖以及依赖的依赖和依赖的依赖的依赖,为了更加直观的表述, ...

  9. WPF开发的彩票程序(练手好例子)

    前言 WPF是.NET最新的界面开发库,开发界面非常灵活!但是学习WPF难度也非常大. 应朋友之邀,编写了一个小程序.程序虽小,五脏俱全,WPF开发的灵活性可窥见一斑. 对于新手学习有很好的借鉴意义, ...

  10. Windows下整合apache和Tomcat

    1.前言: 引用:http://www.cnblogs.com/liaokailin/p/3963603.html 引用的博客里面有比较详细的说明,并且结合图片,非常生动: 我这里只做简单的说明和整合 ...