C# 让Excel在互操作后关闭
我有一个从Excel电子表格中提取数据的应用程序。我没法把它清理干净。EXCEL.EXE将继续运行。我读过其他帖子,上面说需要对所有COM对象执行C# 让Excel在互操作后关闭,c#,excel,com,interop,C#,Excel,Com,Interop,我有一个从Excel电子表格中提取数据的应用程序。我没法把它清理干净。EXCEL.EXE将继续运行。我读过其他帖子,上面说需要对所有COM对象执行Marshal.ReleaseComObject()。我相信我这样做是正确的 为了让事情变得更清楚,我删除了错误检查代码(没有一个与Excel相关),但代码如下: public override MarketPriceItem[] GetMarketPrices() { string[] files = Directory.GetFiles(C
Marshal.ReleaseComObject()
。我相信我这样做是正确的
为了让事情变得更清楚,我删除了错误检查代码(没有一个与Excel相关),但代码如下:
public override MarketPriceItem[] GetMarketPrices()
{
string[] files = Directory.GetFiles(ConfigurationManager.AppSettings["XXSpreadsheets"]);
Excel.Application app = new Excel.Application { Visible = false };
List<MarketPriceItem> prices = new List<MarketPriceItem>();
Excel.Workbooks workbooks = app.Workbooks;
foreach (string filename in files)
{
Excel.Workbook wb = null;
wb = workbooks.Open(filename);
Excel.Worksheet sheet = wb.Sheets[1];
cell = sheet.Cells[1, 4];
string dateStr = cell.Text;
Marshal.ReleaseComObject(cell);
DateTime friday = DateTime.Parse(dateStr);
DateTime today = DateTime.Today;
int days = 5 - (friday - today).Days;
int todayCell = (days * 2) + 1;
int rows = sheet.UsedRange.Rows.Count;
foreach (int key in _codeToDescription.Keys)
{
for (int index = 4; index < rows; index++)
{
cell = sheet.Cells[index, 1];
string desc = cell.Text;
Marshal.ReleaseComObject(cell);
if (desc.Trim() == _codeToDescription[key].Trim())
{
cell = sheet.Cells[index, todayCell];
if (cell == null)
{
Marshal.ReleaseComObject(cell);
continue;
}
var valVal = cell.Value;
Marshal.ReleaseComObject(cell);
if (valVal == null)
{
continue;
}
prices.Add(new MarketPriceItem()
{
MarketCode = key.ToString(),
MarketDate = today,
MarketTypeCode = "XX",
Price = (decimal) valVal
});
}
}
}
Marshal.ReleaseComObject(sheet);
Marshal.ReleaseComObject(wb);
}
Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(app);
return prices.ToArray();
}
public覆盖MarketPriceItem[]GetMarketPrices()
{
string[]files=Directory.GetFiles(ConfigurationManager.AppSettings[“XXSpreadsheets”]);
Excel.Application app=new Excel.Application{Visible=false};
定价=新列表();
Excel.Workbooks Workbooks=app.Workbooks;
foreach(文件中的字符串文件名)
{
Excel.Workbook wb=null;
wb=工作簿.打开(文件名);
Excel.Worksheet sheet=wb.Sheets[1];
单元=薄板。单元[1,4];
字符串dateStr=cell.Text;
Marshal.ReleaseComObject(单元);
DateTime星期五=DateTime.Parse(dateStr);
DateTime today=DateTime.today;
整数天=5-(星期五-今天)。天;
int todayCell=(天*2)+1;
int rows=sheet.UsedRange.rows.Count;
foreach(int-key-in\u-code-todescription.key)
{
对于(int-index=4;索引<行;索引++)
{
单元格=页。单元格[索引,1];
string desc=cell.Text;
Marshal.ReleaseComObject(单元);
if(desc.Trim()==\u code说明[key].Trim())
{
单元格=表。单元格[索引,今日单元格];
if(单元格==null)
{
Marshal.ReleaseComObject(单元);
继续;
}
var VAVAL=单元值;
Marshal.ReleaseComObject(单元);
if(valVal==null)
{
继续;
}
prices.Add(新市场价格项()
{
MarketCode=key.ToString(),
MarketDate=今天,
MarketTypeCode=“XX”,
价格=(十进制)valVal
});
}
}
}
元帅发布对象(第页);
发布对象元帅(wb);
}
Marshal.ReleaseComObject(工作簿);
Marshal.ReleaseComObject(应用程序);
返回价格。ToArray();
}
我遗漏了什么?在代码末尾,您试过了吗
app.Quit();
这应该会退出正在运行的Excel实例。哇,我觉得自己很笨。谢谢。不用担心,我以前也穿过这种鞋(完全一样的问题,但有词!)