I couldn't reproduce your problem, here is my testing code:<% @ Page Language="C#" %>
<% @ Import Namespace="System.IO"%>
<% @ Import Namespace="System.Data"%>
<% @ Import Namespace="System.Data.SqlClient"%>
<script Languagle="C#" runat="server">
public void Page_Load(Object src,EventArgs e)
{
string strconn ="server=localhost;uid=sa;pwd=;database=northwind";
string stracce ="Select * from Employees";
DataSet ds = new DataSet();

SqlDataAdapter Myadapter = new SqlDataAdapter (stracce, strconn);
Myadapter.Fill(ds,"Employees");

DataGrid1.DataSource=ds;
DataGrid1.DataMember ="Employees";
DataGrid1.DataBind(); Response.AddHeader("Content-Disposition", "attachment; filename=KenWang.xls"); 
Response.Charset = "UTF-8"; 
Response.ContentType = "application/vnd.ms-excel"; 
Response.Charset = ""; 
this.EnableViewState= false; 
StringWriter tw = new StringWriter(); 
HtmlTextWriter hw = new HtmlTextWriter(tw); 
DataGrid1.RenderControl(hw); 
Response.Write(tw.ToString()); 
Response.End(); }
</script>
<html>
<head>
<title>sample</title>
</head>
<body>
<form runat="server">
<asp:DataGrid id="DataGrid1" runat="server"
AutoGenerateColumns="true"
/>
</form>
</body>
</html>

解决方案 »

  1.   

    不过还有个问题:
    我有二个表没有任何关系,能导在一张sheet1表上吗?
      

  2.   

    yes, but use two grids, for example
    <% @ Page Language="C#" %>
    <% @ Import Namespace="System.IO"%>
    <% @ Import Namespace="System.Data"%>
    <% @ Import Namespace="System.Data.SqlClient"%>
    <script Languagle="C#" runat="server">
    public void Page_Load(Object src,EventArgs e)
    {
    string strconn ="server=localhost;uid=sa;pwd=;database=northwind";
    string stracce ="Select * from Employees;select * from Categories";
    DataSet ds = new DataSet(); SqlDataAdapter Myadapter = new SqlDataAdapter (stracce, strconn);
    Myadapter.Fill(ds);

    DataGrid1.DataSource=ds.Tables[0].DefaultView;
    DataGrid1.DataBind();
    DataGrid2.DataSource=ds.Tables[1].DefaultView;
    DataGrid2.DataBind(); Response.AddHeader("Content-Disposition", "attachment; filename=KenWang.xls"); 
    Response.Charset = "UTF-8"; 
    Response.ContentType = "application/vnd.ms-excel"; 
    Response.Charset = ""; 
    this.EnableViewState= false; 
    StringWriter tw = new StringWriter(); 
    HtmlTextWriter hw = new HtmlTextWriter(tw); 
    DataGrid1.RenderControl(hw); 
    DataGrid2.RenderControl(hw); 
    Response.Write(tw.ToString()); 
    Response.End(); }
    </script>
    <html>
    <head>
    <title>sample</title>
    </head>
    <body>
    <form runat="server">
    <asp:DataGrid id="DataGrid1" runat="server"
    AutoGenerateColumns="true"
    />
    <asp:DataGrid id="DataGrid2" runat="server"
    AutoGenerateColumns="true"
    />
    </form>
    </body>
    </html>
      

  3.   

    I think Excel takes a guess on your data and display them as numbers, you cannot control that in ASP.NET, the best you could probably do is to add a non-numeric character before '0001'
      

  4.   

    Select 'X' + Your字符Column  Your字符Column, Column2, Column3  from YourTable
      

  5.   

    only do it as you said
      

  6.   

    如果datagrid中有linkbutton的化,还是会报错的
    System.Web.HttpException: 类型“DataGridLinkButton”的控件“DataGrid1__ctl14__ctl1”必须放在具有 runat=server 的窗体标记内。楼主你是怎么解决的啊、
    help?
      

  7.   

    没做过,不知道,你可以问saucer.
      

  8.   

    高手,可惜我原是学VB的,正想学C#。