jsp导出excel

jsp导出excel有两种不同的方式 一种时网页直接导出 另一种是通过开源jar直接操作excel


一.网页导出格式

    导出excel按钮直接请求此页面,然后将要导出的值放到request里面即可

//此处必须添加 指定excel格式

<%@ page contentType="application/vnd.ms-excel; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%  
      response.addHeader("Content-Disposition", "filename=test.xls");  
%>
<html>
    <head>
        <meta name="Generator" content="Microsoft Excel 11">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>

    <body>
        <center>
            <b>表格名</b>
        </center>
        <br>
        <table border="1" align="center" cellpadding="0" cellspacing="1">
            <tr>
                <td>
                    姓名
                </td>
                <td>
                    年龄
                </td>
                <td>
                    生日
                </td>
            </tr>
            <c:forEach var="user" items="${userInfo}}">
                <tr>
                    <td>
                        ${user.userName}
                    </td>
                    <td>
                        ${user.age}
                    </td>
                    <td>
                        ${user.date}
                    </td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

二。利用开源jar POI


Servlet代码如下:直接请求Servelt,处理完成不需要跳转,这个请求会事另外一个页面跳转

response.setContentType("application/vnd.ms-excel;charset=UTF-8");// // 指定文件的保存类型。
POIExcel  we=new POIExcel();
we.getExcel("111.xls",response.getOutputStream(),list);


POIExcel代码:一看就知道什么意思


package com.sunjing.jquery;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class POIExcel {

    /**
     * 导出excel
     * @param sheetName sheet名称
     * @param output  响应输出流
     * @param list  导出结果集
     */
    public void getExcel(String sheetName, OutputStream output, List list) {
        HSSFWorkbook wb = new HSSFWorkbook();

        HSSFSheet sheet1 = wb.createSheet("sheet1");
        //创建题头行
        HSSFRow titleRow = sheet1.createRow((short) 0);
        //创建题头列0
        HSSFCell cell0= titleRow.createCell((short) 0);
        cell0.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell0.setCellValue("姓名");
        
        //创建题头列1
        HSSFCell cell1 = titleRow.createCell((short) 1);
        cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell1.setCellValue("年龄");
        
        //创建题头列2
        HSSFCell cell12 = titleRow.createCell((short) 2);
        cell12.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell12.setCellValue("出生日期");
        
        //创建内容行
        int rowBegin = 1;
        for (int i = 0; i < list.size(); i++) {
            UserInfo info = (UserInfo) list.get(i);
            HSSFRow contentRow = sheet1.createRow((short) rowBegin);
            
            HSSFCell contentCell0 = contentRow.createCell((short) 0);
            contentCell0.setEncoding(HSSFCell.ENCODING_UTF_16);
            //注意此处要判断是否为空 下面属性判断省略
            if(null != info.getUserName() && !"".equals(info.getUserName())){
                contentCell0.setCellValue(info.getUserName());
            }
            HSSFCell contentCell1 = contentRow.createCell((short) 1);
            contentCell1.setEncoding(HSSFCell.ENCODING_UTF_16);
            contentCell1.setCellValue(info.getAge().intValue());
            
            HSSFCell contentCell2 = contentRow.createCell((short) 2);
            contentCell2.setEncoding(HSSFCell.ENCODING_UTF_16);
            contentCell2.setCellValue(info.getDate());
            
            rowBegin++;
        }
        //FileOutputStream fileout=new FileOutputStream("workbook.xls");
        try {
            output.flush();
            wb.write(output);
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Output   is   closed ");
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tof21

支持原创

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值