日期:2014-05-18  浏览次数:20987 次

怎么把DataGrid数据导出到word
怎么把DataGrid数据导出到word
Datagrid数据如下:
部门 计划 是否完成
人力资源部 方法 完成
人力资源部 试试 未完成
财务部 中国 完成
财务部 历史 完成
财务部 事实上 未完成

我要导出到WORD里如下:




------解决方案--------------------
/// <summary>
/// 导出Excel文件
/// </summary>
/// <param name="ControlId">要导出控件的Id</param>
/// <param name="Response">Page页的Response对象</param>
/// <param name="FileName">要导出的文件名</param>
public static void ResponseExcel(Control ControlId, HttpResponse Response, string FileName)
{
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); 
Response.AddHeader("Content-Disposition", "inline;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8) + ".xls");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
sb.Append("<html><head>");
sb.Append("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>"); 
sb.Append("</head><body>");
ControlId.RenderControl(hw);
sb.Append("</body></html>");
Response.Write(sb.ToString());
Response.End();
}
/// <summary>
/// 导出Word文件
/// </summary>
/// <param name="ControlId">要导出控件的Id</param>
/// <param name="Response">Page页的Response对象</param>
/// <param name="FileName">要导出的文件名</param>
public static void ResponseWord(Control ControlId, HttpResponse Response, string FileName)
{
Response.Clear();
Response.ContentType = "application/ms-word";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); 
Response.AddHeader("Content-Disposition", "inline;filename=" + HttpUtility.UrlEncode(FileName+".doc", Encoding.UTF8));
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
sb.Append("<html><head>");
sb.Append("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
sb.Append("</head><body>");
ControlId.RenderControl(hw);
sb.Append("</body></html>");
Response.Write(sb.ToString());
Response.End();
}
------解决方案--------------------
ding 


------解决方案--------------------
C# code

private void ExportToWord_Click(object sender, System.EventArgs e)
{
 Response.Clear();
 Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
 Response.Charset = "";
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 Response.ContentType = "application/vnd.word";
 System.IO.StringWriter stringWrite = new System.IO.StringWriter();
 System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(