将数据库数据读出到DataGrid然后保存到excel中[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

    //关闭 ViewState
    EnableViewState = false;
    System.IO.StringWriter tw = new System.IO.StringWriter();//将信息写入字符串
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。
    //此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能
    //获取control的HTML
    dgExcel.RenderControl(hw);//将DATAGRID中的内容输出到HtmlTextWriter对象中
    // 把HTML写回浏览器
    Response.Write(tw.ToString());
    Response.End();
  }

 }
}

前台的页面:

<%@ Page language="c#" Codebehind="excel.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.excel" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>excel</title>
  <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="excel" method="post" runat="server">
   <TABLE id="Table1" style="Z-INDEX: 102; LEFT: 16px; WIDTH: 288px; POSITION: absolute; TOP: 16px; HEIGHT: 178px"
    cellSpacing="1" cellPadding="1" width="288" border="0">
    <TR>
     <TD>
      <asp:datagrid id="dgExcel" runat="server" Font-Names="宋体" Font-Size="9pt" Height="100%" Width="100%"
       BorderStyle="None" BorderWidth="1px" BorderColor="#CC9966" BackColor="White" CellPadding="4">
       <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
       <AlternatingItemStyle BackColor="#FFCC99"></AlternatingItemStyle>
       <ItemStyle BorderWidth="2px" ForeColor="#330099" BorderStyle="Solid" BorderColor="Black" BackColor="White"></ItemStyle>
       <HeaderStyle Font-Bold="True" HorizontalAlign="Center" BorderWidth="2px" ForeColor="#FFFFCC"
        BorderStyle="Solid" BorderColor="Black" BackColor="#990000"></HeaderStyle>
      </asp:datagrid></TD>
    </TR>
    <TR>
     <TD>
      <asp:Button id="btnGetExcel" runat="server" Text="把DataGrid中的保存到Excel"></asp:Button></TD>
    </TR>
   </TABLE>
  </form>
 </body>
</HTML>

本文关键:将数据库数据读出到DataGrid然后保存到excel中
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top