设置文档格式并添加内容
#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 文档中添加图片
——部分资料整理自网络