现在的位置: 首页 > 综合 > 正文

生成xls报表工具类

2013年10月02日 ⁄ 综合 ⁄ 共 3009字 ⁄ 字号 评论关闭

    通过工具类jxl编写的excel报表工具类,使得生成excel时更加方便,这也是本人总结多次,到现在为止(2011-10)的版本!

工具类下载链接地址http://download.csdn.net/detail/linlin_jiong/3717583

示例:

public static void main(String[] args){
  /**
   * 生成Excel报表
   * **/
  short tableBeginColumNum = 0;
  String nowDate = TimeOperate.getNowTime("yyyy-MM-dd");
  XlsCreateExport createExcelReport = new XlsCreateExport("工作情况报表("+nowDate+")");
  
  int tableColumnCount = 4;
  createExcelReport.creatRowMergeCell(1, tableBeginColumNum, (short)tableColumnCount, createExcelReport.getCellTableNameStyle(), "工作情况报表   --   "+nowDate);
  
  List<String[]> headerList = new ArrayList();
  headerList.add(new String[]{"分组","",""});
  headerList.add( new String[]{"工作区县","",""});
  headerList.add(new String[]{"拟发整改通知数 ","",""});
  headerList.add( new String[]{"督检通知数 ","",""});
  createExcelReport.createTrValues(tableBeginColumNum, createExcelReport.getCellTitleStyle(), headerList);
  
  List<String[]> countList = new ArrayList();
  headerList.add(new String[]{"合计","2",""});
  headerList.add( new String[]{"13","",""});
  headerList.add(new String[]{"35","",""});
  createExcelReport.createTrValues(tableBeginColumNum, countList);
  
  
  List<String[]> elementList = new ArrayList();
  headerList.add(new String[]{mergeKey+"朱斌忠组","大渡口区","2","5"});
  headerList.add( new String[]{mergeKey+"朱斌忠组","渝中区","5", "25"});
  headerList.add(new String[]{mergeKey+"余伟国组","渝中区 ","23", "29"});
  createExcelReport.createMergeRowsData(tableBeginColumNum, elementList);
  
//  createExcelReport.localSaveReport("f:/test.xls");
  /**********************************************/
 }

报表生成结果

 

excel报表展示jsp页面:

<%@ page language="java" import="java.util.Date" pageEncoding="gb2312"%>
<%@ page  import="java.text.SimpleDateFormat"%>

<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@ page import="javax.servlet.ServletOutputStream"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Excel报表数据</title>
</head>
<%

HSSFWorkbook workbook = (HSSFWorkbook)request.getAttribute("HSSFWorkbook");;
if(workbook == null){
 %>
 <label style="color:red;">生成失败</label>  
    <a href="#" onClick="window.close();return false;">关闭</a>
 <%
 return;
}

//若使用字节流进行文件下载,应该首先清空一下out对象,默认是字符流
out.clear();
//out = pageContext.pushBody();
//response.reset();

//通过设置头标题来显示文件传送到前端浏览器的文件信息
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
String formatDateStr = sf.format(new Date());

String filename = workbook.getSheetName(0)+"("+formatDateStr+").xls";
//需转换字体格式
filename = new String(filename.getBytes("gb2312"),"iso8859-1");
response.setHeader("Location",filename);
//response.setHeader("Content-Type","application/vnd.ms-excel");
//response.setHeader("Content-Disposition","attachment; filename=" + filename);
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=" + filename);

ServletOutputStream output = null;
try{
   output = response.getOutputStream();
   workbook.write(output);
}catch(Exception e){
   System.out.print(e.getMessage());
}finally{
 try{
  output.flush();
  output.close();
  output = null;
 }catch(Exception e){
  
 }
}

%>

<script type="text/javascript">
 window.close();
</script>

</html>

 

 

抱歉!评论已关闭.