导出文件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="导出" />
        <br /><br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="username" DataSourceID="LinqDataSource1">
            <Columns>
                <asp:BoundField DataField="username" HeaderText="用户名" ReadOnly="True" SortExpression="username" />
                <asp:BoundField DataField="password" HeaderText="密码" SortExpression="password" />
                <asp:BoundField DataField="nickname" HeaderText="昵称" SortExpression="nickname" />
                <asp:BoundField DataField="genderStr" HeaderText="性别" SortExpression="gender" />
                <asp:BoundField DataField="birthdayStr" HeaderText="生日" SortExpression="birthday" />
                <asp:BoundField DataField="area" HeaderText="地区" SortExpression="area" />
                <asp:BoundField DataField="nationName" HeaderText="民族" SortExpression="nation" />
            </Columns>
            <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        </asp:GridView>
        <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="mydbDataContext" EntityTypeName="" TableName="Users">
        </asp:LinqDataSource>
    </div>
    </form>
</body>
</html>
HTML页面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        using (mydbDataContext conn = new mydbDataContext())
        {
            List<Users> list = conn.Users.ToList();
            StringBuilder s = new StringBuilder();
            
            //拼接表格字符串
            s.Append("<table border=1>"); 
            
            //标题
            s.Append("<tr>");   
            s.Append("<td>用户名</td>");
            s.Append("<td>密码</td>");
            s.Append("<td>昵称</td>");
            s.Append("<td>性别</td>");
            s.Append("<td>生日</td>");
            s.Append("<td>地区</td>");
            s.Append("<td>民族</td>");
            s.Append("</tr>");

            //内容
            foreach (Users u in list)
            {
                s.Append("<tr>");
                s.Append("<td>"+u.username+"</td>");
                s.Append("<td>"+u.password+"</td>");
                s.Append("<td>"+u.nickname+"</td>");
                s.Append("<td>"+u.genderStr+"</td>");
                s.Append("<td>"+u.birthdayStr+"</td>");
                s.Append("<td>"+u.area+"</td>");
                s.Append("<td>"+u.nationName+"</td>");
                s.Append("</tr>");
            }
            s.Append("</table>");

            //文件保存路径
            string path = "file/" + DateTime.Now.ToString("yyyyMMdd") + "Users表数据.xlsx";
            
            //创建流
            StreamWriter sw = new StreamWriter(Server.MapPath(path));

            //写入文件
            sw.Write(s);

            //关闭流
            sw.Close();

            //下载
            Response.Redirect(path);
        }
    }
}
C#代码

 

posted @ 2016-10-29 21:15  庚xiao午  阅读(185)  评论(0编辑  收藏  举报