我现在有一个一页的dot模板,需要生成一份word文档,文档的页数和内容根据数据库记录填充,每页都需要应用这个dot模板,这个如何实现?

解决方案 »

  1.   

    Microsoft.Office.Interop.Word.dll
    找找关于它的资料看看.
      

  2.   

    Microsoft.Office.Interop.Word.dll 
    每次將模板讀取來new 一次,然后設定好后保存為其他你需要的文件名
    不過WebForm中New Word實例.全部都是在服務器端的內存中生成,多人使用的時候,服務器負荷不起哦
    考慮把模板存放在DB中,每次使用從DB讀取?
      

  3.   

    我是用Microsoft.Office.Interop.Word.dll可是一般都只是打开模板,进行编辑,保存为doc。一个dot对应一个doc,无法将一个dot反复应用到每一页。
      

  4.   

    复制模板到另一文件夹,再操作
    public string JBHJ(System.Web.UI.Page page,DataSet ds,object FileName,string JIE,string CI)
    {
    string strM="";
    object nothing=System.Reflection.Missing.Value;
    object optional=System.Reflection.Missing.Value;
    object visible=true;
    object saveChanges = true;
    object NOTsaveChanges = false;
    object docreadonly=true;
    object originalFormat = System.Reflection.Missing.Value;
    object routeDocument =System.Reflection.Missing.Value;
    Word.ApplicationClass app=new Word.ApplicationClass();
    object FileNa=page.Server.MapPath("../Template_temp/"+FileName);
    Word.Document Doc=app.Documents.Open(ref FileNa,ref optional,ref docreadonly,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional, ref visible);
    int index=1;
    try
    {
    Doc.Paragraphs.First.Range.Text="";
    Doc.Paragraphs.First.Range.Font.Size=18;
    Doc.Paragraphs.First.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;
    Doc.Paragraphs.First.Range.Font.Bold=5;
                        
    Doc.Paragraphs.Add(ref nothing);
    Doc.Paragraphs.Item(2).Range.Text="";
    Doc.Paragraphs.Item(2).Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;
    Doc.Paragraphs.Item(2).Range.Font.Size=17; Word.Paragraph ph=Doc.Paragraphs.Add(ref nothing);
    ph.Range.ParagraphFormat.FirstLineIndent-=1; 
    ph.Range.Font.Size=15;
    ph.Range.Font.Bold=2;
    ph.Alignment=Word.WdParagraphAlignment.wdAlignParagraphLeft;

    Word.Table tb=ph.Range.Tables.AddOld(ph.Range ,1,1);
    tb.Rows.Alignment=Word.WdRowAlignment.wdAlignRowCenter;

    tb.Range.Font.Size=15;
    tb.Range.Font.Bold=2;
    tb.Cell(1,1).Range.Text="";
    if(ds.Tables[0].Rows.Count >0)
    {
    for(int i=0;i<ds.Tables[0].Rows.Count ;i++)
    {
    Word.Row row=tb.Rows.Add(ref nothing);
    row.Alignment=Word.WdRowAlignment.wdAlignRowCenter;
    row.Range.Font.Size=13;
    row.Range.Font.Bold=0;
    index++;
    tb.Cell(index,1).Range.Text=ds.Tables[0].Rows[i]["MC"].ToString();
    }
    }
    object strFi=page.Server.MapPath(@"../Template_temp/A.doc");
    Doc.SaveAs(ref strFi,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional,ref optional, ref optional, ref optional, ref optional);
    Doc.Close(ref  nothing,ref nothing,ref nothing);
    app.Quit(ref NOTsaveChanges, ref originalFormat, ref routeDocument);

    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc);
    app=null;
    Doc=null;
    GC.Collect();
    GC.Collect();

    }
    catch(Exception err)
    {
    strM=err.Message+",导出失败!";
    return strM;
    }
    return "";
    }
      

  5.   

    感谢wuyq11。和我的想法一样,循环复制过去之后插入一个回车。不过有一个问题,我的模板里面有书签,为了在某些地方去加入数据内容,单纯复制过去书签是不过去的(就算过去书签名也是个问题呵呵),后来我只好每个循环内打开模板,插入书签内容,复制,关闭模板,再打开模板,插入书签...不过效率有点低。不知有无更好的办法?
      

  6.   

    see see 。maybe has useful