• C#word(2007)操作类新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化


     
    -0
    这两天整理了一下使用C#对word的操作,这里和大家分享:
    1、新建Word文档
    0
     
     
    #region 新建Word文档
    0
    /// 
    0
    /// 动态生成Word文档并填充内容 
    0
    /// 
    0
    /// 文档目录
    0
    /// 文档名
    0
    /// 返回自定义信息
    0
    public static bool CreateWordFile(string dir, string fileName)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
     
    0
            if (!Directory.Exists(dir))
    0
            {
    0
                //创建文件所在目录
    0
                Directory.CreateDirectory(dir);
    0
            }
    0
            //创建Word文档(Microsoft.Office.Interop.Word)
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Add(
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            //保存
    0
            object filename = dir + fileName;
    0
            WordDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion 新建Word文档
    2、给word文档添加页眉页脚
    0
     
    0
    #region 给word文档添加页眉页脚
    0
    /// 
    0
    /// 给word文档添加页眉
    0
    /// 
    0
    /// 文件名
    0
    /// 
    0
    public static bool AddPageHeaderFooter(string filePath)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            object filename = filePath;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            ////添加页眉方法一:
    0
            //WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
    0
            //WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
    0
            //WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**公司" );//页眉内容
    0
     
    0
            ////添加页眉方法二:
    0
            if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView ||
    0
                WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)
    0
            {
    0
                WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
    0
            }
    0
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
    0
            WordApp.Selection.HeaderFooter.LinkToPrevious = false;
    0
            WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
    0
            WordApp.Selection.HeaderFooter.Range.Text = "页眉内容";
    0
     
    0
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;
    0
            WordApp.Selection.HeaderFooter.LinkToPrevious = false;
    0
            WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
    0
            WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("页脚内容");
    0
     
    0
            //跳出页眉页脚设置
    0
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
    0
     
    0
            //保存
    0
            WordDoc.Save();
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion 给word文档添加页眉页脚
    3、设置文档格式并添加文本内容、超链接
    0
     
    0
    #region 设置文档格式并添加文本内容、超链接
    0
    /// 
    0
    /// 设置文档格式并添加内容
    0
    /// 
    0
    /// 文件名
    0
    /// 
    0
    public static bool AddContent(string filePath)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            object filename = filePath;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            //设置居左
    0
            WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
    0
     
    0
            //设置文档的行间距
    0
            WordApp.Selection.ParagraphFormat.LineSpacing = 15f;
    0
            //插入段落
    0
            //WordApp.Selection.TypeParagraph();
    0
            Microsoft.Office.Interop.Word.Paragraph para;
    0
            para = WordDoc.Content.Paragraphs.Add(ref oMissing);
    0
            //正常格式
    0
            para.Range.Text = "This is paragraph 1";
    0
            //para.Range.Font.Bold = 2;
    0
            //para.Range.Font.Color = WdColor.wdColorRed;
    0
            //para.Range.Font.Italic = 2;
    0
            para.Range.InsertParagraphAfter();
    0
     
    0
            para.Range.Text = "This is paragraph 2";
    0
            para.Range.InsertParagraphAfter();
    0
     
    0
            //插入Hyperlink
    0
            Microsoft.Office.Interop.Word.Selection mySelection = WordApp.ActiveWindow.Selection;
    0
            mySelection.Start = 9999;
    0
            mySelection.End = 9999;
    0
            Microsoft.Office.Interop.Word.Range myRange = mySelection.Range;
    0
     
    0
            Microsoft.Office.Interop.Word.Hyperlinks myLinks = WordDoc.Hyperlinks;
    0
            object linkAddr = @"http://www.cnblogs.com/lantionzy";
    0
            Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,
    0
                ref oMissing);
    0
            WordApp.ActiveWindow.Selection.InsertAfter("\n");
    0
     
    0
            //落款
    0
            WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();
    0
            WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
    0
     
    0
            //保存
    0
            WordDoc.Save();
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion 设置文档格式并添加文本内容、超链接
    4、添加图片
    0
     
    0
    #region 文档中添加图片
    0
    /// 
    0
    /// 文档中添加图片
    0
    /// 
    0
    /// word文件名
    0
    /// picture文件名
    0
    /// 
    0
    public static bool AddPicture(string filePath, string picPath)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            object filename = filePath;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            //移动光标文档末尾
    0
            object count = WordDoc.Paragraphs.Count;
    0
            object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdParagraph;
    0
            WordApp.Selection.MoveDown(ref WdLine, ref count, ref oMissing);//移动焦点
    0
            WordApp.Selection.TypeParagraph();//插入段落
    0
     
    0
            object LinkToFile = false;
    0
            object SaveWithDocument = true;
    0
            object Anchor = WordDoc.Application.Selection.Range;
    0
            WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(picPath, ref LinkToFile, ref SaveWithDocument, ref Anchor);
    0
     
    0
            //保存
    0
            WordDoc.Save();
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion 文档中添加图片
    5、表格处理(插入表格、设置格式、填充内容、表格中加图片)
    0
     
    0
    #region 表格处理(插入表格、设置格式、填充内容)
    0
    /// 
    0
    /// 表格处理
    0
    /// 
    0
    /// word文件名
    0
    /// 
    0
    public static bool AddTable(string filePath)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            object filename = filePath;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            //插入表格
    0
            Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref oMissing, ref oMissing);
    0
            //设置表格
    0
            newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
    0
            newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
    0
            newTable.Columns[1].Width = 100f;
    0
            newTable.Columns[2].Width = 220f;
    0
            newTable.Columns[3].Width = 105f;
    0
     
    0
            //填充表格内容
    0
            newTable.Cell(1, 1).Range.Text = "我的简历";
    0
            //设置单元格中字体为粗体
    0
            newTable.Cell(1, 1).Range.Bold = 2;
    0
     
    0
            //合并单元格
    0
            newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
    0
     
    0
            //垂直居中
    0
            WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
    0
            //水平居中
    0
            WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
    0
     
    0
            //填充表格内容
    0
            newTable.Cell(2, 1).Range.Text = "座右铭:
    0
    ";
    0
            //设置单元格内字体颜色
    0
            newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
    0
            //合并单元格
    0
            newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
    0
            WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
    0
     
    0
            //填充表格内容
    0
            newTable.Cell(3, 1).Range.Text = "姓名:";
    0
            newTable.Cell(3, 2).Range.Text = "雷鑫";
    0
            //纵向合并单元格
    0
            newTable.Cell(3, 3).Select();
    0
            //选中一行
    0
            object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
    0
            object moveCount = 3;
    0
            object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
    0
            WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
    0
            WordApp.Selection.Cells.Merge();
    0
     
    0
            //表格中插入图片
    0
            string pictureFileName = System.IO.Directory.GetCurrentDirectory() + @"\picture.jpg";
    0
            object LinkToFile = false;
    0
            object SaveWithDocument = true;
    0
            object Anchor = WordDoc.Application.Selection.Range;
    0
            WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureFileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
    0
            //图片宽度
    0
            WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;
    0
            //图片高度
    0
            WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;
    0
            //将图片设置为四周环绕型
    0
            Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
    0
            s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
    0
     
    0
            newTable.Cell(12, 1).Range.Text = "备注:";
    0
            newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
    0
            //在表格中增加行
    0
            WordDoc.Content.Tables[1].Rows.Add(ref oMissing);
    0
     
    0
            //保存
    0
            WordDoc.Save();
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion #region 表格处理
    6、把Word文档转化为Html文件
    0
     
    0
    #region 把Word文档转化为Html文件
    0
    /// 
    0
    /// 把Word文档转化为Html文件
    0
    /// 
    0
    /// word文件名
    0
    /// 要保存的html文件名
    0
    /// 
    0
    public static bool WordToHtml(string wordFileName, string htmlFileName)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            object filename = wordFileName;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            // Type wordType = WordApp.GetType();
    0
            // 打开文件
    0
            Type docsType = WordApp.Documents.GetType();
    0
            // 转换格式,另存为
    0
            Type docType = WordDoc.GetType();
    0
            object saveFileName = htmlFileName;
    0
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, WordDoc,
    0
                new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML });
    0
     
    0
            其它格式:
    0
     
    0
            //保存
    0
            WordDoc.Save();
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion 把Word文档转化为Html文件
     
    可以从这里下载整个类(包含更多内容)
     
    分类: C#
    标签: C#Word2007操作类
    绿色通道: 好文要顶 关注我 收藏该文与我联系 
    0
    0
    +加关注
    6
    0
    (请您对文章做出评价)
    posted on 2009-10-23 10:09 lantionzy 阅读(5099) 评论(9) 编辑 收藏
     
    评论:
    #1楼 2009-10-23 21:38 | 通用C#系统架构  
    兄弟,这个要放首页,你太那个了,哈哈。
    支持(0)反对(0)
      
    #2楼 2009-11-04 23:30 | 若泥[未注册用户]
    Word to Pdf
      
    #3楼 2010-01-20 11:59 | zwei  
    Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,
    ref oMissing);
    这个地方编译不过去。
    支持(0)反对(0)
      
    #4楼 2010-09-04 11:58 | 冷月孤峰  
    楼主,添加页眉页脚的那个方法,对2003格式的可以,但是如果给2010文档添加了页眉后就不能打开文档了,不晓得你那里是否有这个问题
    支持(0)反对(0)
      
    #5楼[楼主] 2010-09-05 17:59 | lantionzy  
    @Michael
    没在2010中试过,也不知道是什么问题
    支持(0)反对(0)
      
    #6楼 2010-09-07 09:20 | 冷月孤峰  
    @lantionzy
    楼主你好,不好意思,是我自己的错,我是从数据库中将文件写入本地后再进行操作的,是我自己代码问题,呵呵
    支持(0)反对(0)
      
    #7楼 2013-02-28 16:17 | kpb  
    楼主 页眉和页脚的格式怎么设置呀??
    支持(0)反对(0)
      
    #8楼 2014-01-09 15:11 | 我和小菜  
    0
     
     
    第一个参数和第二个参数都不是很明白,希望lz可以告诉我一下,可以吗?
    支持(0)反对(0)
      
    #9楼 2015-01-28 14:22 | Jerk_du  
    Word中插入,提取,替换图片
    Word文档转换成PDF,XML等格式
    支持(0)反对(0)
      
    注册用户登录后才能发表评论,请 登录 或 注册,访问网站首页。
    最新IT新闻:
    最新知识库文章:
     
    欢迎来到我的博客
    0
     
    0
     
     
    0
     
    0
     
    0
     
    0
     
    0
     
    0
     
    0
     
    0
     
     
    昵称:lantionzy
    园龄:5年4个月
    粉丝:25
    关注:14
    +加关注
     
    27
    28
    29
    30
    1
    2
    3
    4
    5
    6
    7
    8
    11
    17
    18
    22
    24
    25
    26
    31
    1
    2
    3
    4
    5
    6
    7
    <
    2009年10月
    >
     
    随笔分类(66)
    随笔档案(55)
    友情链接
    常用链接
    积分与排名
    • 积分 - 18984
    • 排名 - 9345
    最新评论
    阅读排行榜
    评论排行榜
    Powered by: 博客园 模板提供:沪江博客 Copyright ©2015 lantionzy
    人生就像卫生纸,没事少扯。 尽心做人,尽力做事。
    博客园   首页   博问   闪存   新随笔   联系   订阅
    0
      管理
    随笔-55  评论-56  文章-0  trackbacks-0
    这两天整理了一下使用C#对word的操作,这里和大家分享:
    1、新建Word文档
    0
     
    0
    #region 新建Word文档
    0
    /// 
    0
    /// 动态生成Word文档并填充内容 
    0
    /// 
    0
    /// 文档目录
    0
    /// 文档名
    0
    /// 返回自定义信息
    0
    public static bool CreateWordFile(string dir, string fileName)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
     
    0
            if (!Directory.Exists(dir))
    0
            {
    0
                //创建文件所在目录
    0
                Directory.CreateDirectory(dir);
    0
            }
    0
            //创建Word文档(Microsoft.Office.Interop.Word)
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Add(
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            //保存
    0
            object filename = dir + fileName;
    0
            WordDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion 新建Word文档
    2、给word文档添加页眉页脚
    0
     
    0
    #region 给word文档添加页眉页脚
    0
    /// 
    0
    /// 给word文档添加页眉
    0
    /// 
    0
    /// 文件名
    0
    /// 
    0
    public static bool AddPageHeaderFooter(string filePath)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            object filename = filePath;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            ////添加页眉方法一:
    0
            //WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
    0
            //WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
    0
            //WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**公司" );//页眉内容
    0
     
    0
            ////添加页眉方法二:
    0
            if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView ||
    0
                WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)
    0
            {
    0
                WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
    0
            }
    0
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
    0
            WordApp.Selection.HeaderFooter.LinkToPrevious = false;
    0
            WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
    0
            WordApp.Selection.HeaderFooter.Range.Text = "页眉内容";
    0
     
    0
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;
    0
            WordApp.Selection.HeaderFooter.LinkToPrevious = false;
    0
            WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
    0
            WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("页脚内容");
    0
     
    0
            //跳出页眉页脚设置
    0
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
    0
     
    0
            //保存
    0
            WordDoc.Save();
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion 给word文档添加页眉页脚
    3、设置文档格式并添加文本内容、超链接
    0
     
    0
    #region 设置文档格式并添加文本内容、超链接
    0
    /// 
    0
    /// 设置文档格式并添加内容
    0
    /// 
    0
    /// 文件名
    0
    /// 
    0
    public static bool AddContent(string filePath)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            object filename = filePath;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            //设置居左
    0
            WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
    0
     
    0
            //设置文档的行间距
    0
            WordApp.Selection.ParagraphFormat.LineSpacing = 15f;
    0
            //插入段落
    0
            //WordApp.Selection.TypeParagraph();
    0
            Microsoft.Office.Interop.Word.Paragraph para;
    0
            para = WordDoc.Content.Paragraphs.Add(ref oMissing);
    0
            //正常格式
    0
            para.Range.Text = "This is paragraph 1";
    0
            //para.Range.Font.Bold = 2;
    0
            //para.Range.Font.Color = WdColor.wdColorRed;
    0
            //para.Range.Font.Italic = 2;
    0
            para.Range.InsertParagraphAfter();
    0
     
    0
            para.Range.Text = "This is paragraph 2";
    0
            para.Range.InsertParagraphAfter();
    0
     
    0
            //插入Hyperlink
    0
            Microsoft.Office.Interop.Word.Selection mySelection = WordApp.ActiveWindow.Selection;
    0
            mySelection.Start = 9999;
    0
            mySelection.End = 9999;
    0
            Microsoft.Office.Interop.Word.Range myRange = mySelection.Range;
    0
     
    0
            Microsoft.Office.Interop.Word.Hyperlinks myLinks = WordDoc.Hyperlinks;
    0
            object linkAddr = @"http://www.cnblogs.com/lantionzy";
    0
            Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,
    0
                ref oMissing);
    0
            WordApp.ActiveWindow.Selection.InsertAfter("\n");
    0
     
    0
            //落款
    0
            WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();
    0
            WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
    0
     
    0
            //保存
    0
            WordDoc.Save();
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion 设置文档格式并添加文本内容、超链接
    4、添加图片
    0
     
    0
    #region 文档中添加图片
    0
    /// 
    0
    /// 文档中添加图片
    0
    /// 
    0
    /// word文件名
    0
    /// picture文件名
    0
    /// 
    0
    public static bool AddPicture(string filePath, string picPath)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            object filename = filePath;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            //移动光标文档末尾
    0
            object count = WordDoc.Paragraphs.Count;
    0
            object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdParagraph;
    0
            WordApp.Selection.MoveDown(ref WdLine, ref count, ref oMissing);//移动焦点
    0
            WordApp.Selection.TypeParagraph();//插入段落
    0
     
    0
            object LinkToFile = false;
    0
            object SaveWithDocument = true;
    0
            object Anchor = WordDoc.Application.Selection.Range;
    0
            WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(picPath, ref LinkToFile, ref SaveWithDocument, ref Anchor);
    0
     
    0
            //保存
    0
            WordDoc.Save();
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion 文档中添加图片
    5、表格处理(插入表格、设置格式、填充内容、表格中加图片)
    0
     
    0
    #region 表格处理(插入表格、设置格式、填充内容)
    0
    /// 
    0
    /// 表格处理
    0
    /// 
    0
    /// word文件名
    0
    /// 
    0
    public static bool AddTable(string filePath)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            object filename = filePath;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            //插入表格
    0
            Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref oMissing, ref oMissing);
    0
            //设置表格
    0
            newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
    0
            newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
    0
            newTable.Columns[1].Width = 100f;
    0
            newTable.Columns[2].Width = 220f;
    0
            newTable.Columns[3].Width = 105f;
    0
     
    0
            //填充表格内容
    0
            newTable.Cell(1, 1).Range.Text = "我的简历";
    0
            //设置单元格中字体为粗体
    0
            newTable.Cell(1, 1).Range.Bold = 2;
    0
     
    0
            //合并单元格
    0
            newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
    0
     
    0
            //垂直居中
    0
            WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
    0
            //水平居中
    0
            WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
    0
     
    0
            //填充表格内容
    0
            newTable.Cell(2, 1).Range.Text = "座右铭:
    0
    ";
    0
            //设置单元格内字体颜色
    0
            newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
    0
            //合并单元格
    0
            newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
    0
            WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
    0
     
    0
            //填充表格内容
    0
            newTable.Cell(3, 1).Range.Text = "姓名:";
    0
            newTable.Cell(3, 2).Range.Text = "雷鑫";
    0
            //纵向合并单元格
    0
            newTable.Cell(3, 3).Select();
    0
            //选中一行
    0
            object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
    0
            object moveCount = 3;
    0
            object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
    0
            WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
    0
            WordApp.Selection.Cells.Merge();
    0
     
    0
            //表格中插入图片
    0
            string pictureFileName = System.IO.Directory.GetCurrentDirectory() + @"\picture.jpg";
    0
            object LinkToFile = false;
    0
            object SaveWithDocument = true;
    0
            object Anchor = WordDoc.Application.Selection.Range;
    0
            WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureFileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
    0
            //图片宽度
    0
            WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;
    0
            //图片高度
    0
            WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;
    0
            //将图片设置为四周环绕型
    0
            Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
    0
            s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
    0
     
    0
            newTable.Cell(12, 1).Range.Text = "备注:";
    0
            newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
    0
            //在表格中增加行
    0
            WordDoc.Content.Tables[1].Rows.Add(ref oMissing);
    0
     
    0
            //保存
    0
            WordDoc.Save();
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion #region 表格处理
    6、把Word文档转化为Html文件
    0
     
    0
    #region 把Word文档转化为Html文件
    0
    /// 
    0
    /// 把Word文档转化为Html文件
    0
    /// 
    0
    /// word文件名
    0
    /// 要保存的html文件名
    0
    /// 
    0
    public static bool WordToHtml(string wordFileName, string htmlFileName)
    0
    {
    0
        try
    0
        {
    0
            Object oMissing = System.Reflection.Missing.Value;
    0
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
    0
            WordApp.Visible = true;
    0
            object filename = wordFileName;
    0
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    0
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    0
     
    0
            // Type wordType = WordApp.GetType();
    0
            // 打开文件
    0
            Type docsType = WordApp.Documents.GetType();
    0
            // 转换格式,另存为
    0
            Type docType = WordDoc.GetType();
    0
            object saveFileName = htmlFileName;
    0
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, WordDoc,
    0
                new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML });
    0
     
    0
            其它格式:
    0
     
    0
            //保存
    0
            WordDoc.Save();
    0
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
    0
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
    0
            return true;
    0
        }
    0
        catch (Exception e)
    0
        {
    0
            Console.WriteLine(e.Message);
    0
            Console.WriteLine(e.StackTrace);
    0
            return false;
    0
        }
    0
    }
    0
    #endregion 把Word文档转化为Html文件
     
     
     
    6
    0
    (请您对文章做出评价)
     
     
     
  • 相关阅读:
    luogu 1593
    luogu 1369
    hdu 1796
    bzoj 3398
    luogu 4587
    luogu 2152
    bzoj 3629
    bzoj 1507: [NOI2003]Editor
    bzoj 1503: [NOI2004]郁闷的出纳员
    bzoj 1497: [NOI2006]最大获利
  • 原文地址:https://www.cnblogs.com/devgis/p/16524125.html
Copyright © 2020-2023  润新知