C#操作Word总结(二)——设置文档格式并添加文本内容、超链接、添加图片

设置文档格式并添加内容

#region 设置文档格式并添加文本内容、超链接
/// <summary>
/// 设置文档格式并添加内容
/// </summary>
/// <param name="filePath">文件名</param>
/// <returns></returns>
public static bool AddContent(string filePath)
{
    try
    {
        Object oMissing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word._Application WordApp = new Application();
        WordApp.Visible = true;
        object filename = filePath;
        Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, 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);

        //设置居左
        WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

        //设置文档的行间距
        WordApp.Selection.ParagraphFormat.LineSpacing = 15f;
        //插入段落
        //WordApp.Selection.TypeParagraph();
        Microsoft.Office.Interop.Word.Paragraph para;
        para = WordDoc.Content.Paragraphs.Add(ref oMissing);
        //正常格式
        para.Range.Text = "This is paragraph 1";
        //para.Range.Font.Bold = 2;
        //para.Range.Font.Color = WdColor.wdColorRed;
        //para.Range.Font.Italic = 2;
        para.Range.InsertParagraphAfter();

        para.Range.Text = "This is paragraph 2";
        para.Range.InsertParagraphAfter();

        //插入Hyperlink
        Microsoft.Office.Interop.Word.Selection mySelection = WordApp.ActiveWindow.Selection;
        mySelection.Start = 9999;
        mySelection.End = 9999;
        Microsoft.Office.Interop.Word.Range myRange = mySelection.Range;

        Microsoft.Office.Interop.Word.Hyperlinks myLinks = WordDoc.Hyperlinks;
        object linkAddr = @"http://www.cnblogs.com/lantionzy";
        Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,
            ref oMissing);
        WordApp.ActiveWindow.Selection.InsertAfter("\n");

        //落款
        WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();
        WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

        //保存
        WordDoc.Save();
        WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
        WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
        return true;
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine(e.StackTrace);
        return false;
    }
}
#endregion 设置文档格式并添加文本内容、超链接
文中添加图片

#region 文档中添加图片
/// <summary>
/// 文档中添加图片
/// </summary>
/// <param name="filePath">word文件名</param>
/// <param name="picPath">picture文件名</param>
/// <returns></returns>
public static bool AddPicture(string filePath, string picPath)
{
    try
    {
        Object oMissing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word._Application WordApp = new Application();
        WordApp.Visible = true;
        object filename = filePath;
        Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, 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);

        //移动光标文档末尾
        object count = WordDoc.Paragraphs.Count;
        object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdParagraph;
        WordApp.Selection.MoveDown(ref WdLine, ref count, ref oMissing);//移动焦点
        WordApp.Selection.TypeParagraph();//插入段落

        object LinkToFile = false;
        object SaveWithDocument = true;
        object Anchor = WordDoc.Application.Selection.Range;
        WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(picPath, ref LinkToFile, ref SaveWithDocument, ref Anchor);

        //保存
        WordDoc.Save();
        WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
        WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
        return true;
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine(e.StackTrace);
        return false;
    }
}
#endregion 文档中添加图片
——部分资料整理自网络


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值