数据导出excel和word文件

您所在的位置:网站首页 excel数据提取到word 数据导出excel和word文件

数据导出excel和word文件

2022-03-26 16:05| 来源: 网络整理| 查看: 265

程序中可以将一些信息导出,以便用户下载使用,这里将简单的做一下导出表格,用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 list = con.car.ToList(); //2、拼接成table表格的格式 StringBuilder str = new StringBuilder();//StringBuilder str.Append(""); foreach (car c in list)//遍历这个集合,将每一条数据拼成一行,循环组成表格 { str.Append(""); str.Append("" + c.ids + ""); str.Append("" + c.code + ""); str.Append("" + c.name + ""); str.Append("" + c.brand + ""); str.Append("" + c.time + ""); str.Append("" + c.oil + ""); str.Append("" + c.powers + ""); str.Append("" + c.exhaust + ""); str.Append("" + c.price + ""); str.Append("" + c.pic + ""); str.Append(""); } str.Append(""); //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");//指向一般处理程序 } } 复制代码

一般处理程序

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 list = con.car.ToList(); StringBuilder str = new StringBuilder(); str.Append(""); foreach (car c in list) { str.Append(""); str.Append("" + c.ids + ""); str.Append("" + c.code + ""); str.Append("" + c.name + ""); str.Append("" + c.brand + ""); str.Append("" + c.time + ""); str.Append("" + c.oil + ""); str.Append("" + c.powers + ""); str.Append("" + c.exhaust + ""); str.Append("" + c.price + ""); str.Append("" + c.pic + ""); str.Append(""); } str.Append(""); context.Response.Write(str); context.Response.End(); } public bool IsReusable { get { return false; } } }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3