if you are ok with csv or tab-delimited data file, seeHOW TO: Use ASP.NET to Query and Display Database Data in Excel by Using Visual C# .NET
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q311194&

解决方案 »

  1.   

    you from using OWC on an Internet web server. Personally I think these particular licensing policies are way too strict. Anyway, you can find more info on licensing issues on OWC .Creating the COM wrapper for OWCVisual Studio .NET will automatically create the wrapper for you, when adding a reference to the Office Web Components library. Alternatively, when using a basic editor, you might have to create the wrapper yourself using the tlbimp.exe command line utility. We will look at these ways of creating the wrapper.We select Add Reference (right click on the project), select the COM tab, and select the Microsoft Office Web Components DLL. Click OK, and VS.NET will add the reference to your project and create a corresponding wrapper. You will see that the OWC reference has been added to your project.
    Using intelli-sense you can now explore the classes, interfaces, delegates and enums of the OWC ‘namespace’.
    To manually create the wrapper we use the tlbimp.exe utility. We can find the OWC COM DLL here: \Program Files\Microsoft Office\Office\MSOWC.DLL.
    We open up a command prompt and navigate to the directory where MSOWC.DLL resides. Then we issue the following command:
    tlbimp msowc.dll /out:dotnetmsowc.dll /namespace:OWC The /namespace option defines which namespace the wrapper will be known by.
    Now we need to move the dotnetmsowc.dll assembly, which contains our .NET wrapper class to the web application’s bin directory.
    To find out which classes, interfaces etc. are available (assuming we’re not using VS.NET), we can use the Intermediary Language Disassembler (ildasm.exe). Open ildasm (Start, Run, ildasm) and open the dotnetmsowc.dll assembly. We can now explore all classes, their constructors, methods, attributes etc. The web form and codebehind file
    Our web form will show the data in the DataGrid control. Additionally a textbox for the Excel file name to be exported and a button to perform the export will be displayed at the top of the page.
    Have a look at the ASPX file:
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="cominterop.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
      <HEAD>
        <title>Exporting Data To Excel, using COM Interop and OWC</title>
      </HEAD>
      <body>
        <form id="Form1" method="post" runat="server">
          Export File Name:
          <asp:TextBox ID="xlfile" Runat="server"></asp:TextBox>
          <asp:Button ID="export2excel" Runat="server" Text="Export to Excel" /><br />
          <br />
          <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
        </form>
      </body>
    </HTML>
     No rocket science there. Note that there is no OnClick or OnServerClick attribute for our ‘Export to Excel’ button. This is because VS.NET adds its own EventHandler for the button’s onclick event, as we can see when we have a look at the codebehind file.using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using OWC;namespace cominterop
    {
      public class WebForm1 : System.Web.UI.Page
      {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        private SqlCommand sql;
        protected System.Web.UI.WebControls.Button export2excel;
        protected System.Web.UI.WebControls.TextBox xlfile;
        private SqlConnection cnn;    private void Page_Load(object sender, System.EventArgs e)
        {
          this.BindDataGrid();      
        }    private void BindDataGrid() {
          cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;uid=sa;pwd=");
          sql = new SqlCommand("select * from products",cnn);
          cnn.Open();
          SqlDataReader reader = sql.ExecuteReader();      
          this.DataGrid1.DataSource = reader;
          this.DataGrid1.DataBind();
          reader.Close();
          cnn.Close();
        }    private void WriteDataGrid2Excel() {      
          SpreadsheetClass xlsheet = new SpreadsheetClass();
          cnn.Open();
          SqlDataReader reader = this.sql.ExecuteReader();
          int numbercols = reader.FieldCount;        
          int row=1;      
          while (reader.Read()) {
            for (int i=0;i<numbercols;i++) 
            {          
              xlsheet.ActiveSheet.Cells[row,i+1] = reader.GetValue(i).ToString();
            }
            row++;
          }      
          reader.Close();
          cnn.Close();
          xlsheet.ActiveSheet.Export(Server.MapPath(".")+"\\"+this.xlfile.Text,OWC.SheetExportActionEnum.ssExportActionNone);
        }    private void export2excel_Click(object sender, System.EventArgs e)
        {
          if (this.xlfile.Text.Trim()!="") 
          {
            this.WriteDataGrid2Excel();
          }
        }    #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
          InitializeComponent();
          base.OnInit(e);
        }
        
        private void InitializeComponent()
        { 
          this.export2excel.Click += new System.EventHandler(this.export2excel_Click);
          this.Load += new System.EventHandler(this.Page_Load);    }
        #endregion  }