希望大家多指点!!
public bool ExportDGV(DataGridView dgv, bool isShowWord)
        {
            Word.Document mydoc = new Word.Document();//实例化Word文档对象
            Word.Table mytable;//声明Word表格
            Word.Selection mysel;//声明Word选区
            Object myobj;
            if (dgv.Rows.Count == 0)
                return false;
            //建立Word对象
            Word.Application word = new Word.Application();
            myobj = System.Reflection.Missing.Value;
            mydoc = word.Documents.Add(ref myobj, ref myobj, ref myobj, ref myobj);
            word.Visible = isShowWord;
            mydoc.Select();
            mysel = word.Selection;
            //将数据生成Word表格文件
            mytable = mydoc.Tables.Add(mysel.Range, dgv.RowCount, dgv.ColumnCount, ref myobj, ref myobj);
            //设置列宽
            mytable.Columns.SetWidth(80, Word.WdRulerStyle.wdAdjustNone);
            //输出列标题数据
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                mytable.Cell(1, i + 1).Range.InsertAfter(dgv.Columns[i].HeaderText);
            }
            //输出控件中的记录
            for (int i = 0; i < dgv.RowCount - 1; i++)
            {
                for (int j = 0; j < dgv.ColumnCount; j++)
                {
                    mytable.Cell(i + 2, j + 1).Range.InsertAfter(dgv[j, i].Value.ToString());
                }
            }
            return true;
        }

解决方案 »

  1.   

    什么问题
    也可
    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();
        }
    public override void VerifyRenderingInServerForm(Control control)
        {
        }
      

  2.   

    这段代码 运行后 word里字体都排到外边了!!
      

  3.   

    用了楼主的代码,把DataGridView改成 DataTabel了字倒是没到两边去,就是打开WORD的时候很卡很卡,除非退出我自己的窗体。我勒个去。谁知道这是怎么回事
      

  4.   

    using   System; 
    using   System.Collections; 
    using   System.ComponentModel; 
    using   System.Data; 
    using   System.Drawing; 
    using   System.Web; 
    using   System.Web.SessionState; 
    using   System.Web.UI; 
    using   System.Web.UI.WebControls; 
    using   System.Web.UI.HtmlControls; 
    using   System.Reflection; namespace   OpenDoc 

    ///   <summary> 
    ///   Summary   description   for   WebForm1. 
    ///   </summary> 
    public   class   WebForm1   :   System.Web.UI.Page 

    protected   System.Web.UI.WebControls.Button   Button1; private   void   Page_Load(object   sender,   System.EventArgs   e) 

    //   Put   user   code   to   initialize   the   page   here 
    } #region   Web   Form   Designer   generated   code 
    override   protected   void   OnInit(EventArgs   e) 

    // 
    //   CODEGEN:   This   call   is   required   by   the   ASP.NET   Web   Form   Designer. 
    // 
    InitializeComponent(); 
    base.OnInit(e); 
    } ///   <summary> 
    ///   Required   method   for   Designer   support   -   do   not   modify 
    ///   the   contents   of   this   method   with   the   code   editor. 
    ///   </summary> 
    private   void   InitializeComponent() 
    {         
    this.Button1.Click   +=   new   System.EventHandler(this.Button1_Click); 
    this.Load   +=   new   System.EventHandler(this.Page_Load); } 
    #endregion private   void   Button1_Click(object   sender,   System.EventArgs   e) 

    Microsoft.Office.Interop.Word.Application   app   =   new   Microsoft.Office.Interop.Word.Application();     
    //Make   sure   that   Word   will   be   visible. 
    app.Visible=true; 
    //   Setting   these   variables   to   Missing.Value   is   comparable 
    //   to   not   providing   a   value   for   an   optional   parameter   in   VB. 
    //   This   gets   the   default   behavior. 
    string   path=Page.Request.MapPath( " "); 
    object   fileName   =   path   +   "/example3.doc "; 
    object   optional   =   Missing.Value; Microsoft.Office.Interop.Word._Document   doc   =   app.Documents.Open2000( ref   fileName, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional); 
    object   first=0; 
    object   last=doc.Characters.Count; 
    doc.Range(ref   first,   ref   last).Select(); doc.Range(ref   first,   ref   last).Cut(); fileName   +=   "_new "; doc.SaveAs2000( ref   fileName, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional, 
    ref   optional); object   saveChanges   =   true; 
    app.Quit(ref   saveChanges,   ref   optional,   ref   optional); } 

    }