有没有办法将gridview导出到MS word并保持gridview的格式。下面的代码用于导出gridview,但我无法弄清楚如何保持格式。 (我不能使用任何第三方软件)
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-word"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.RenderControl(hw)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
答案 0 :(得分:2)
我最好的猜测是,您通过classes
文件中定义的CSS
来定义网格的大部分颜色,列对齐等,导出时,无法再引用这些类。
要修复它,您需要使用内联样式,以便呈现的HTML是自包含的,并且在将网格写为Word文档时保留颜色和其他格式。
答案 1 :(得分:0)
来自forums.asp.net的此示例声称可以完成这项工作:
Response.AppendHeader("content-disposition", "attachment;filename=FileEName.doc");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-word";
this.EnableViewState = false;
Response.Write(word.InnerHtml);
Response.End();
答案 2 :(得分:0)
>First bind your grid
>Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment;
filename={0}", "Customers.doc"));
Response.Charset = "";
Response.ContentType = "application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Records.aspx.cs"
Inherits="Records"
MasterPageFile="~/Master.master" EnableEventValidation="false" %>
>use this code if img click event not fired
protected void mswrd_imgbtn_PreRender(object sender, EventArgs e)
{
ImageButton btn = sender as ImageButton;
ScriptManager sc = ScriptManager.GetCurrent(this.Page);
sc.RegisterPostBackControl(btn);
}