表格部份用MailMerge.ExecuteWithRegions
另外两个框框用DocumentBuilder:
builder.MoveToBook("note");
builder.InsertHtml(note);

解决方案 »

  1.   

    C#创建Word文档
     private Word.Application G_wa;//定义Word应用程序字段
            private object G_missing = //定义G_missing字段并添加引用
                System.Reflection.Missing.Value;
            private FolderBrowserDialog G_FolderBrowserDialog;//定义浏览文件夹字段
            private object G_str_path;//定义文件保存路径字段        private void btn_New_Click(object sender, EventArgs e)
            {
                btn_New.Enabled = false;//将新建按钮设置为不可用
                ThreadPool.QueueUserWorkItem(//开始线程池
                    (pp) =>//使用lambda表达式
                    {
                        G_wa = new Microsoft.Office.Interop.Word.Application();//创建应用程序对象
                        object P_obj = "Normal.dot";//定义文档模板
                        Word.Document P_wd = G_wa.Documents.Add(//向Word应用程序中添加文档
                            ref P_obj, ref G_missing, ref G_missing, ref G_missing);
                        G_str_path = string.Format(//计算文件保存路径
                            @"{0}\{1}", G_FolderBrowserDialog.SelectedPath,
                            DateTime.Now.ToString("yyyy年M月d日h时s分m秒fff毫秒") + ".doc");
                        P_wd.SaveAs(//保存Word文件
                            ref G_str_path,
                            ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                            ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                            ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                            ref G_missing, ref G_missing, ref G_missing);
                        ((Word._Application)G_wa.Application).Quit(//退出应用程序
                            ref G_missing, ref G_missing, ref G_missing);
                        this.Invoke(//调用窗体线程
                            (MethodInvoker)(() =>//使用lambda表达式
                            {
                                MessageBox.Show(//提示已经创建Word
                                    "成功创建Word文档!", "提示!");
                                btn_display.Enabled = true;//启用显示按钮
                            }));
                    });
            }
    //在word文档中绘制表格
     private void btn_New_Click(object sender, EventArgs e)
            {
                G_ToolProgressBar.Minimum = 1;//设置进度条最小值
                G_ToolProgressBar.Maximum = //设置进度条最大值
                    int.Parse(txt_row.Text)+1;
                btn_New.Enabled = false;//停用新建按钮
                ThreadPool.QueueUserWorkItem(//使用线程池
                    (P_temp) =>//使用lambda表达式
                    {
                        G_wa = new Word.Application();//创建Word应用程序对象
                        Word.Document P_wd = G_wa.Documents.Add(//建立新文档
                            ref G_missing, ref G_missing, ref G_missing, ref G_missing);
                        Word.Range P_Range = P_wd.Paragraphs[1].Range;//得到文档范围对象
                        object P_DefaultTable = //创建表格参数对象
                            Word.WdDefaultTableBehavior.wdWord8TableBehavior;
                        object P_AutoFit = //创建表格参数对象
                            Word.WdAutoFitBehavior.wdAutoFitWindow;
                        Word.Table P_WordTable = P_Range.Tables.Add(//向文档中添加表格
                            P_Range,
                            int.Parse(txt_row.Text),
                            int.Parse(txt_column.Text),
                            ref P_DefaultTable, ref P_AutoFit);
                        for (int i = 1; i < int.Parse(txt_row.Text) + 1; i++)
                        {
                            for (int j = 1; j < int.Parse(txt_column.Text) + 1; j++)
                            {
                                P_WordTable.Cell(i, j).Range.Text =//使用双层循环向表格中添加数据
                                    string.Format("{0}行 {1}列", i.ToString(), j.ToString());
                                Thread.Sleep(10);//线程挂起10毫秒
                            }
                            this.Invoke(//调用窗体线程
                                (MethodInvoker)(() => //使用Lambda表达式
                                {
                                    G_ToolProgressBar.Value = i + 1;//设置进度信息
                                }));
                        }
                        G_str_path = string.Format(//计算文件保存路径
                            @"{0}\{1}", G_FolderBrowserDialog.SelectedPath,
                            DateTime.Now.ToString("yyyy年M月d日h时s分m秒fff毫秒") + ".doc");
                        P_wd.SaveAs(//保存Word文件
                            ref G_str_path,
                            ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                            ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                            ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                            ref G_missing, ref G_missing, ref G_missing);
                        ((Word._Application)G_wa.Application).Quit(//退出应用程序
                            ref G_missing, ref G_missing, ref G_missing);
                        this.Invoke(//开始执行窗体线程
                            (MethodInvoker)(() =>//使用lambda表达式
                            {
                                btn_Display.Enabled = true;//启用显示按钮
                                MessageBox.Show("成功创建Word文档!", "提示!");
                            }));
                    });
            }
      

  2.   


    这个报错:无法删除范围。
    (4)光标移动
    A:标签:
    系统预定义标签:object oEndOfDoc = "//endofdoc"; /* /endofdoc is a predefined book 系统预定义的书签?*/
    自定义标签:
    B:利用标签获取位置
    Word.Range wrdRng = oDoc.Books.get_Item(ref oEndOfDoc).Range;
    插入段落、表格时都会用到这个位置:
    oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
    oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);public void GotoBookMark(string strBookMarkName)  
            {  
                // VB :  Selection.GoTo What:=wdGoToBook, Name:="nome"  
                object Book = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBook;  
                object NameBookMark = strBookMarkName;  
                oWord.Selection.GoTo(ref Book, ref missing, ref missing, ref NameBookMark);  
            }