码迷,mamicode.com
首页 > 其他好文 > 详细

导出信息

时间:2016-08-20 00:15:05      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

程序中可以将一些信息导出,以便用户下载使用,这里将简单的做一下导出表格,用car表做例子,直接用Linq连接到数据库

car表:

技术分享

方法一:

创建一个页面,放上一个按钮,在按钮中写事件,按钮id就叫做button1

 

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 Default1 : System.Web.UI.Page
{

    DataClassesDataContext con = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        //1、要导出哪些数据,需要一个泛型集合
        List<car> list = con.car.ToList();

        //2、拼接成table表格的格式
        StringBuilder str = new StringBuilder();//StringBuilder

        str.Append("<table border=‘1‘>");
        foreach (car c in list)//遍历这个集合,将每一条数据拼成一行,循环组成表格
        {
            str.Append("<tr>");
            str.Append("<td>" + c.ids + "</td>");
            str.Append("<td>" + c.code + "</td>");
            str.Append("<td>" + c.name + "</td>");
            str.Append("<td>" + c.brand + "</td>");
            str.Append("<td>" + c.time + "</td>");
            str.Append("<td>" + c.oil + "</td>");
            str.Append("<td>" + c.powers + "</td>");
            str.Append("<td>" + c.exhaust + "</td>");
            str.Append("<td>" + c.price + "</td>");
            str.Append("<td>" + c.pic + "</td>");
            str.Append("</tr>");
        }
        str.Append("</table>");
        //3、导出到服务器指定路径
        //为了每次导出名字不冲突,可以拼接上当前的日期时间,这里导出的是Excel表格,后缀名为xlsx
        string path = Server.MapPath("File/" + DateTime.Now.ToString("yyyyMMdd") + "car.xlsx");
        StreamWriter wr = new StreamWriter(path);//开启流指向绝对路径
        wr.Write(str);//将表格通过流通道写到指定位置
        wr.Close();//*********************************注意关闭流

        //4、给用户下载
        Response.Redirect("File/" + DateTime.Now.ToString("yyyyMMdd") + "car.xlsx");
    }
}

 

方法二:

创建一个页面,放上一个按钮,在按钮中写事件,按钮id就叫做button2,创建一个一般处理程序,点击按钮直接指向这个一般处理程序,在里面写,点击按钮不需要导出,直接下载(这个方法不怎么好用)

后台代码:直接在按钮事件中指向一般处理程序

 

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 Default1 : System.Web.UI.Page
{

    DataClassesDataContext con = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        Button2.Click += Button2_Click;
    }

    void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("aaaa.ashx");//指向一般处理程序
    }
}

 

一般处理程序

 

<%@ WebHandler Language="C#" Class="aaaa" %>

using System;
using System.Web;
using System.Linq;
using System.Data.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;

public class aaaa : IHttpHandler {
    DataClassesDataContext con = new DataClassesDataContext();
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "appliction/vnd.ms-excel";
        //context.Response.ContentType = "appliction/msword";
        List<car> list = con.car.ToList();

        StringBuilder str = new StringBuilder();
        str.Append("<table border=‘1‘>");
        foreach (car c in list)
        {
            str.Append("<tr>");
            str.Append("<td>" + c.ids + "</td>");
            str.Append("<td>" + c.code + "</td>");
            str.Append("<td>" + c.name + "</td>");
            str.Append("<td>" + c.brand + "</td>");
            str.Append("<td>" + c.time + "</td>");
            str.Append("<td>" + c.oil + "</td>");
            str.Append("<td>" + c.powers + "</td>");
            str.Append("<td>" + c.exhaust + "</td>");
            str.Append("<td>" + c.price + "</td>");
            str.Append("<td>" + c.pic + "</td>");
            str.Append("</tr>");
        }
        str.Append("</table>");

        context.Response.Write(str);
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

 

导出信息

标签:

原文地址:http://www.cnblogs.com/zyg316/p/5789384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!