程序代码: |
this.EnableViewState = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true); System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.DataGrid1.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End(); |
程序代码: |
<%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="WebUI.WebForm4" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm4</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:datagrid id="DataGrid1" runat="server" width="100%"> <ItemStyle HorizontalAlign="Center"></ItemStyle> <HeaderStyle HorizontalAlign="Center"></HeaderStyle> <FooterStyle HorizontalAlign="Center"></FooterStyle> <PagerStyle PageButtonCount="15" Mode="NumericPages"></PagerStyle> </asp:datagrid> <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 152px" runat="server" Text="Button"></asp:Button> </form> </body> </HTML> |
程序代码: |
private void Button1_Click(object sender, System.EventArgs e) { DataTable SourceTb = new DataTable(); DataColumn myDataColumn; myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "RowIndex"; //序 号 SourceTb.Columns.Add(myDataColumn); myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "CheckUpManName"; //审批人 SourceTb.Columns.Add(myDataColumn); myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "CheckUpIdeas"; //审批意见 SourceTb.Columns.Add(myDataColumn); myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "CheckUpDate"; //审批时间 SourceTb.Columns.Add(myDataColumn); myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "CheckUpRole"; //审批岗位 SourceTb.Columns.Add(myDataColumn); myDataColumn = new DataColumn(); myDataColumn.DataType = System.Type.GetType("System.String"); myDataColumn.ColumnName = "HandleType"; //操作类型(1:提交| 9:驳回) SourceTb.Columns.Add(myDataColumn); DataRow myDataRow; for(int i = 0;i < 30;i ++) { myDataRow = SourceTb.NewRow(); myDataRow["RowIndex"] = i.ToString(); myDataRow["CheckUpManName"] = "张三"; myDataRow["CheckUpIdeas"] = "同意"; myDataRow["CheckUpDate"] = "2006-03-20"; myDataRow["CheckUpRole"] = "物资部主任"; SourceTb.Rows.Add(myDataRow); } //绑定数据到DataGrid1 this.DataGrid1.DataSource = SourceTb.DefaultView; this.DataGrid1.DataBind(); //将DataGrid1构成的html代码写进StringWriter this.DataGrid1.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); this.DataGrid1.RenderControl(hw); string HtmlInfo = tw.ToString().Trim(); string DocFileName = "审批信息.xls"; string FilePathName = Request.PhysicalPath; FilePathName = FilePathName.Substring(0,FilePathName.LastIndexOf("\\")); //得到Excel文件的物理地址 FilePathName = FilePathName +"\\" + DocFileName; System.IO.File.Delete(FilePathName); FileStream Fs = new FileStream(FilePathName, FileMode.Create); BinaryWriter BWriter= new BinaryWriter(Fs,System.Text.Encoding.GetEncoding("GB18030")); //将DataGrid的信息写入Excel文件 BWriter.Write(HtmlInfo); BWriter.Close(); Fs.Close(); } |
欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛
https://bbs.txwb.com
关注天下网吧微信/下载天下网吧APP/天下网吧小程序,一起来超精彩
|
本文来源:vczx 作者:佚名