public static void BuildIterationReport(ProjectEntity project, string path) { ExcelInterop.Application excel = new ExcelInterop.Application(); excel.DisplayAlerts = false; ExcelInterop.Workbook workbook = excel.Workbooks.Add(); nativeResources.Add(workbook); ExcelInterop.Sheets sheets = workbook.Worksheets; nativeResources.Add(sheets); List <Tuple <string, Type> > allSheets = new List <Tuple <string, Type> >() { Tuple.Create <string, Type>("首页及说明", typeof(HomeSheet)), Tuple.Create <string, Type>("项目整体说明", typeof(OverviewSheet)), Tuple.Create <string, Type>("系统需求统计分析", typeof(FeatureSheet)), Tuple.Create <string, Type>("Backlog统计", typeof(BacklogSheet)), Tuple.Create <string, Type>("提交单分析", typeof(CommitmentSheet)), Tuple.Create <string, Type>("Bug统计分析", typeof(BugSheet)), Tuple.Create <string, Type>("审查工作分析", typeof(WorkReviewSheet)), Tuple.Create <string, Type>("工作量统计", typeof(WorkloadSheet)), Tuple.Create <string, Type>("改进建议", typeof(SuggestionSheet)), Tuple.Create <string, Type>("人员考评结果", typeof(PerformanceSheet)), }; ExcelInterop.Worksheet lastSheet = null; for (int i = 0; i < allSheets.Count; i++) { ExcelInterop.Worksheet sheet; if (lastSheet == null) { sheet = (ExcelInterop.Worksheet)sheets.Add(); } else { sheet = (ExcelInterop.Worksheet)sheets.Add(After: lastSheet); } lastSheet = sheet; nativeResources.Add(sheet); sheet.Name = allSheets[i].Item1; Console.WriteLine("\t" + sheet.Name); Type t = allSheets[i].Item2; var ci = t.GetConstructor(new Type[] { typeof(ExcelInterop.Worksheet) }); object obj = ci.Invoke(new object[] { sheet }); t.InvokeMember("Build", BindingFlags.InvokeMethod, null, obj, new object[] { project }); } Console.WriteLine("保存文件."); sheets.Select();//选择所有的sheet var window = excel.ActiveWindow; nativeResources.Add(window); window.DisplayGridlines = false;//都不显示表格线 var ite = TFS.Utility.GetBestIteration(project.Name); string fname = String.Format("{0}\\{1}总结({2}_{3}).xlsx", path, ite.Path.Replace("\\", " "), (DateTime.Parse(ite.StartDate)).ToString("yyyyMMdd"), (DateTime.Parse(ite.EndDate)).ToString("yyyyMMdd") ); workbook.SaveAs(fname); workbook.Close(); Console.WriteLine("释放资源."); foreach (object com in nativeResources) { TFS.Utility.ReleaseComObject(com); } Console.WriteLine("附件已经保存在:" + fname); excel.Quit(); }