小山

付出最大努力,追求最高成就,享受最佳生活,收获无悔人生

博客园 首页 新随笔 联系 订阅 管理

1.html代码
<HTML>
    
<HEAD>
        
<title>WriteToCVS</title>
        
<meta content="False" name="vs_snapToGrid">
        
<meta content="Microsoft Visual Studio .NET 7.1" 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="Form1" method="post" runat="server">
            
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 14px; POSITION: absolute; TOP: 109px"
                runat
="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White"
                CellPadding
="4">
                
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
                
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
                
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
                
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
                
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
            
</asp:DataGrid>
            
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 18px; POSITION: absolute; TOP: 11px" runat="server"
                Text
="DataSet导出到csv文件" Width="148px"></asp:Button>
            
<asp:Button id="Button2" style="Z-INDEX: 103; LEFT: 19px; POSITION: absolute; TOP: 42px" runat="server"
                Text
="DataGrid导出到csv文件" Width="157px"></asp:Button>
            
<asp:Button id="Button3" style="Z-INDEX: 104; LEFT: 18px; POSITION: absolute; TOP: 73px" runat="server"
                Text
="DataGrid导出到Excel" Width="149px"></asp:Button>
        
</form>
    
</body>
</HTML>
2.cs代码
using System.IO;
using System.Data.SqlClient;

public class WriteToCVS : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
        
protected System.Web.UI.WebControls.Button Button2;
        
protected System.Web.UI.WebControls.Button Button3;
        
protected System.Web.UI.WebControls.Button Button1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
if(!Page.IsPostBack)
            
{
                
string sql="select * from TestGrid";
                ds
=GetDataSet(sql);
                
this.DataGrid1.DataSource=ds;
                
this.DataGrid1.DataBind();
            }

        }


        
WriteDSToCsv 传入dataset生成csv文件

        
WriteDatagridToCsv 传入datagrid生成csv文件

        
ToExcel

        
DelQuota

        
GetDataSet

        
Web Form Designer generated code

        
private void Button1_Click(object sender, System.EventArgs e)
        
{
            WriteDSToCsv(ds);
        }


        
private void Button2_Click(object sender, System.EventArgs e)
        
{
            WriteDatagridToCsv(
this.DataGrid1);        
        }


        
private void Button3_Click(object sender, System.EventArgs e)
        
{
            ToExcel(
this.DataGrid1,"meng");
        }


        
property
    }
3.数据库脚本
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestGrid]'and OBJECTPROPERTY(id, N'IsUserTable'= 1)
drop table [dbo].[TestGrid]
GO

CREATE TABLE [dbo].[TestGrid] (
    
[UserID] [int] IDENTITY (11NOT NULL ,
    
[UserName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[provinceID] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[cityID] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[areaID] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[Enabled] [bit] NULL 
ON [PRIMARY]
GO
posted on 2005-12-27 16:14  小山  阅读(6444)  评论(6编辑  收藏  举报