我将查询结果显示在表格中,要实现点"ToExcel"按钮,表格中的数据以excel的格式显示,但不需要提示保存或下载,只是直接在ie中以excel的格式打开,按后退还可以返回刚才的查询结果页面。
有以下几点要注意,
1.我的表格是在.cs文件中写出来的,即Response.Write("<table id='table1' border=1>");
2.不需要提示保存或下载,直接在ie中以excel的格式打开
3.按后退还可以返回刚才的查询结果页面

解决方案 »

  1.   

    function toExcel(tablename)
    //导出到excel
    {
      var mysheet=new ActiveXObject("OWC.Spreadsheet");
      with(mysheet)
      {
    DataType = "HTMLData";
    HTMLData =tablename.outerHTML;
    try{

      ActiveSheet.Cells(1,1).value="";
      ActiveSheet.Cells(2,1).value="";
    //   ActiveSheet.Cells(34,1).value="导出完毕";

      ActiveSheet.Export("c:\\前6周发货预测系数表.xls", 0);
     
      alert('导出完毕');
      window.close();
      };
    catch (e){
       alert('导出Excel表失败,请确定已安装Excel2000(或更高版本),并且没打开同名xls文件');
         window.close();
      };
    }
     }
      

  2.   

    Response.clear();
    Response.Write("<table><tr>.....<table">);
    Response.ContentType="Application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition","attachment;filename=ReqForQuote.xls");
    Response.End();
      

  3.   

    string path = Server.MapPath(this.xlfile.Text+".xls"); System.IO.FileInfo file = new System.IO.FileInfo(path);
    Response.Clear();
    Response.Charset="GB2312";
    Response.ContentEncoding=System.Text.Encoding.UTF8;
    // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
    // 添加头信息,指定文件大小,让浏览器能够显示下载进度
    Response.AddHeader("Content-Length", file.Length.ToString());

    // 指定返回的是一个不能被客户端读取的流,必须被下载
    Response.ContentType = "application/ms-excel";

    // 把文件流发送到客户端
    Response.WriteFile(file.FullName);
    // 停止页面的执行

    Response.End();
      

  4.   

    createDataSet(ref report);
    DataSet ds =new DataSet();
    ds =report;
    ds.Namespace="";
    //Response.ContentType = "text/html";
    Response.Clear();
    string httpHeader="attachment;filename=backup.xls";
    Response.AppendHeader("Content-Disposition", httpHeader);
    Response.Charset = "UTF-8";
    Response.BufferOutput = true;
    XmlDataDocument xdd =new XmlDataDocument(ds);
    System.Xml.Xsl.XslTransform xt =new XslTransform();
    if(type=="0")
    {
    xt.Load(Server.MapPath("1.xslt"));
    }
    else
    {
    xt.Load(Server.MapPath("2.xslt"));
    }
    xt.Transform(xdd,null,Response.OutputStream);
    Response.End();
      

  5.   

    实在不行的话,大家告诉我怎么把tablename传进去,
    比如,我的导出的事件中ToExcel(Tablename),但我的Table是在.cs文件出Response出来的,Response.Write("<table id='table1' border=1>");
    如果直接ToExcel(table1),编译通不过,大家谁知道有什么办法?谢谢!
      

  6.   

    Dim dgrdProducts As DataGrid = CType(Session("dgrdProducts"), DataGrid)
      If dgrdProducts Is Nothing Then
        Response.Write("<br>Error: Session('dgedProducts') Is Nothing!")    
        Response.End()
      End If 
      
      Dim sw As New StringWriter()
      Dim htw As New HtmlTextWriter(sw)  
      dgrdProducts.RenderControl(htw) 
      Response.ContentType = "application/vnd.ms-excel"
      Response.Write(sw.ToString())
      Response.End()
      

  7.   

    Excel.Application  oExcel;  
    Excel.Workbook  oBook;  
    Object  oMissing  =  System.Reflection.Missing.Value;  
    oExcel  =  new  Excel.Application();  
    oBook  =  oExcel.Workbooks.Add(oMissing);  
    for  (int  i=1;i<=4;i++)  
    {  
       oExcel.Cells[i,1]=i.ToString();  
       oExcel.Cells[i,2]="'bbb2";  
       oExcel.Cells[i,3]="'ccc3";  
       oExcel.Cells[i,4]="'aaa4";  
    }  
    oBook.Saved  =  true;  
    oExcel.UserControl  =  false;  
    string  mm=Server.MapPath(".")+"\\aa.xls";//服务器保存地址  
    oExcel.ActiveWorkbook.SaveCopyAs  (mm);  
    oExcel.Quit();  
    Response.Redirect  ("aa.xls");
      

  8.   

    点击 ToExcel,在服务器端生成一个excel文件,然后把他读进来,这样就在ie中打开了,然后可以把它删掉。
    不知可否?
      

  9.   

    Dim sw As New System.IO.StringWriter
            sw.Write("<html><body><TABLE border =1><tr><td>aaa</td><td>bbb</td></tr><tr><td colspan=2>afasdfasfasdfsadfsadfasf</td></tr></TABLE></body></html>")
            sw.WriteLine()
            Dim htw As New HtmlTextWriter(sw)
            dtlProducts.RenderControl(htw)
            Response.ContentType = "application/vnd.ms-excel"
            ' Response.Charset = ""         Response.Write(sw.ToString())
            Response.End()