当前位置: 代码迷 >> C# >> C#如何根据Word模板生成一份Word文档
  详细解决方案

C#如何根据Word模板生成一份Word文档

热度:388   发布时间:2016-05-05 02:52:54.0
C#怎么根据Word模板生成一份Word文档
之前用excel vba做出来了一个根据dot模板文件生成Word的小程序。只要在模板中设置好书签,再用代码将需要的内容复制粘贴到对应的书签上就好了。
现在想要C#实现这个功能该怎么做呢?
网上搜的示例代码都好长好多,我刚接触C#不怎么明白。
有没有大神能提供一下最简单基本的示例代码呢~~~
------解决思路----------------------
http://www.cnblogs.com/kingteach/archive/2011/11/22/2258801.html
http://blog.csdn.net/syz_yumeizhou_yy/article/details/7547164

参考
------解决思路----------------------


                StreamReader sr = new StreamReader("Template\\XXXX.xml");
                string content = sr.ReadToEnd();

                content = content.Replace("Textbox01", "替换的内容");

                StreamWriter sw = new StreamWriter("Template\\XXXX.doc");
                sw.Write(content);

                sr.Close();
                sw.Close();




------解决思路----------------------
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;//启用显示按钮
                        }));
                });
        }
//选择创建文档路径地址
private void txt_select_Click(object sender, EventArgs e)
        {
            G_FolderBrowserDialog = //创建浏览文件夹对象
                new FolderBrowserDialog();
            DialogResult P_DialogResult =//浏览文件夹
                G_FolderBrowserDialog.ShowDialog();
            if (P_DialogResult==DialogResult.OK)//确认已经选择文件夹
            {
                btn_New.Enabled = true;//启用新建按钮
                txt_path.Text = //显示选择路径
                    G_FolderBrowserDialog.SelectedPath;
            }
        }
  相关解决方案