jsp导出excel(原创)

<%@ page contentType="text/html; charset=utf-8" language="java"
 errorPage=""
 import="java.sql.*,java.util.*,com.huilin.smartstat.model.*,com.huilin.smartstat.tools.*,com.huilin.panda.dao.*,com.huilin.panda.model.*"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.apache.poi.hssf.usermodel.*,org.apache.poi.hssf.util.*,java.io.*"%>
<%@page import="javax.servlet.ServletOutputStream"%>
<%@page import="com.huilin.commons.db.DBConnectionFactory"%>
<%@page import="java.util.Date"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>stat</title>
<link href="../../css/main.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" media="all" href="css/theme.css" />
<link rel="stylesheet" type="text/css" media="all"
 href="css/calendar-win2k-cold-1.css" />
<script type="text/javascript" src="js/calendar.js"></script>
<script type="text/javascript" src="js/calendar-zh.js"></script>
<script type="text/javascript" src="js/calendar-setup.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%

 //String startTime = request.getParameter("startTime");
 //String endTime = request.getParameter("endTime");
 String title=request.getParameter("title");//标题
    String field_name[]=request.getParameter("field_name").split("\\,");//字段名
 List list=(List)request.getSession().getAttribute("list");//数据
    //System.out.println("list:"+list.size());
    // 声明一个工作薄
    HSSFWorkbook workbook = null;
    ServletOutputStream sos=null;
    workbook = new HSSFWorkbook();
    // 生成一个表格
    HSSFSheet sheet = workbook.createSheet(title);
    // 设置表格默认列宽度为15个字节
    sheet.setDefaultColumnWidth((short) 15);
   
    HSSFRow titlea = sheet.createRow(0);
    HSSFCell cella=titlea.createCell((short) 0);
   
    HSSFFont titlefont = workbook.createFont();
 titlefont.setFontName("黑体");
    titlefont.setCharSet((byte) 12);
 
    HSSFCellStyle titlestyle = workbook.createCellStyle();
 titlefont.setFontHeightInPoints((short) 14);
 titlestyle.setFont(titlefont);
 titlestyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 cella.setCellStyle(titlestyle);
 cella.setCellValue(title);
 HSSFRow row = sheet.createRow(1);
    sheet.addMergedRegion(new Region(0,(short) 0,0,(short)(field_name.length-1)));//合并单元格
     
   for (short i = 0; i < field_name.length; i++) { //产生表格标题行
        HSSFCell  cell = row.createCell(i);
        HSSFRichTextString text = new HSSFRichTextString(field_name[i]);
        cell.setCellValue(text);
       }
      
       int rowList=list.size()/field_name.length;//行数
        //System.out.println("rowList:"+rowList);
       int x=0;       
        for(int s=0;s<rowList;s++){
            row = sheet.createRow(s+2);
            for (short m = 0; m < field_name.length; m++) {
                row.createCell((short)m).setCellValue((String)list.get(x));
                x=x+1;
            }
        }
       
try {
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
         response.setHeader("Content-Disposition", "attachment;filename="
         + java.net.URLEncoder.encode("result", "UTF-8")+ ".xls");
  //创建文件输出流
  sos = response.getOutputStream();
  //将Excell工作空间写入到指定的Excell表格中去
  workbook.write(sos);
  sos.close();
  out.clear();
  out = pageContext.pushBody();

} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}
%>
   
</body>
</html>

你可能感兴趣的:(导出Excel)