2

有没有办法将 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()
4

3 回答 3

2

我最好的猜测是,您正在定义大多数网格的颜色、列对齐方式等,通过classesCSS文件中定义,当您导出时,这些类将不再被引用。

要修复它,您需要使用内联样式,以便在将网格编写为 Word 文档时,呈现的 HTML 是自包含的,并且颜色和其他格式会被保留。

于 2012-08-31T20:09:30.743 回答
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();

http://forums.asp.net/t/1823755.aspx/1

于 2012-08-31T20:07:25.667 回答
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);
     }
于 2014-02-10T11:18:38.217 回答