C# WinForm如何把DataGridView的数据导出到Word?

解决方案 »

  1.   

    直接把你绑定到DataGridView的数据源导出来就行了.
    导出DataTable的方法网上有很多.
      

  2.   

    如果没装WORD,可以写成HTML的编码,导出,文件成DOC格式即可,
    如果装了word,就更好做了.
      

  3.   

    下面代码部分略写word.document mydoc=new word.document();
    word.table mytable;
    word.selection mysel;
    object myobj;
    if(dgv.rows.count>0)
    {
    word.application word=new ();
    myobj=system.reflection.missing.value;
    mydoc=word.document.add();
    word.visible=isshowword;
    mydoc.select();
    mysel=word.selection;
    mytable=mydoc.tables.add();
    mytable.columns.setwidth();来2个for 把mytable的数据输入到word里,代码略
    }
      

  4.   

    public class BiultReportForm   
       {   
           ///    
           /// word 应用对象   
           ///    
           private Microsoft.Office.Interop.Word.Application _wordApplication;   
      
           ///    
           /// word 文件对象   
           ///    
           private Microsoft.Office.Interop.Word.Document _wordDocument;    
           ///    
           /// 创建文档   
           ///    
           public void CreateAWord()   
           {   
               //实例化word应用对象   
               this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();   
               Object myNothing = System.Reflection.Missing.Value;   
      
               this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);   
           }   
           ///    
           /// 添加页眉   
           ///    
           ///    
           public void SetPageHeader(string pPageHeader)   
           {   
               //添加页眉   
               this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView;   
               this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;   
               this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);   
               //设置中间对齐   
               this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;   
               //跳出页眉设置   
               this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;   
           }   
           ///    
           /// 插入文字   
           ///    
           /// 文本信息   
           /// 字体打小   
           /// 字体颜色   
           /// 字体粗体   
           /// 方向   
           public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment)   
           {   
               //设置字体样式以及方向   
               this._wordApplication.Application.Selection.Font.Size = pFontSize;   
               this._wordApplication.Application.Selection.Font.Bold = pFontBold;   
               this._wordApplication.Application.Selection.Font.Color= pFontColor;   
               this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;   
               this._wordApplication.Application.Selection.TypeText(pText);   
           }   
      
      
           ///    
           /// 换行   
           ///    
           public void NewLine()   
           {   
               //换行   
               this._wordApplication.Application.Selection.TypeParagraph();   
           }
           ///    
           /// 插入一个图片   
           ///    
           ///    
           public void InsertPicture(string pPictureFileName)   
           {   
               object myNothing = System.Reflection.Missing.Value;   
               //图片居中显示   
               this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;   
               this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);   
           }
           ///    
           /// 保存文件    
           ///    
           /// 保存的文件名   
           public void SaveWord(string pFileName)   
           {   
               object myNothing = System.Reflection.Missing.Value;   
               object myFileName = pFileName;   
               object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument;   
               object myLockd = false;   
               object myPassword = "";   
               object myAddto = true;   
               try  
               {   
                   this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,   
                       ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,    
                       ref myNothing, ref myNothing, ref myNothing);   
               }   
               catch  
               {   
                   throw new Exception("导出word文档失败!");   
               }   
           }   
       } 
      

  5.   


    这个使用了组件的#region 导出当前页DataGridView中的数据到Word中        public void ExportDataGridViewToWord(DataGridView srcDgv, ProgressBar progreesBar, SaveFileDialog sfile)
            {
                if (srcDgv.Rows.Count == 0)
                {
                    MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    sfile.AddExtension = true;
                    sfile.DefaultExt = ".doc";
                    sfile.Filter = "(*.doc)|*.doc";
                    if (sfile.ShowDialog() == DialogResult.OK)
                    {
                        progreesBar.Visible = true;
                        object path = sfile.FileName;
                        Object none = System.Reflection.Missing.Value;
                        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
                        Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
                        //建立表格
                        Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count, srcDgv.Columns.Count, ref none, ref none);
                        try
                        {
                            for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
                            {
                                table.Cell(0, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;
                            }
                            for (int i = 1; i < srcDgv.Rows.Count; i++)//填充数据
                            {
                                for (int j = 0; j < srcDgv.Columns.Count; j++)
                                {
                                    table.Cell(i + 1, j + 1).Range.Text = srcDgv[j, i - 1].Value.ToString();
                                }
                                progreesBar.Value += 100 / srcDgv.RowCount;
                            }
                            document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
                            document.Close(ref none, ref none, ref none);                        progreesBar.Value = 100;
                            MessageBox.Show("数据已经成功导出到:" + sfile.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            progreesBar.Value = 0;
                            progreesBar.Visible = false;
                        }
                        catch(Exception e)
                        {
                            MessageBox.Show(e.Message, "友情提示", MessageBoxButtons.OK);
                        }
                        finally
                        {
                            wordApp.Quit(ref none, ref none, ref none);
                        }
                    }
                }
                
            } 
            #endregion