/// <summary> /// 合并到一个Excel中 /// </summary> /// <param name="mainFile"></param> /// <param name="files"></param> /// <param name="v"></param> public void MergeIntoMainExcel(string mainFile, string[] subFiles, string posInfoCol, string diffFile = "") { //建立一个新的 MainFile this.Wbk = this.App.Workbooks.Add(); Excel.Worksheet mainSht = this.Wbk.ActiveSheet; Excel.Workbook diffWbk = null; Excel.Worksheet diffSht = null; if (diffFile != "") { diffWbk = App.Workbooks.Add(); diffSht = diffWbk.ActiveSheet; } List <CellSegment_Ex> segList = new List <CellSegment_Ex>(); //遍历子文件获取信息并写入到mainsht中 string mainPath = Path.GetDirectoryName(mainFile); foreach (string subFile in subFiles) { string relativePath = subFile.Replace(mainPath, ".."); ShowStatus(string.Format("正在抽取文件:{0}...", relativePath)); segList.AddRange(ExctractContentInfoByWorkBook(subFile, relativePath, false, diffSht, posInfoCol)); } //写入到主表单 ShowStatus("正在写入数据..."); WriteToMainSht(mainSht, segList, posInfoCol); //隐藏位置信息列 mainSht.Range[posInfoCol + ":" + posInfoCol].ColumnWidth = 0; if (diffFile != "") { //删除第一行 因为第一行是空 diffSht.Range["1:1"].EntireRow.Delete(Excel.XlDeleteShiftDirection.xlShiftUp); diffSht.Range[posInfoCol + ":" + posInfoCol].ColumnWidth = 0; diffWbk.Title = "DifferentFont"; diffWbk.SaveAs(diffFile); //关闭 diffWbk.Close(Excel.XlSaveAction.xlDoNotSaveChanges); } this.Wbk.Title = "Merged"; this.Wbk.SaveAs(mainFile); //关闭 Wbk.Close(Excel.XlSaveAction.xlDoNotSaveChanges); ShowStatus("合并完成"); }
/// <summary> /// 无保存关闭 文档 /// </summary> public void CloseWbk() { Wbk.Close(Excel.XlSaveAction.xlDoNotSaveChanges); }