当前位置:编程学习 > C#/ASP.NET >>

急求高手解答,100分的问题

最近在做的一个项目要用c#操作word .dot文件生成word文档,我的做法也是通常的是做法就是在.dot文件定义标签,然后根据标签来填充内容,但是这个有问题,比如我只能在固定的位置插入标题,然后插入内容,然后插入下一个标题,但是内容有多少行是不固定的,在.dot中只能定死光标的位置啊,如果内容多了,可能会把下一个标题的位置覆盖,如果少了,又会留下空白,所以很不好把握,老板(导师)要求必须这么做。而且插入的图片的位置不是我事先定义的书签的位置啊,求大侠帮忙,QQ:851959715,代码如下:
 #region 填充书签
        /// <summary>   
        /// 填充书签   
        /// </summary>   
        public void ReplaceBookMark(string bookmark, string value)
        {
            try
            {
                object bkObj = bookmark;
                if (wApp.ActiveDocument.Bookmarks.Exists(bookmark) == true)
                {
                    wApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
                }
                else return;
                wApp.Selection.Font.Name = "华文新魏";   //设置字体   
                wApp.Selection.Font.Size = 20f;
                wApp.Selection.Font.Color = Word.WdColor.wdColorRed;
                wApp.Selection.Font.UnderlineColor = Word.WdColor.wdColorRed;
                //wApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineSingle; 
                wApp.Selection.TypeText(value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region 填充表格
        /// <summary>   
        /// 填充表格  
        /// </summary>   
        public bool FindBookMark(string bookmarkTable)
        {
            try
            {
                object bkObj = bookmarkTable;
                if (wApp.ActiveDocument.Bookmarks.Exists(bookmarkTable) == true)
                {
                    wApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {               throw ex;
            }
        }
        public bool InsertPicture(string bookmark)
        {
            try
            {
                object bkObj = bookmark;
                if (wApp.ActiveDocument.Bookmarks.Exists(bookmark) == true)
                {
                    wApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
                    object oRng = wDoc.Bookmarks.get_Item(ref bkObj).Range;
                    string FileName = @"c:\program_process.jpg";//图片所在路径
                    object LinkToFile = false;
                    object SaveWithDocument = true;
                    wDoc.Words.Last.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref oRng);
                    wDoc.Application.ActiveDocument.InlineShapes[1].Width = 150f;//图片宽度
                    wDoc.Application.ActiveDocument.InlineShapes[1].Height = 150f;//图片高度
                    //设置图片版式   
                    Word.Shape s = wDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                    s.WrapFormat.Type = Word.WdWrapType.wdWrapInline;
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MoveNextCell()
        {
            try
            {
                Object unit = Word.WdUnits.wdCell;
                Object count = 1;
                wApp.Selection.Move(ref unit, ref count);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void SetCellValue(string value)
        {
            try
            {
                wApp.Selection.TypeText(value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void MoveNextRow()
        {
            try
            {
                Object extend = Word.WdMovementType.wdExtend;
                Object unit = Word.WdUnits.wdCell;
                Object count = 1;
                wApp.Selection.MoveRight(ref unit, ref count, ref extend);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        public void MainCreateWord()
        {
            try
            {
                this.CreateNewWordDocument("D:\\五.dot");
                this.ReplaceBookMark("标题一", "范围");
                this.ReplaceBookMark("标题一点一", "标识");
                this.ReplaceBookMark("标题一点二", "系统概述");
                this.ReplaceBookMark("一点二的内容", "系统的概述");
                this.ReplaceBookMark("标题一点三", "文档概述");
                this.ReplaceBookMark("标题一点三的内容", "文档的概述");
                this.ReplaceBookMark("标题二", "引用文档");
                this.ReplaceBookMark("标题二的内容", "引用的文档");
                this.ReplaceBookMark("标题三", "术语应用");
                this.ReplaceBookMark("三的内容", "术语的应用");
                this.ReplaceBookMark("标题四", "程序系统组织结构");
                this.ReplaceBookMark("标题四点一", "程序运行过程");
                //this.ReplaceBookMark("四点一的内容", "程序的运行过程");
                if(this.FindBookMark("图片一"))
                {
                    InsertPicture("图片一");
                }
                this.ReplaceBookMark("标题四点二", "程序逻辑");
                this.ReplaceBookMark("标题五", "组织导航软件详细设计(中断和主函数内部每一个函数都需要“函数功能描述”)");
                this.ReplaceBookMark("标题五点一", "中断描述");
                this.ReplaceBookMark("五点一的内容", "中断的描述");
                this.ReplaceBookMark("标题五点二", "主程序");
                this.ReplaceBookMark("五点二的内容", "主的程序");
                this.ReplaceBookMark("标题六", "需求可追踪性");
                if (this.FindBookMark("表格"))
                {
                    // 第1行数据
                    this.MoveNextRow();
                    this.SetCellValue("1");
                    this.MoveNextCell();
                    this.SetCellValue("HP电脑");
                    this.MoveNextCell();
                    this.SetCellValue("台");
                    this.MoveNextCell();
                    this.SetCellValue("50");
                    this.MoveNextCell();
                    this.SetCellValue("250,000");
                    // 第2行数据
                    this.MoveNextRow();
                    this.SetCellValue("2");
                    this.MoveNextCell();
                    this.SetCellValue("DELL笔记本");
                    this.MoveNextCell();
                    this.SetCellValue("台");
                    this.MoveNextCell();
                    this.SetCellValue("10");
                    this.MoveNextCell();
                    this.SetCellValue("40,000");
                    // 第3行数据
                    this.MoveNextRow();
                    this.SetCellValue("3");
                    this.MoveNextCell();
                    this.SetCellValue("联想喷墨打印机");
                    this.MoveNextCell();
                    this.SetCellValue("台");
                    this.MoveNextCell();
                    this.SetCellValue("100");
                    this.MoveNextCell();
                    this.SetCellValue("80,000");
                    // 第4行数据
                    this.MoveNextRow();
                    this.SetCellValue("4");
                    this.MoveNextCell();
                    this.SetCellValue("中国移动打印机");
                    this.MoveNextCell();
                    this.SetCellValue("台");
                    this.MoveNextCell();
                    this.SetCellValue("100");
                    this.MoveNextCell();
                    this.SetCellValue("80,000");
                }
                this.SaveAs("d:\\1.doc");
                this.CloseDocument();
            }
            catch (Exception ex)
            {
                return;
            }
        }
      

    }
    }
}  



--------------------编程问答-------------------- --------------------编程问答-------------------- 如果内容多了,可能会把下一个标题的位置覆盖,
问一下,是那个标题下出的问题???
是不是你计算添加的位置有问题? --------------------编程问答-------------------- http://www.codeproject.com/KB/office/Generating_Word_Reports.aspx
--------------------编程问答-------------------- --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,