您的位置:首页 > 其它

DataTable导出到word或excel

2013-07-18 16:13 197 查看
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Drawing;

using System.IO;

using System.Text;

/// <summary>

/// Author:匆匆 Blog:http://www.cnblogs.com/huangjianhuakarl/

/// 将Gridview中的数据导出Excel表格

/// </summary>

public partial class GridviewExcel : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

bind();

}

}

/// <summary>

/// 数据绑定

/// </summary>

public void bind()

{

string sqlStr = "select * from Employee";

DataSet myds = Common.dataSet(sqlStr);

GridView1.DataSource = myds;

GridView1.DataKeyNames = new string[] { "ID" };

GridView1.DataBind();

}

/// <summary>

/// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

foreach (TableCell tc in e.Row.Cells)

{

tc.Attributes["style"] = "border-color:Black";

}

if (e.Row.RowIndex != -1)

{

int id = GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1;

e.Row.Cells[0].Text = id.ToString();

}

}

/// <summary>

/// 导出Excel

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void Button1_Click(object sender, EventArgs e)

{

Export("application/ms-excel", "Employee information.xls");

}

/// <summary>

/// 定义导出Excel的函数

/// </summary>

/// <param name="FileType"></param>

/// <param name="FileName"></param>

private void Export(string FileType, string FileName)

{

Response.Charset = "GB2312";

Response.ContentEncoding = System.Text.Encoding.UTF8;

Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());

Response.ContentType = FileType;

this.EnableViewState = false;

StringWriter tw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(tw);

GridView1.RenderControl(hw);

Response.Write(tw.ToString());

Response.End();

}

/// <summary>

/// 此方法必重写,否则会出错

/// </summary>

/// <param name="control"></param>

public override void VerifyRenderingInServerForm(Control control)

{

}

protected void Button2_Click(object sender, EventArgs e)

{

//Export("application/ms-excel", "Employee.doc");

Export("application/ms-word", "员工信息.doc");//都可以

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: