下面是针对最近项目,然后从网上搜集一部分资料得到的Word类,提供了word模板的创建,导入模板,删除word进程的函数,以及里面对word的操作。
添加表格、添加段落、添加页眉页脚、设置字体、对齐等。
有待牛人指点的是如何在里面添加项目符号。虽然知道是和ListFormat属性有关,但是没找到相关资料。
期待牛人出现。
public class MyWord
{
Object oMissing = System.Reflection.Missing.Value;
public Microsoft.Office.Interop.Word.Application WordApp;
public Microsoft.Office.Interop.Word.Document WordDoc;
#region 设置可视或者不可视
public void SetVisible()
{
WordApp.Visible = true;
}
public void SetInVisible()
{
WordApp.Visible = false;
}
#endregion
#region 动态生成Word文档并填充数据
/**/
/// <summary>
/// 动态生成Word文档并填充数据
/// </summary>
/// <returns>返回自定义信息</returns>
public void CreateWordFile(string WordPath,string PageHead,string PageFoot,string Title1,string Title2)
{
try
{
object filename = WordPath; //文件保存路径
//创建Word文档
WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
WordDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//添加页眉方法二:
if (WordApp.ActiveWindow.ActivePane.View.Type == Microsoft.Office.Interop.Word.WdViewType.wdNormalView || WordApp.ActiveWindow.ActivePane.View.Type == Microsoft.Office.Interop.Word.WdViewType.wdOutlineView)
{
WordApp.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;
}
WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
//string sHeader = "页眉内容";
WordApp.Selection.HeaderFooter.LinkToPrevious = false;
WordApp.Selection.HeaderFooter.Range.Text = PageHead;
WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
WordApp.Selection.HeaderFooter.LinkToPrevious = false;
WordApp.Selection.HeaderFooter.Range.Text = PageFoot;
WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置
WordApp.Selection.ParagraphFormat.LineSpacing = 20f;//设置文档的行间距
//标题段
Microsoft.Office.Interop.Word.Paragraph oPara2;
oPara2 = WordDoc.Content.Paragraphs.Add(ref oMissing);
oPara2.Range.Text = Title1;
oPara2.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
oPara2.Range.Font.Size = 30;
oPara2.Range.InsertParagraphAfter();
//副标题段
Microsoft.Office.Interop.Word.Paragraph oPara1;
oPara1 = WordDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = Title2;
oPara1.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
oPara1.Range.Font.Size = 20;
oPara1.Range.InsertParagraphAfter();
//第一段
Microsoft.Office.Interop.Word.Paragraph oPara3;
oPara3 = WordDoc.Content.Paragraphs.Add(ref oMissing);
oPara3.Range.Text = "1、第一段!";
oPara3.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
oPara3.Range.Font.Size = 12;
oPara3.Range.InsertParagraphAfter();
//第二段
Microsoft.Office.Interop.Word.Paragraph oPara4;
oPara4 = WordDoc.Content.Paragraphs.Add(ref oMissing);
oPara4.Range.Text = "2、第二段!各时段行车间隔及上线列车数如下表:";
oPara4.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
oPara4.Range.Font.Size = 12;
oPara4.Range.InsertParagraphAfter();
//换行创建一个表格
object count2 = 16;
object WdLine2 = Microsoft.Office.Interop.Word.WdUnits.wdLine;
WordApp.Selection.MoveDown(ref WdLine2, ref count2, ref oMissing);
//WordDoc.Range.Font.Size = 12;
//WordApp.Selection.Range.Font.Size = 12;
Microsoft.Office.Interop.Word.Table newTable1 = WordDoc.Tables.Add(WordApp.Selection.Range, 6, 3, ref oMissing, ref oMissing);
newTable1.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
newTable1.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable1.Columns[1].Width = 140f;
newTable1.Columns[2].Width = 140f;
newTable1.Columns[3].Width = 140f;
//填充表头内容
newTable1.Cell(1,1).Range.Font.Size = 12;
newTable1.Cell(1, 1).Range.Text = "时 段";
newTable1.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
newTable1.Cell(1, 2).Range.Font.Size = 12;
newTable1.Cell(1, 2).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable1.Cell(1,2).Range.Text="行车间隔";
newTable1.Cell(1, 2).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
newTable1.Cell(1,3).Range.Font.Size = 12;
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
newTable1.Cell(1,3).Range.Text="上线列车";
newTable1.Cell(1,3).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
Microsoft.Office.Interop.Word.Paragraph oPara7;
oPara7 = WordDoc.Content.Paragraphs.Add(ref oMissing);
oPara7.Range.Text = "";
oPara7.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
oPara7.Range.Font.Size = 12;
oPara7.Range.InsertParagraphAfter();
Microsoft.Office.Interop.Word.Paragraph oPara5;
oPara5 = WordDoc.Content.Paragraphs.Add(ref oMissing);
oPara5.Range.Text = "3、第三段!两端站对外服务首、尾班车开车时间:";
oPara5.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
oPara5.Range.Font.Size = 12;
oPara5.Range.InsertParagraphAfter();
//换行创建一个表格
object count3 = 16;
object WdLine3 = Microsoft.Office.Interop.Word.WdUnits.wdLine;
WordApp.Selection.MoveDown(ref WdLine3, ref count3, ref oMissing);
Microsoft.Office.Interop.Word.Table newTable2 = WordDoc.Tables.Add(WordApp.Selection.Range, 5, 5, ref oMissing, ref oMissing);
newTable2.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
newTable2.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable2.Columns[1].Width = 50f;
newTable2.Columns[2].Width = 110f;
newTable2.Columns[3].Width = 60f;
newTable2.Columns[4].Width = 100f;
newTable2.Columns[5].Width = 100f;
newTable2.Cell(1, 1).Range.Font.Size = 12;
newTable2.Cell(1, 1).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(1, 1).Range.Text = "上下行";
newTable2.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
newTable2.Cell(1, 2).Range.Font.Size = 12;
newTable2.Cell(1, 2).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(1, 2).Range.Text = "两端区间";
newTable2.Cell(1, 2).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
newTable2.Cell(1, 3).Range.Font.Size = 12;
newTable2.Cell(1, 3).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(1, 3).Range.Text = "首尾班车";
newTable2.Cell(1, 3).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
newTable2.Cell(1, 4).Range.Font.Size = 12;
newTable2.Cell(1, 4).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(1, 4).Range.Text = "车次";
newTable2.Cell(1, 4).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
newTable2.Cell(1, 5).Range.Font.Size = 12;
newTable2.Cell(1, 5).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(1, 5).Range.Text = "始发时间";
newTable2.Cell(1, 5).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
newTable2.Cell(2, 3).Range.Font.Size = 12;
newTable2.Cell(2, 3).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(2, 3).Range.Text = "首班车";
newTable2.Cell(2, 3).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
newTable2.Cell(3, 3).Range.Font.Size = 12;
newTable2.Cell(3, 3).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(3, 3).Range.Text = "尾班车";
newTable2.Cell(3, 3).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
newTable2.Cell(4, 3).Range.Font.Size = 12;
newTable2.Cell(4, 3).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(4, 3).Range.Text = "首班车";
newTable2.Cell(4, 3).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
newTable2.Cell(5, 3).Range.Font.Size = 12;
newTable2.Cell(5, 3).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(5, 3).Range.Text = "尾班车";
newTable2.Cell(5, 3).Range.Bold = 2;//设置单元格中字体为粗体
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
newTable2.Cell(2, 1).Merge(newTable2.Cell(3, 1));
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
newTable2.Cell(2, 1).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(2, 1).Range.Text = "上行";
newTable2.Cell(2, 1).Range.Bold = 2;//设置单元格中字体为粗体
newTable2.Cell(4, 1).Merge(newTable2.Cell(5, 1));
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
newTable2.Cell(4, 1).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(4, 1).Range.Text = "下行";
newTable2.Cell(4, 1).Range.Bold = 2;//设置单元格中字体为粗体
newTable2.Cell(2, 2).Merge(newTable2.Cell(3, 2));
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
newTable2.Cell(2, 2).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(2, 2).Range.Text = "奥体——》迈皋桥";
newTable2.Cell(2, 2).Range.Bold = 2;//设置单元格中字体为粗体
newTable2.Cell(4, 2).Merge(newTable2.Cell(5, 2));
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
newTable2.Cell(4, 2).Range.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
newTable2.Cell(4, 2).Range.Text = "迈皋桥——》奥体";
newTable2.Cell(4, 2).Range.Bold = 2;//设置单元格中字体为粗体
Microsoft.Office.Interop.Word.Paragraph oPara8;
oPara8 = WordDoc.Content.Paragraphs.Add(ref oMissing);
oPara8.Range.Text = "";
oPara8.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
oPara8.Range.Font.Size = 12;
oPara8.Range.InsertParagraphAfter();
Microsoft.Office.Interop.Word.Paragraph oPara6;
oPara6 = WordDoc.Content.Paragraphs.Add(ref oMissing);
oPara6.Range.Text = "4、第四段!";
oPara6.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
oPara6.Range.Font.Size = 12;
oPara6.Range.InsertParagraphAfter();
WordDoc.Paragraphs.Last.Range.Text = "模板创建时间:" + DateTime.Now.ToString();//“落款”
WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
}
catch
{
MessageBox.Show("Word模板生成异常!");
}
}
#endregion
public void WordSave(string WordPath)
{
object filesave = WordPath;
try
{
WordDoc.SaveAs(ref filesave, 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);
WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
}catch
{
MessageBox.Show("Word保存失败!");
}
}
#region 导入模板
/**/
/// <summary>
/// 导入模板
/// </summary>
/// <param name="filePath">模板文档路径</param>
public void ImportTemplate(string filePath)
{
object oMissing = System.Reflection.Missing.Value;
WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
WordApp.Visible = true;
object fileName = filePath;
WordDoc = WordApp.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);
}
#endregion
#region 清楚word进程
/**/
/// <summary>
/// 清楚word进程
/// </summary>
public static void KillWordProcess()
{
System.Diagnostics.Process[] myPs;
myPs = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in myPs)
{
if (p.Id != 0)
{
string myS = "WINWORD.EXE" + p.ProcessName + " ID:" + p.Id.ToString();
try
{
if (p.Modules != null)
if (p.Modules.Count > 0)
{
System.Diagnostics.ProcessModule pm = p.Modules[0];
myS += "\n Modules[0].FileName:" + pm.FileName;
myS += "\n Modules[0].ModuleName:" + pm.ModuleName;
myS += "\n Modules[0].FileVersionInfo:\n" + pm.FileVersionInfo.ToString();
if (pm.ModuleName.ToLower() == "winword.exe")
p.Kill();
}
}
catch
{ }
finally
{
;
}
}
}
}
public void close()
{
try
{
WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
GC.Collect();
}
catch
{
MessageBox.Show("无法关闭打开的Word文档!");
}
}
#endregion
}