我从数据库去出数据后,想以excel文件的格式显示在页面,但是没有成功,请教各位?

解决方案 »

  1.   

    大家看我的代码错在哪里? 谢谢 
    private void doExport(DataSet ds)
    {
                
                DateTime begin=DateTime.Now;
    Excel.Application excel= new Excel.Application();
    DateTime end=DateTime.Now;
                
    int rowIndex=1;
    int colIndex=0;
    excel.Application.Workbooks.Add(true);
                
        
    System.Data.DataTable table=ds.Tables[0] ;
    foreach(DataColumn col in table.Columns)
    {
    colIndex++;    
    excel.Cells[1,colIndex]=col.ColumnName;                
    } foreach(DataRow row in table.Rows)
    {
    rowIndex++;
    colIndex=0;
    foreach(DataColumn col in table.Columns)
    {
    colIndex++;
    excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
    }
    }
    excel.Visible=true;
    Process[] myProcesses;
    myProcesses = Process.GetProcessesByName("EXCEL");
    foreach(Process myProcess in myProcesses)
    {
    DateTime startTime = myProcess.StartTime; if(startTime > begin && startTime < end)
    {
    myProcess.Kill();
    }
    }
    }
      

  2.   

    HttpResponse response = HttpContext.Current.Response; 
    response.Clear();
    response.WriteFile(strFilePath);
    response.ContentType = "application/octet-stream";
    string httpHeader="inline;filename=backup.Xls";
    //filename=backup.Xls";
    response.AppendHeader("Content-Disposition", httpHeader );
    response.Flush();

    //System.IO.File.Delete(path + fileName);
    response.End();
    加上这句
      

  3.   

    谢谢 ,但我的excel文件 还不是一个实际文件,我只是在程序中声名了一个Excel.Application 对象,
    没有一个实际的路竟啊!
      

  4.   

    Dim f As System.IO.FileStream
            Dim sr As System.IO.BinaryReader
            Dim b() As Byte        f = New System.IO.FileStream(Server.MapPath("1.xls"), IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
            sr = New System.IO.BinaryReader(f, System.Text.Encoding.Default)
            b = sr.ReadBytes(f.Length)
            sr.Close()
            Response.ContentType = "Application/octet-stream"
            Response.Clear()
            Response.AddHeader("Content-Disposition", "filename = 1.xls")
            Response.BinaryWrite(b)
            Response.End()
      

  5.   

    谢谢,你看以下我的代码,我没有实际的文件1.xls!
      

  6.   

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebTest.WebForm1" %>
    <%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
      <HEAD>
    <title>WebForm1</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
    <meta content="Visual Basic .NET 7.1" name=CODE_LANGUAGE>
    <meta content=JavaScript name=vs_defaultClientScript>
    <meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
      </HEAD>
    <body>
    <form id=Form1 method=post runat="server"><asp:button id=Button1 runat="server" Text="Button"></asp:Button><asp:datagrid id=DataGrid1 runat="server"></asp:DataGrid></FORM>
    </body>
    </HTML>
      

  7.   

    private void Button1_Click(object sender, System.EventArgs e)
    {
    string excelFilePath=@"D:\Book1.xls";
    Excel.Application myExcel=new Excel.ApplicationClass( ) ;
    object oMissing = System.Reflection.Missing.Value ;
    myExcel.Application.Workbooks.Open(excelFilePath,oMissing,oMissing,oMissing,oMissing,oMissing, oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing) ;
    Excel.Workbook myBook = myExcel.Workbooks[1] ;
    Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1] ;
    System.Data.DataTable dt=new System.Data.DataTable("rycjsb");
    dt.Columns.Add("F1", System.Type.GetType("System.String"));
    dt.Columns.Add("F2", System.Type.GetType("System.String"));
    dt.Columns.Add("F3", System.Type.GetType("System.String"));
    dt.Columns.Add("F4", System.Type.GetType("System.String"));
    dt.Columns.Add("F5", System.Type.GetType("System.String"));
    DataSet myDs = new DataSet();
    myDs.Tables.Add(dt);
    DataRow myRow;
    myDs.Clear();
    for( int i = 2 ; i <= 4 ; i ++ )
    {
    myRow = myDs.Tables["rycjsb"].NewRow();
    for( int j = 1 ; j <= 5 ; j ++ )
    {

    Excel.Range r=(Excel.Range)mySheet.Cells[i,j];
    string strValue=r.Text.ToString();
    string aa=strValue;
    string columnname="F"+j.ToString();
    myRow[columnname]=strValue;
    }
    myDs.Tables["rycjsb"].Rows.Add(myRow);
    }

    DataGrid1.DataSource=myDs.Tables["rycjsb"].DefaultView;
    DataGrid1.DataBind();
    }