在JSP中有个导出excel链接, 点击链接后弹出文件提示框(保存在本地的位置),点击保存后在本地生成Excel文件
怎么实现?

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【LOVEYOULOVE】截止到2008-06-24 16:08:37的历史汇总数据(不包括此帖):
    发帖数:15                 发帖分:680                
    结贴数:15                 结贴分:680                
    未结数:0                  未结分:0                  
    结贴率:100.00%            结分率:100.00%            
    敬礼!
      

  2.   

    我给你份源码吧你自己研究下%@ page contentType="application/vnd.ms-excel; charset=gb2312" language="java" import="java.sql.*"%>
    <%@ page import="java.io.IOException"%>
    <%@ page import="java.util.*"%>
    <%@ page import="jxl.Workbook"%>
    <%@ page import="jxl.format.UnderlineStyle"%>
    <%@ page import="jxl.write.*"%>
    <%@ page import="cn.dl.basedb.actionform.*"%>
    <%@ page import="cn.dl.basedb.dao.*"%>
    <%
    response.reset();
    response.setContentType("application/vnd.ms-excel");
    response.setContentType("APPLICATION/OCTET-STREAM ");
    response.setHeader("Content-Disposition",
    "attachment; filename=serLog.xls");
    List hisList = (List)session.getAttribute("list3");if(hisList ==null)
     response.sendRedirect("/index.jsp");
    String fieldName[] = { "姓名","份证号","市民邮箱账号","市民邮箱域","查询时间","查询请求IP","详细"};
    WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10,
    WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
    jxl.format.Colour.RED);
    WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
    try {
    titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
    } catch (WriteException e) {
    System.out.print(e);
    //log.info(e.toString());
    }
    WritableFont noteFont = new WritableFont(WritableFont.ARIAL, 10,
    WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
    jxl.format.Colour.BLACK);
    WritableCellFormat noteFormat = new WritableCellFormat(noteFont);
    try {
    noteFormat.setAlignment(jxl.format.Alignment.CENTRE);
    } catch (WriteException e) {
    //log.info(e.toString());
    }
    //response.setHeader("Content-disposition","attachment;filename=new.xls");
    WritableWorkbook writbook = null;
    try {
    writbook = Workbook.createWorkbook(response.getOutputStream());
    } catch (IOException e) {
    //log.info(e.toString());
    }
    WritableSheet sheet = writbook.createSheet("Sheet1", 0);
    sheet.setColumnView(0, 10);
    sheet.setColumnView(1, 20);
    sheet.setColumnView(2, 10);
    sheet.setColumnView(3, 15);
    sheet.setColumnView(4, 20);
    sheet.setColumnView(5, 15);
    sheet.setColumnView(6, 50);
    Label label = null;
    for (int i = 0; i < 7; i++) {
    label = new Label(i, 0, fieldName[i], titleFormat);
    try {
    sheet.addCell(label);
    } catch (WriteException e) {
    //log.info(e.toString());
    }
    } int row = 1;
    int column = 0;
    for (int i = 0; i < hisList.size(); i++) {
    PSearchLogForm psf = (PSearchLogForm) hisList.get(i);
    label = new Label(column, row, psf.getP_name(),noteFormat);
    try {
    sheet.addCell(label);
    } catch (WriteException e) {
    //log.info(e.toString());
    }
    column++;
    label = new Label(column, row, psf.getP_zjhm(),noteFormat);
    try {
    sheet.addCell(label);
    } catch (WriteException e) {
    //log.info(e.toString());
    }
    column++;
    label = new Label(column, row, psf.getPm_user(),
    noteFormat);
    try {
    sheet.addCell(label);
    } catch (WriteException e) {
    //log.info(e.toString());
    }
    column++;
    label = new Label(column, row, psf.getPm_domaion(), noteFormat);
    try {
    sheet.addCell(label);
    } catch (WriteException e) {
    //log.info(e.toString());
    }
    column++;
    label = new Label(column, row, psf.getPsl_time(), noteFormat);
    try {
    sheet.addCell(label);
    } catch (WriteException e) {
    //log.info(e.toString());
    }
    column++;
    label = new Label(column, row, psf.getPsl_ip(), noteFormat);
    try {
    sheet.addCell(label);
    } catch (WriteException e) {
    //log.info(e.toString());
    }
    column++;

    String psl_id = psf.getPsl_id().toString();
    PSearchLogDAO p = new PSearchLogDAO();
    List l = p.select("select * from p_search_log where psl_id="+psl_id);
    //out.print("select * from p_search_log where psl_id="+psl_id);
    PSearchLogForm psf1 = new PSearchLogForm();
    psf1 = (PSearchLogForm)l.get(0);
    String requestContent="select * from p_search_log where psl_id="+psl_id+psf1.getPsl_request()+"-"+psf1.getPsl_result();

    label = new Label(column, row, requestContent, noteFormat);
    try {
    sheet.addCell(label);
    } catch (WriteException e) {
    //log.info(e.toString());
    }
    column = 0;
    row++;
    } try {
    writbook.write();
    } catch (IOException e) {
    //log.info(e.toString());
    }
    try {
    writbook.close();
    } catch (Exception e) {
    //log.info(e.toString());
    }
    %>
      

  3.   


    import java.io.*;
    import java.util.*;
    import jxl.*;
    import jxl.write.Label;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WriteException;
    import jxl.write.biff.RowsExceededException;
    public class ExcelBean {

    public String expordExcel(List list,OutputStream os )throws Exception
    {

    jxl.write.WritableWorkbook wbook=Workbook.createWorkbook(os);//建立Excel文件
         String title="测试数据";
         jxl.write.WritableSheet sheet=wbook.createSheet("第一页", 0);//为文件的sheet设置名字
         //设置标题  start
           jxl.write.WritableFont wfont=new  jxl.write.WritableFont(WritableFont.ARIAL,16,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);  //设置标题的格式
           jxl.write.WritableCellFormat wcformat=new  jxl.write.WritableCellFormat(wfont);
           jxl.write.Label wlabel;
           wlabel=new jxl.write.Label(1,0,title,wcformat);//把标题放到excel文件中‘2’代表列,‘0’代表行,title是内容,wcformat是格式
     sheet.addCell(wlabel);
     //设置标题 end
     //设置表头  start
                     wfont=new jxl.write.WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.DARK_GREEN);  //设置表头的格式
                     wcformat=new jxl.write.WritableCellFormat(wfont);
                     wlabel=new jxl.write.Label(0,1,"TID",wcformat);
                     sheet.addCell(wlabel);
                     wlabel=new jxl.write.Label(2,1,"Tname",wcformat);
                     sheet.addCell(wlabel);
     //设置表头  end
                     
                 //输出数据格式的设置  start
                     
                     wfont=new jxl.write.WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,jxl.format.UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);  //设置表头的格式
                     wcformat=new jxl.write.WritableCellFormat(wfont);
                     for(int i=0;i<list.size();i++)
                     {
                      TestForm tf=(TestForm)list.get(i);
                      wlabel=new jxl.write.Label(0,i+2,tf.getTid(),wcformat);
                      sheet.addCell(wlabel);
                      wlabel=new jxl.write.Label(2,i+2,tf.getTname(),wcformat);
                      sheet.addCell(wlabel);
                     }
                     wbook.write();
                     wbook.close();
                 //输出数据格式的设置  end
     return "success";
    }}
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    import java.util.List;
    import java.io.*;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import com.business.TestBusiness;
    import com.struts.form.ExcelBean;
    /** 
     * MyEclipse Struts
     * Creation date: 03-20-2008
     * 
     * XDoclet definition:
     * @struts.action validate="true"
     */
    public class TestDownloadAction extends Action {
    /*
     * Generated Methods
     */ /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
    String filename="mytest";//文件名称
     TestBusiness tb=new TestBusiness();
     List list=tb.getData();//导出的数据集合
     try {
    OutputStream os=response.getOutputStream();//获取输出流
    response.reset();
    response.setHeader("Content-disposition", "attachment; filename=" + filename + ".xls");//设定输出文件头
    response.setContentType("application/msexcel");//设定输出类型
    ExcelBean eb=new ExcelBean();

    eb.expordExcel(list, os);

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    return mapping.findForward("success");
    }
    }
    <%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html:html locale="true">
      <head>
        <html:base />
        
        <title>Download.jsp</title> <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
        <html:form action="/testDownload">
        <html:submit value="daochu"></html:submit>
        </html:form>
      </body>
    </html:html>
      

  4.   

      ServletOutputStream out = response
        .getOutputStream();
      response.setContentType("application/excel");
      
      response.setHeader("Content-disposition",
        "attachment; " + "filename=temp.xls");  BufferedInputStream bis = null;
      BufferedOutputStream bos = null;
      try {
       FileInputStream fin = new FileInputStream(
         fileName);
       bis = new BufferedInputStream(fin);
       bos = new BufferedOutputStream(out);
       byte buff[] = new byte[1024];
       int bytesRead;
       while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
        bos.write(buff, 0, bytesRead);
      } catch (MalformedURLException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      } finally {
       if (bis != null)
        bis.close();
       if (bos != null)
        bos.close();
      }   
        } 
      

  5.   

    http://fengdaji.blog.163.com/blog/static/317721772008422638148/
    讲得比较清楚,楼主可以看看