private void CloseAllSheet() { try { workbook1.Close(true, Missing.Value, Missing.Value); excelApp1.Quit(); workbook2.Close(true, Missing.Value, Missing.Value); excelApp2.Quit(); releaseObject(excelApp1); releaseObject(excelApp2); releaseObject(workbook1); releaseObject(workbook2); new_workbook.Save(); new_workbook.Close(true, Missing.Value, Missing.Value); newapp.Quit(); releaseObject(newapp); releaseObject(new_workbook); Application.Current.Shutdown(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// 清理无用的Sheet(xlsx文件格式) /// </summary> /// <param name="xlsBook_trans"></param> public static void CleanSheet(Excel.Workbook xlsBook_trans) { Excel.Worksheet xlsSheet_trans = null; for (int i = xlsBook_trans.Sheets.Count; i >= 1; i--) { xlsSheet_trans = (Excel.Worksheet)xlsBook_trans.Sheets[i]; if (xlsSheet_trans.Name.Contains("Sheet")) { xlsSheet_trans.Delete(); } } xlsBook_trans.Save(); }
/// <summary> /// 清理无用的Sheet(xlsm,xlsx文件格式) /// </summary> /// <param name="xlsBook_trans"></param> public static void CleanSheet(Excel.Application app, Excel.Workbook xlsBook_trans) { Excel.Worksheet xlsSheet_trans = null; for (int i = xlsBook_trans.Sheets.Count; i >= 1; i--) { xlsSheet_trans = (Excel.Worksheet)xlsBook_trans.Sheets[i]; if (xlsSheet_trans.Name.Contains("Sheet")) { app.DisplayAlerts = false; //注意一定要加上这句 xlsSheet_trans.Delete(); app.DisplayAlerts = true; //注意一定要加上这句 } } xlsBook_trans.Save(); }
private static void Find(string path) { object missing = null; Excel.Range currentFind = null; Excel.Range firstFind = null; var app = new Excel.Application(); app.Visible = true; Excel.Workbook workbook = app.Workbooks.Open(path, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); var worksheet = workbook.Sheets[1]; Excel.Range foundNames = worksheet.Range["A1", "B3"]; // You should specify all these parameters every time you call this method, // since they can be overridden in the user interface. currentFind = foundNames.Find("Peter, Paul, Mary", LookIn: XlFindLookIn.xlValues, LookAt: XlLookAt.xlPart); currentFind.Replace(What: "Peter, Paul, Mary", Replacement: "Peter, John, Susan"); workbook.Save(); }
private void buttonXP3_Click(object sender, EventArgs e) { if (this.dw_doctorearning.RowCount > 0) { SaveFileDialog FD = new SaveFileDialog(); FD.Filter = "Excel 文档|*.xls"; FD.Title = "导出"; FD.ShowDialog(); int m_intColumnCount = this.dw_doctorearning.ColumnCount; if (FD.FileName.Trim() != "") { this.dw_doctorearning.SaveAs(FD.FileName.Trim(), Sybase.DataWindow.FileSaveAsType.Excel, true, Sybase.DataWindow.FileSaveAsEncoding.Utf8); string m_strTemp; string m_strText; Excel.Application excel = new Excel.Application(); Excel.Workbook wb = null; object missing = System.Reflection.Missing.Value; wb = excel.Workbooks.Open(FD.FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); excel.Visible = false; for (int i = 1; i <= m_intColumnCount; i++) { m_strTemp = this.dw_doctorearning.Describe("#" + i.ToString() + ".name"); m_strTemp += "_t.text"; m_strText = this.dw_doctorearning.Describe(m_strTemp); excel.Cells[1, i] = m_strText; } wb.Save(); excel.Quit(); IntPtr t = new IntPtr(excel.Hwnd); int k = 0; GetWindowThreadProcessId(t, out k); System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); p.Kill(); } } }
// Catch the before close event private void ApplicationOnWorkbookBeforeClose(Excel.Workbook wb, ref bool cancel) { if (!wb.Saved) { switch (MessageBox.Show(text: $"Do you want to save changes you made to '{this.Application.ActiveWorkbook.Name}'?", caption: "Microsoft Excel", buttons: MessageBoxButtons.YesNoCancel, icon: MessageBoxIcon.Exclamation)) { case DialogResult.Cancel: // case want to cancel - break the closing event { cancel = true; return; } case DialogResult.Yes: // case user want to save wb - save wb { wb.Save(); break; } case DialogResult.No: // case user don't want to save wb - mark wb as saved to avoid the application messabe to appear { wb.Saved = true; break; } } } if (IsAnyWorkbookOpen()) { // replace this with your code MessageBox.Show("Some books will still be open, don't turn off the ribbon"); return; } // replace this with your code MessageBox.Show("All books will be closed closed"); }
/// <summary> /// Replace single scripts depend value sheet /// </summary> public void DependentValReplace(string source, string file) { string _sPath_Source = source; string _sPath_Destine = file; object oMissing = System.Reflection.Missing.Value; System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); objExl_Source.EnableEvents = false; objExl_Source.DisplayAlerts = false; objExl_Source.Visible = false; double[] widthOfScripts = new double[15] { 84.75, 134.25, 54, 36.75, 127.5, 220.5, 80.25, 99, 103.5, 132.75, 54, 54, 54, 54, 54 }; // objExl_Source.ScreenUpdating = true; Excel.Workbook objWBook_Source = objExl_Source.Workbooks.Open(_sPath_Source, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); //ApplicationClass objExl_Destine = new ApplicationClass(); //objExl_Destine.EnableEvents = false; //objExl_Destine.DisplayAlerts = false; //objExl_Destine.Visible = false; // objExl_Destine.ScreenUpdating = true; Excel.Workbook objWBook_Destine = objExl_Source.Workbooks.Open(_sPath_Destine, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); //Workbook objWBook_Source = objExl_Source.Workbooks.Open(_sPath_Source, // 0,false,5,oMissing,oMissing,false,XlPlatform.xlWindows, // oMissing,true,false,0,true,false,false); //Workbook objWBook_Destine = objExl_Destine.Workbooks.Open(_sPath_Destine, // 0, false, 5, oMissing, oMissing, false, XlPlatform.xlWindows, // oMissing, true, false, 0, true, false, false); string strSheetName = "Scripts"; Excel.Worksheet objWSheet_Source = (Excel.Worksheet)objWBook_Source.Worksheets[strSheetName]; //objWSheet_Source.Copy((Excel.Worksheet)objWBook_Destine.Sheets[objWBook_Destine.Sheets.Count], oMissing); //strSheetName = "Action DataValDepend"; //objWSheet_Source = (Excel.Worksheet)objWBook_Source.Worksheets[strSheetName]; //objWSheet_Source.Copy((Excel.Worksheet)objWBook_Destine.Sheets[objWBook_Destine.Sheets.Count], oMissing); Excel.Range range = objWSheet_Source.get_Range(objWSheet_Source.Cells[1, 1], objWSheet_Source.Cells[900, 20]); // range.Value = "123"; Excel.Worksheet sheet1 = (Excel.Worksheet)objWBook_Destine.Sheets[1]; Excel.Range range1 = sheet1.get_Range(sheet1.Cells[1, 1], sheet1.Cells[900, 20]); range.Copy(range1); for (int i = 1; i <= 15; i++) { Excel.Range rmgSource = (Excel.Range)objWSheet_Source.Cells[1, i]; Excel.Range rmgDestine = (Excel.Range)sheet1.Cells[1, i]; rmgDestine.EntireColumn.ColumnWidth = rmgSource.EntireColumn.ColumnWidth; //rmgDestine.EntireColumn.ColumnWidth = widthOfScripts[i - 1]; //rmg.EntireColumn.AutoFit(); //rmg.Width = widthOfScripts[i-1]; //MessageBox.Show(rmg.Width.ToString()); } foreach (Excel.Worksheet xSheet in objWBook_Source.Worksheets) { if (xSheet.Name.Equals("Notes") || xSheet.Name.Equals("Note")) { strSheetName = "Notes"; objWSheet_Source = (Excel.Worksheet)objWBook_Source.Worksheets[strSheetName]; objWSheet_Source.Copy(oMissing, (Excel.Worksheet)objWBook_Destine.Sheets[objWBook_Destine.Sheets.Count]); } } //objWBook_Source.Save(); objWBook_Destine.Save(); objWBook_Destine = null; objExl_Source.Quit(); // objExl_Destine.Quit(); }
public void CloseExcel() { wbb.Save(); wbb.Close(true, System.Type.Missing, System.Type.Missing); wbb.Application.Quit(); }
/// <summary> /// Fetch all the List of Sheets and Charts /// </summary> /// <param name="strFileName">Select the xls File Name</param> /// <param name="bReadOnly">Specifies how to open it</param> public void GetListofSheetsAndCharts(string strFileName, bool bReadOnly, DropDownList drpList) { Excel.Workbook workbook = null; try { if (!bReadOnly) { // Write Mode Open //workbook = appOP.Workbooks.Open(strFileName, 2, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, true, 0, true, 1, 0); // For Optimal Opening workbook = appOP.Workbooks.Open(strFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } else { // Read Mode Open //workbook = appOP.Workbooks.Open(strFileName, 2, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, true, 0, true, 1, 0); // For Optimal Opening workbook = appOP.Workbooks.Open(strFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } // Reading of Excel File object SheetRChart = null; int nTotalWorkSheets = workbook.Sheets.Count; int nIndex = 0; for (int nWorkSheet = 1; nWorkSheet <= nTotalWorkSheets; nWorkSheet++) { SheetRChart = workbook.Sheets[(object)nWorkSheet]; if (SheetRChart is Excel.Worksheet) { ListItem lstItemAdd = new ListItem(((Excel.Worksheet)SheetRChart).Name + "*WorkSheet", nIndex.ToString(), true); drpList.Items.Add(lstItemAdd); lstItemAdd = null; nIndex++; } else if (SheetRChart is Excel.Chart) { ListItem lstItemAdd = new ListItem(((Excel.Chart)SheetRChart).Name + "*Chart", nIndex.ToString(), true); drpList.Items.Add(lstItemAdd); lstItemAdd = null; nIndex++; } } if (workbook != null) { if (!bReadOnly) { // Write Mode Close workbook.Save(); workbook = null; } else { // Read Mode Close workbook.Close(false, false, Type.Missing); workbook = null; } } } catch (Exception expFile) { Response.Write(expFile.ToString()); } finally { if (workbook != null) { if (!bReadOnly) { // Write Mode Close workbook.Save(); workbook = null; } else { // Read Mode Close workbook.Close(false, false, Type.Missing); workbook = null; } } } }
/// <summary> /// Displaying a given Excel WorkSheet /// </summary> /// <param name="strFileName">The Filename to be selected</param> /// <param name="strSheetRChartName">The Sheet or Chart Name to be Displayed</param> /// <param name="bReadOnly">Specifies the File should be open in Read only mode, /// If it is true then the File will be open ReadOnly</param> /// <param name="lblErrorText">If any Error Occurs should be Displayed</param> /// <param name="bIsChart">Specifies whether it is a Chart</param> /// <returns>Returns Boolean Value the Method Succeded</returns> public bool DisplayExcelSheet(string strFileName, string strSheetRChartName, bool bReadOnly, Label lblErrorText, bool bIsChart) { appOP.DisplayAlerts = false; Excel.Workbook workbook = null; Excel.Worksheet worksheet = null; Excel.Chart chart = null; try { if (!bReadOnly) { // Write Mode Open //workbook = appOP.Workbooks.Open(strFileName, 2, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, true, 0, true, 1, 0); // For Optimal Opening workbook = appOP.Workbooks.Open(strFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } else { // Read Mode Open //workbook = appOP.Workbooks.Open(strFileName, 2, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, true, 0, true, 1, 0); // For Optimal Opening workbook = appOP.Workbooks.Open(strFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } // Reading of Excel File if (bIsChart) { chart = (Excel.Chart)workbook.Charts[strSheetRChartName]; } else { worksheet = (Excel.Worksheet)workbook.Sheets[strSheetRChartName]; } // Reading the File Information Codes goes Here if (bIsChart) { if (chart == null) { lblErrorText.Text = strSheetRChartName + " Chart is Not Available"; } else { ExcelChartRead(chart, this.pnlBottPane); } } else { if (worksheet == null) { lblErrorText.Text = strSheetRChartName + " Sheet is Available"; } else { this.pnlBottPane.Controls.Add(ExcelSheetRead(worksheet, lblErrText)); } } if (!bReadOnly) { // Write Mode Close workbook.Save(); workbook = null; } else { // Read Mode Close workbook.Close(false, false, Type.Missing); workbook = null; } } catch (Exception expInterop) { lblErrText.Text = expInterop.ToString(); return(false); } finally { if (workbook != null) { if (!bReadOnly) { // Write Mode Close workbook.Save(); workbook = null; } else { // Read Mode Close workbook.Close(false, false, Type.Missing); workbook = null; } } appOP.DisplayAlerts = true; } return(true); }
static void Main(string[] args) { Console.WriteLine("**********************************************"); Console.WriteLine("*************** INICI *********************"); Console.WriteLine("******* ACTUALITZACIO DE DOCUMENTS ***********"); Console.WriteLine("**********************************************"); Console.WriteLine("**********************************************"); string configPath = AppDomain.CurrentDomain.BaseDirectory + "\\config.ini"; PropertiesUtility properties = new PropertiesUtility(); properties.LoadProperties(configPath); string timeoutStr = properties.GetProperty("timeout"); Console.Write("Espera per document: " + timeoutStr + " millisegons"); Console.WriteLine(); int timeout = Int32.Parse(timeoutStr); string pathList = properties.GetProperty("pathlist"); Console.WriteLine("Llista ubicada a: " + pathList + "Lista.xlsx"); string pathExcels = properties.GetProperty("pathexcels"); Console.WriteLine("Excels ubicats a: " + pathExcels + "*.*"); Excel.Application application = new Excel.Application(); string path = pathList + "Lista.xlsx"; Excel.Workbook lista = application.Workbooks.Open(path); Excel.Worksheet mySheet = (Excel.Worksheet)lista.Sheets["Hojas"]; string[] terms = new string[400]; int numTerms = 0; Excel.Range dataRange = null; for (int row = 1; row < mySheet.Rows.Count; row++) { dataRange = (Excel.Range)mySheet.Cells[row, 1]; if (dataRange.Value2 == null) { break; } if (row > 400) { break; } terms[row - 1] = String.Format(dataRange.Value2.ToString()); numTerms++; } Console.WriteLine(); lista.Close(false, path, null); for (int i = 0; i < numTerms; i++) { Excel.Workbook workbook = application.Workbooks.Open(pathExcels + terms[i]); workbook.RefreshAll(); Console.Write(i + 1 + ". Actualitzant... " + pathExcels + terms[i]); System.Threading.Thread.Sleep(timeout); Console.Write(" ok"); Console.WriteLine(); application.DisplayAlerts = false; workbook.Save(); workbook.Close(false, pathExcels + terms[i], null); workbook = null; } Console.WriteLine("**********************************************"); Console.WriteLine("*************** FI *********************"); Console.WriteLine("******* ACTUALITZACIO DE DOCUMENTS ***********"); Console.WriteLine("**********************************************"); Console.WriteLine("**********************************************"); application.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(application); }
//********************************************************************** /// <summary> /// Close the excel /// </summary> public void close() { xBook.Save(); excelMain.Quit(); excelMain = null; }
/// <summary> /// 复制Sheet,填充Sheet /// </summary> public bool ExceptTransFile(DataTable Up_dt, DataTable Down_dt, int currentPage, int totalPages) { Excel.Application xlsApp = new Excel.Application(); Excel.Workbook xlsBook = null; Excel.Worksheet xlsSheet = null; Excel.Workbook xlsBook_trans = null; Excel.Worksheet xlsSheet_trans = null; bool isCleanSheet = false;//是否清空没用的Sheet try { string partNumberExt = Up_dt.Rows[0][1].ToString(); //品号_面次:DEC-21458-00-1A_B string partNumber = Up_dt.Rows[0][1].ToString().Substring(0, 15); //品号:DEC-21458-00 string partDesc = mGetPartData(partNumber, imsapi, sessionContext); //品名:北京现代ADc倒车雷达SMT半成品 string programName = partNumberExt.Replace("_", " ") + " " + Up_dt.Rows[1][1].ToString(); //程序名称:DEC-21458-00-1A B V1.4-S1E string machineGroup = Up_dt.Rows[1][1].ToString().Substring(Up_dt.Rows[1][1].ToString().IndexOf('-') + 1); //机械别:S1E string todayDate = DateTime.Now.ToString("yyyy-M-d"); string side = Up_dt.Rows[9][1].ToString().ToUpper().Substring(0, 3); //面次:TOP/BOT int totalPositionCount = 0; //合计点数 string sheetName = machineGroup + "-" + side + ((totalPages == 1) ? "" : "(" + currentPage + ")"); //Sheet名称 string page = currentPage + "/" + totalPages; #region 创建excel文件 //1,验证目录 if (!Directory.Exists(config.GenerateFolder)) { Directory.CreateDirectory(config.GenerateFolder); errorHandler(0, "创建目录:" + config.GenerateFolder); } //2,验证文件 string filePath = config.GenerateFolder + partNumber + ".xlsm"; if (!File.Exists(filePath)) { ExcelHelper.CopyFile(config.TemplateFolder, filePath); errorHandler(0, "创建文件:" + filePath); isCleanSheet = true; } #endregion #region 制Sheet GC.Collect(); //模板页Sheet xlsBook = xlsApp.Workbooks.Open(config.TemplateFolder, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); xlsSheet = (Excel.Worksheet)xlsBook.ActiveSheet; //生成文件Sheet xlsBook_trans = xlsApp.Workbooks.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); xlsSheet_trans = (Excel.Worksheet)xlsBook_trans.Sheets[1]; //将模板的格式复制过来 xlsSheet.Copy(xlsSheet_trans, Type.Missing); #endregion #region 填充Sheet //填充表头部分 xlsSheet_trans = (Excel.Worksheet)xlsBook_trans.ActiveSheet; xlsSheet_trans.Cells[4, 2] = machineGroup; xlsSheet_trans.Cells[4, 9] = page; xlsSheet_trans.Cells[5, 3] = programName; xlsSheet_trans.Cells[6, 3] = partDesc; xlsSheet_trans.Cells[7, 3] = partNumber; xlsSheet_trans.Cells[7, 7] = todayDate; //表身部分 int idx = 9;//表身开始行(这个值是根据原始数据文档出来的) foreach (DataRow row in Down_dt.Rows) { string zAxisNumber = row["pos."].ToString(); //Z轴编号 string partNumberSource = row["Part Num"].ToString(); //品号 //料架型号(FUJI8mm*16PH) 要求:1,只要大于"8mm"不需后面的直接设置为"FJ24mm";2,只要Z轴编号为6-A或6-B开头的料架型号为TRAY string prefixRackType = "FUJI"; //料架型号前缀 string suffixRackType = row["Width"].ToString() + "*" + row["Feed Pitch"].ToString() + "PH"; //料架型号后缀 string valueRackType = row["Width"].ToString().TrimEnd('m'); Match canTransInt = Regex.Match(valueRackType, "\\d"); if (canTransInt.Success) { if (Convert.ToInt32(valueRackType) > 8) { prefixRackType = "FJ"; suffixRackType = "24mm"; } } if (zAxisNumber.ToUpper().Trim().StartsWith("6-A") || zAxisNumber.ToUpper().Trim().StartsWith("6-B")) { prefixRackType = "TRAY"; suffixRackType = ""; } string rackType = prefixRackType + suffixRackType; //材料规格(这个有点复杂) string materialSpec = GenerateMaterialSpec(partNumberSource.Trim()); //部品贴装位置,点数 string position = string.Empty; int positionCount = 0; GeneratePosition(row["Reference"].ToString(), out position, out positionCount); totalPositionCount += positionCount; //插入数据(用固定的模板来做) xlsSheet_trans.Cells[idx, 1] = zAxisNumber; xlsSheet_trans.Cells[idx, 2] = rackType; xlsSheet_trans.Cells[idx, 3] = partNumberSource; xlsSheet_trans.Cells[idx, 4] = materialSpec; xlsSheet_trans.Cells[idx, 5] = position; xlsSheet_trans.Cells[idx, 8] = positionCount; //部品贴装位置要合并单元格 ExcelHelper.MerageCell(xlsBook_trans, idx, 5, idx, 7); //自动调衡行高(根据装贴位置数,4个一行) Excel.Range range1 = (Excel.Range)xlsSheet_trans.Rows[idx, Missing.Value]; range1.RowHeight = position.Length < 21 ? 21 : Math.Ceiling((position.Length - Math.Ceiling(position.Length / 20.00)) / 19.00) * 15; idx++; } //添加行"以下空白" if (isOver) { xlsSheet_trans.Cells[idx, 2] = "以下空白"; //部品贴装位置要合并单元格 ExcelHelper.MerageCell(xlsBook_trans, idx, 5, idx, 7); idx++; } //填充点数合并 xlsSheet_trans.Cells[32, 8] = totalPositionCount; #endregion //修改Sheet名称之前先将先前同名的删除掉 Excel.Worksheet xlsSheet_clean = null; for (int i = xlsBook_trans.Sheets.Count; i >= 1; i--) { xlsSheet_clean = (Excel.Worksheet)xlsBook_trans.Sheets[i]; if (xlsSheet_clean.Name.Contains(sheetName)) { errorHandler(0, "清除Sheet:" + xlsSheet_clean.Name); xlsSheet_clean.Delete(); } } xlsBook_trans.Save(); //修改Sheet名称 ((Excel.Worksheet)xlsBook_trans.ActiveSheet).Name = sheetName; xlsBook_trans.Save(); if (isCleanSheet) { ExcelHelper.CleanSheet(xlsApp, xlsBook_trans); } errorHandler(0, "生成成功"); return(true); } catch (Exception ex) { errorHandler(2, ex.Message); return(false); } finally { xlsBook.Close(false, Type.Missing, Type.Missing); xlsBook_trans.Close(false, Type.Missing, Type.Missing); xlsApp.Quit(); KillSpecialExcel(xlsApp); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsBook_trans); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsSheet_trans); xlsSheet = null; xlsBook = null; xlsApp = null; xlsSheet_trans = null; xlsBook_trans = null; GC.Collect(); GC.WaitForPendingFinalizers(); } }