数据后,关闭Excel应用程序已经被抓走 [英] Close EXCEL application process after data has been fetched

查看:166
本文介绍了数据后,关闭Excel应用程序已经被抓走的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的数据形成通过这样的方式在列表中的Excel文件中的列:

I am trying to get the Columns data form an Excel file in lists by this way :

private void Form1_Load(object sender, EventArgs e)
        {
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:/test.xlsx");
            Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            List<string> FirstColumnValues = new List<string>();
            List<string> SecondColumnValues = new List<string>();

            for (int row=1; row <= rowCount; row++)
            {
                for (int col = 1; col <= colCount; col++)
                {
                    switch (col)
                    {
                        case 1:
                            FirstColumnValues.Add(xlRange.Cells[row, col].Value2.ToString());
                            break;
                        case 2:
                            SecondColumnValues.Add(xlRange.Cells[row, col].Value2.ToString());
                            break;
                    }
                }
            }

            if (FirstColumnValues.Count != 0 && SecondColumnValues.Count != 0)
            {
                xlWorkbook.Close();
                xlApp.Quit();
                MessageBox.Show("Completed");
                Marshal.ReleaseComObject(xlRange);
                Marshal.ReleaseComObject(xlWorksheet);
                Marshal.ReleaseComObject(xlWorkbook);
                Marshal.ReleaseComObject(xlApp);
                xlApp = null;
            }
        }



现在的问题是,这个过程 EXCEL.EXE 不打烊,即使我已经尝试了所有的事情正确关闭它。我知道这里有张贴了关于正确关闭Excel进程的许多问题。但我不是一个专业的,我已经试过几乎所有我能做什么。
仍然没有运气。

The problem is, that the process EXCEL.EXE is not closing even I have tried all the things to close it properly. I know there are many questions posted here about closing excel process properly. But I am not a professional and I have tried almost everything what I can do. Still no luck.

所以,任何人可以告诉我什么是错的代码?以及如何处理可一旦当所有的数据都存储在列表关闭?

So, Can anybody please tell me what is wrong with this code? And how the process can be closed once when all the data is stored in the lists?

推荐答案

我以前去过那里。这里有一篇文章,说真的帮我整理出来:

I've been there before. Here's an article that really helped me sort it out:

http://devcity.net/Articles/239/1/article.aspx

Excel中似乎是关于终止很固执处理。您将很可能最终会杀死使用的System.Diagnostics.Process过程。

Excel seems to be very stubborn about terminating the process. You will more than likely end up killing the process using System.Diagnostics.Process.

这篇关于数据后,关闭Excel应用程序已经被抓走的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆