菜鸟提问:我有一个word模板,希望通过程序来填充数据,保存成wword格式,如何实现?
最好有实例,谢谢~!

解决方案 »

  1.   


    Application wordApp = new ApplicationClass();
    Document o = this.OpenDoc(‘路径’, ref wordApp, 1);
    foreach (Book book in o.get_Books())
     {
         
         book .Select();
         book .get_Range().set_Text('文字'); }
    private Document OpenDoc(string strDocPath, ref Application WordApp, int flag)
    {
        Document document2;
        if (!File.Exists(strDocPath))
        {
            return null;
        }
        object obj2 = strDocPath;
        object missing = this.missing;
        object obj4 = this.missing;
        WordApp.set_Visible(false);
        try
        {
            document2 = WordApp.get_Documents().Open(ref obj2, ref this.missing, ref obj4, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing);
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
        return document2;
    }
      

  2.   

    private void TestPrint()
        {//利用word模板生成
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
            //模板文件
            string TemplateFile = @"E:\work\testPrint.doc";//@"E:\work\testPrint.doc";
            TemplateFile = Server.MapPath("~/UpLoad/TestWord/testPrint.doc");
            //生成的具有模板样式的新文件
            string FileName = @"C:\Documents and Settings\Administrator\桌面\" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc";//@"C:\Documents and Settings\Administrator\桌面\" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc";
            string Fname = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc";
            FileName = Server.MapPath("~/UpLoad/TestWord/" + Fname);
            //模板文件拷贝到新文件
            File.Copy(TemplateFile, FileName);
            Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
            object Obj_FileName = FileName;
            object Visible = false;
            object ReadOnly = false;
            object missing = System.Reflection.Missing.Value;
            //打开文件
            doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
             ref missing, ref missing, ref missing, ref missing,
             ref missing, ref missing, ref missing, ref Visible,
             ref missing, ref missing, ref missing,
             ref missing);
            doc.Activate();        #region MyRegion
            //foreach (Microsoft.Office.Interop.Word.Book bm in doc.Books)
            //{
            //    if (bm.Name == "a")
            //    {
            //        bm.Select();
            //        bm.Range.Text = this.txtTitle.Value;
            //    }
            //    //else if (bm.Name == "class")
            //    //{
            //    //    bm.Select();
            //    //    bm.Range.Text = ViewState["_class"].ToString();
            //    //}
            //    //else if (bm.Name == "caizhi")
            //    //{
            //    //    bm.Select();
            //    //    bm.Range.Text = ViewState["_caizhi"].ToString();
            //    //}
            //    //else if (bm.Name == "guige")
            //    //{
            //    //    bm.Select();
            //    //    bm.Range.Text = ViewState["_guige"].ToString();
            //    //}
            //    //else if (bm.Name == "chicun")
            //    //{
            //    //    bm.Select();
            //    //    bm.Range.Text = txtSize.Text;
            //    //}
            //    //else if (bm.Name == "count")
            //    //{
            //    //    bm.Select();
            //    //    bm.Range.Text = txtCount.Text;
            //    //}
            //    //else if (bm.Name == "sweight")
            //    //{
            //    //    bm.Select();
            //    //    bm.Range.Text = txtSingleWeight.Text;
            //    //}
            //    //else if (bm.Name == "weight")
            //    //{
            //    //    bm.Select();
            //    //    bm.Range.Text = txtWeight.Text;
            //    //}        //} 
            #endregion
            object IsSave = true;
            doc.Close(ref IsSave, ref missing, ref missing);        //hplTestWord.Visible = true;
            //hplTestWord.Target = "_blank";
            //hplTestWord.NavigateUrl = "~/UpLoad/TestWord/" + Fname;        Response.Write("<script language='javascript'>alert('生成word成功!');</script>");
        }
      

  3.   

    Object Nothing=System.Reflection.Missing.value;
        //取得Word文件保存路径
        object [email protected];
        //创建一个名为WordApp的组件对象
        Word.Application WordApp=new Word.ApplicationClass();
        //创建一个名为WordDoc的文档对象
        Word.Document WordDoc=WordApp.Documents.Add(ref Nothing,ref Nothing,
                                                               ref Nothing,ref Nothing);
        //增加一表格
        Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,1,1,ref Nothing,ref Nothing);
        //在表格第一单元格中添加自定义的文字内容
        table.Cell(1,1).Range.Text=wordText.Text;
        //在文档空白地方添加文字内容
        WordDoc.Paragraphs.Last.Range.Text="Wellcome To Aspxcn.Com";
        //将WordDoc文档对象的内容保存为DOC文档
        WordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,
                    ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,
                    ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
        //关闭WordDoc文档对象
        WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
        //关闭WordApp组件对象
        WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
        //返回结果
        result.Text="文档路径:<a href="/"+SaveAs.Text+"'>"+SaveAs.Text
                                            +"</a>(点击链接查看)<br/>生成结果:成功!";