引用Microsoft.Office.Interop.Word
using System.IO;
using System.Reflection;
using System.Windows;
using MSWord = Microsoft.Office.Interop.Word;
public bool WriteNewWord2lead(string RepPath, out string FailMsg)//生成自动测试报表
{
try
{
object path;
MSWord.Application wordApp; //Word应用程序变量
MSWord.Document wordDoc; //Word文档变量
path = RepPath;
wordApp = new MSWord.ApplicationClass
{
Visible = false//使文档可见
}; //初始化
//如果已存在,则删除
if (File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
//wordDoc.PageSetup.PaperSize = MSWord.WdPaperSize.wdPaperA4;//设置纸张样式为A4纸
wordDoc.PageSetup.Orientation = MSWord.WdOrientation.wdOrientPortrait;//排列方式为垂直方向
wordDoc.PageSetup.TopMargin = 5.0f;
wordDoc.PageSetup.BottomMargin = 5.0f;
wordDoc.PageSetup.LeftMargin = 20.0f;
wordDoc.PageSetup.RightMargin = 5.0f;
//插入表格
object unite = MSWord.WdUnits.wdStory;
wordDoc.Content.InsertAfter("\n");//这一句与下一句的顺序不能颠倒,原因还没搞透
wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
int tableRow = 1;
int tableColumn = 5;
//定义一个Word中的表格对象
MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range,
tableRow, tableColumn, ref Nothing, ref Nothing);
//默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
table.Borders.Enable = 1;//这个值可以设置得很大,例如5、13等等
string[] colTest = new string[] { "缺陷名称", "测试缺陷数", "误报缺陷数", "漏报缺陷数", "备注" };
table.Rows.Add(ref Nothing); tableRow++;
for (int j = 1; j <= tableColumn; j++)
{
table.Cell(tableRow, j).Range.Text = colTest[j - 1];
}
int allNum = 0;
int allWuNum = 0;
int allLouNum = 0;
for (int i = 0; i < 3; i++)
{
table.Rows.Add(ref Nothing); tableRow++;
SetRangeText(table.Cell(tableRow, 1).Range, i.ToString(), MSWord.WdColor.wdColorRed);
SetRangeText(table.Cell(tableRow, 2).Range, i.ToString(), MSWord.WdColor.wdColorYellow);
SetRangeText(table.Cell(tableRow, 3).Range, i.ToString(), MSWord.WdColor.wdColorBlack);
SetRangeText(table.Cell(tableRow, 4).Range, i.ToString(), MSWord.WdColor.wdColorBlue);
}
table.Rows.Add(ref Nothing); tableRow++;
table.Cell(tableRow, 1).Range.Text = "合计";
table.Cell(tableRow, 2).Range.Text = allNum.ToString();
table.Cell(tableRow, 3).Range.Text = allWuNum.ToString();
table.Cell(tableRow, 4).Range.Text = allLouNum.ToString();
table.Rows.Add(ref Nothing); tableRow++;
for (int i = 1; i <= tableColumn; i++)
{
table.Columns[i].Width = 110;
}
for (int i = 1; i <= tableRow; i++)
{
table.Rows[i].Height = 30;
table.Rows[i].Range.Font.Size = 12F;
}
table.Cell(1, 1).Merge(table.Cell(1, tableColumn));//横向合并
table.Cell(1, 1).Range.Text = "自动测试报告";
table.Cell(tableRow, 1).Merge(table.Cell(tableRow, tableColumn));
table.Cell(tableRow, 1).Range.Text = "F";
table.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//表格文本居中
table.Range.Cells.VerticalAlignment = MSWord.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
//WdSaveFormat为Word 2003文档的保存格式
object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;// office 2007就是wdFormatDocumentDefault
wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭wordDoc文档对象
//看是不是要打印
//wordDoc.PrintOut();
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
catch (Exception ee)
{
FailMsg = ee.Message;
//MsgWnd.Show(this, ee.Message, "WriteDoc", MessageBoxButton.OK);
return false;
}
FailMsg = "";
return true;
}
public void SetRangeText(MSWord.Range range, string Text, MSWord.WdColor color)
{
range.Text = Text;
//修改文字颜色
range.Font.Color = color;
}
无法嵌入互操作类型ApplicationClass解决办法
word打开效果