首页 > 代码库 > 数据导出excel表格和Word文档

数据导出excel表格和Word文档

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 Button2_Click(object sender, EventArgs e)    {        Response.Redirect("aaaa.ashx");    }    void Button1_Click(object sender, EventArgs e)    {        //1、要导出哪些数据,需要一个泛型集合        List<car> list = con.car.ToList();        //2、拼接成table表格的格式        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>");        //3、导出到服务器指定路径        string path = Server.MapPath("File/" + DateTime.Now.ToString("yyyyMMdd") + "car.xlsx");        StreamWriter wr = new StreamWriter(path);        wr.Write(str);        wr.Close();//*********************************8        //4、给用户下载        Response.Redirect("File/" + DateTime.Now.ToString("yyyyMMdd") + "car.xlsx");    }}

数据导出excel表格和Word文档