Ниже приведен код, который открывает и удаляет экземпляр Excel.Нам просто нужно убедиться, что все объекты, связанные с Excel, закрыты.
string strFilePath = @"C:\Sample.xlsx";
try
{
Excel.Application excelApp = null;
Excel.Workbook excelWorkbook = null;
Excel.Sheets excelSheets = null;
Excel.Worksheet excelWorksheet = null;
Excel.Workbooks excelWorkbooks = null;
Excel.Range excelUsedRange = null;
excelApp = new Microsoft.Office.Interop.Excel.Application();
int nData = excelApp.Hwnd;
// excelApp = new Excel.ApplicationClass();
//excelApp.Visible = true;
excelWorkbooks = excelApp.Workbooks;
excelWorkbook = excelWorkbooks.Add(System.Reflection.Missing.Value);
excelWorkbook = excelApp.Workbooks.Open(strFilePath, 2, false);
//excelWorkbook = excelApp.Workbooks.Open(strFilePath,
//
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);
excelSheets = excelWorkbook.Worksheets;
// excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(1);
excelWorksheet = (Excel.Worksheet)excelWorkbook.Worksheets["Dem0_1"];
excelUsedRange = excelWorksheet.UsedRange;
//Excel.Range lastCell = usedRange.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
//int lastRow = lastCell.Row;
//int lastCol = lastCell.Column;
//int rowMin = lastRow + 1;
//int colMin = lastCol + 1;
int nRowsCount = excelUsedRange.Rows.Count;
int nColCount = excelUsedRange.Columns.Count;
int N_Quality_Header = -1;
int N_Measurement_Name = -1;
int N_Lower_Tolerance = -1;
int N_Upper_Tolerance = -1;
//Read the Columns Index
for (int nColIndex = 1; nColIndex <= nColCount; nColIndex++)
{
Excel.Range cell = usedRange.Cells[1, nColIndex] as Excel.Range;
String strCellValue = cell.Value2.ToString();
if (strCellValue == "Quality Header")
N_Quality_Header = nColIndex;
else if (strCellValue.IndexOf("Measurement Name", StringComparison.OrdinalIgnoreCase) > -1)
N_Measurement_Name = nColIndex;
else if (strCellValue.IndexOf("Lower Tolerance", StringComparison.OrdinalIgnoreCase) > -1)
N_Lower_Tolerance = nColIndex;
else if (strCellValue.IndexOf("Upper Tolerance", StringComparison.OrdinalIgnoreCase) > -1)
N_Upper_Tolerance = nColIndex;
}
//Read all rows to get the values
for (int nRowIndex = 2; nRowIndex <= nRowsCount; nRowIndex++)
{
Excel.Range cellQualityHeader = usedRange.Cells[nRowIndex, N_Quality_Header] as Excel.Range;
String strValue = cellQualityHeader.Value2.ToString();
if (strValue == String_Empty)
continue;
}
}
catch (Exception oException)
{
}
finally
{
excelUsedRange.Clear();
//excelWorkbook.Save();
excelWorkbook.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
excelWorkbooks.Close();
excelApp.Quit();
Marshal.ReleaseComObject(excelUsedRange);
Marshal.ReleaseComObject(excelWorksheet);
Marshal.ReleaseComObject(excelSheets);
Marshal.ReleaseComObject(excelWorkbooks);
Marshal.ReleaseComObject(excelWorkbook);
Marshal.ReleaseComObject(excelApp);
excelUsedRange = null;
excelWorksheet = null;
excelSheets = null;
excelWorkbooks = null;
excelWorkbook = null;
excelApp = null;
GC.GetTotalMemory(false);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.GetTotalMemory(true);
}