在Web下,读取word模板,替换指定位置变量,最后生成word文档,怎么完成啊?用户可以修改模板,我们只要在替换指定位置变量(此变量可能是表格,图片,或文字)大狭快快支招啊

解决方案 »

  1.   

    1,取数据
    2,读取word模板文件
    3,依据模板,生成填入数据的word文档
    4,直接显示或者提示用户下载新生成的word文档。
    以上的办法实施过,均可性。但个人不赞成使用直接生成word文档的办法,通过第三放控件生成报表,再转成pdf是比较简单快捷的方式。
      

  2.   

        /// <summary>
        /// 添写WORD文档
        /// </summary>
        /// <param name="dotpath">模板地址</param>
        /// <param name="struseid">资料ID</param>
        public void datoword(string dotpath, string struseid)
        {
            string strsvepath;//储存目录
             object missing = System.Reflection.Missing.Value;
            if (App == null)
                App = new Word.ApplicationClass();
            App.Visible = false;        object oTemplate = dotpath;
            //试过真接输出到PDF打印机。效率更低
            App.ActivePrinter = "MS Publisher Color Printer";
            doc = App.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);
            doc.Activate();
            doc.ActiveWindow.View.ShowFieldCodes = true;
            Word.Books bms = doc.Books;        //建立D目录
            strsvepath =Server.MapPath(".").ToString() + "\\doc\\"+struseid+"\\";
            if (Directory.Exists(strsvepath))
            {
            }
            else
            {
                Directory.CreateDirectory(strsvepath);
            }
            strsvepath = Server.MapPath(".").ToString() + "\\pdf\\" + struseid + "\\";
            if (Directory.Exists(strsvepath))
            {
            }
            else
            {
                Directory.CreateDirectory(strsvepath);
            }        //添写内容
            inputdata(bms, struseid);
            oTemplate = Server.MapPath(".") + "\\doc\\"+struseid+ "\\" + struseid +".doc";        doc.SaveAs(ref oTemplate, ref missing, ref  missing, ref  missing, ref  missing, ref  missing, ref missing, ref missing, ref  missing, ref missing, ref  missing, ref  missing, ref  missing, ref  missing, ref missing, ref  missing);
            App.Quit(ref missing, ref missing, ref missing);
            
            
        }
      

  3.   

        /// <summary>
        /// 将数据添入指定WORD表区域,注意列数量保持和WORD一样不然出错。
        /// </summary>
        /// <param name="wordtab">标签位置表对像</param>
        /// <param name="dts">要添加的数据源</param>
        /// <param name="rowscont">原行数量</param>
        /// <param name="tablehei">行高</param>
        /// <param name="x">开始添加的行</param>
        /// <param name="y">开始添加的列</param>
        private void inputtable(Word.Table wordtab,DataTable dts,float tablehei,int rowscont, int x,int y)
        {
            //Word.Table newTable = bm.Range.Tables[1];
            //bm.Range.Tables[1].Rows.Height;
            //粘贴
            //bm.Range.Paste();
            //添加一行
            if (dts.Rows.Count > 0)
            {
                object ff = 1;
                //得出控制高度
                tablehei = tablehei * rowscont;
                //bm.Application.ActiveWindow.View.ShowFieldCodes = true;
                float rowhei = 0;            if (dts.Rows.Count > rowscont)
                {
                    ff = dts.Rows.Count - rowscont;
                    App.Selection.InsertRows(ref ff);
                    //获得总高度,以保持高度不变
                    tablehei = App.Selection.Cells.Height * rowscont;
                    rowhei = tablehei / dts.Rows.Count;
                }
                for (int b = 0; b < dts.Columns.Count; b++)
                {
                    for (int i = 0; i < dts.Rows.Count; i++)
                    {
                        if (rowhei != 0)
                        {
                            wordtab.Cell(i + x, b + y).Height = rowhei;                    }
                        wordtab.Cell(i + x, b + y).Range.Text = dts.Rows[i][b].ToString();
                    }
                }
            }    }
      

  4.   

    using WordApplication = Microsoft.Office.Interop.Word.Application;
    using Document = Microsoft.Office.Interop.Word.Document;
    using Book = Microsoft.Office.Interop.Word.Book;        public void WebPrint()
            {
                String modePath, tempPath;
                modePath = @"c:\model.doc";
                tempPath = @"c:\host.doc";
                WordApplication word = null;
                if (word == null)
                {
                    try
                    {
                        word = new WordApplication();
                    }
                    catch   
                    {                }                object oMissing = System.Reflection.Missing.Value;
                    object path1 = (object)modePath;
                    object path2 = (object)tempPath;
                    Document doc = null;
                    doc = word.Documents.OpenOld(ref path1, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                    foreach (Book bm in doc.Books)
                    {
                        switch (bm.Name)
                        {
                            case "host":
                                bm.Select();
                                bm.Range.Text="test";
                                break;
                        }
                    }
                    word.Visible = false;                doc.SaveAs(ref path2, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                    doc.PrintPreview();
                    doc.Close(ref oMissing, ref oMissing, ref oMissing);
                    word.Quit(ref oMissing, ref oMissing, ref oMissing);
                   
                    word = null;
                    
                }
      

  5.   

    添加TAble                foreach (Book bm in doc.Books)
                    {
                        switch (bm.Name)
                        {
                            case "host":
                                bm.Select();
                                bm.Range.Text = "test";
                                break;
                            case "Table":
                                bm.Select();
                                Table table = bm.Range.Tables.Add(word.Selection.Range, 3, 3, ref para, ref para);
                                table.Cell(1, 1).Range.Text = "header1";
                                table.Cell(1, 2).Range.Text = "header2";
                                table.Cell(1, 3).Range.Text = "header3";                            break;
                        }
                    }