以下为生成excel并设置数据
你可以将数据库里的数据取出,放在数组里,然后再参照以下赋值
Excel2.Application oExcel;
Excel2.Workbook oBook;
Object oMissing = System.Reflection.Missing.Value; // Create an instance of Excel.
oExcel = new Excel2.Application(); // Add a workbook.
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";
}
oExcel.Visible =true; }最后你必须做的是设置Dcomcnfg中的Microsoft Excel应用程序权限,否则Excel不能被正常调用。

解决方案 »

  1.   

    利用OWC,以下为利用OWC生成Excel的例子,
    <%@ 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  }
    } //查看以下这篇文章!
    http://www.wimdows.net/articles/article.aspx?aid=15
    会帮助你解决这个问题!