当前位置:编程学习 > C#/ASP.NET >>

C#关闭excel.exe进程的问题

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Excel;
using System.Runtime.InteropServices; 

namespace DMP.Common
{
    /// <summary>
    /// ExcelAccess 的摘要说明

    /// </summary>
    public class ExcelAccess
    {
        /// <summary>
        /// ExcelAccess构造方法
        /// </summary>
        public ExcelAccess()
        {
            // 构造方法
        }

        /// <summary>
        /// 获取进程Id
        /// </summary>
        /// <param name="hwnd"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

        /// <summary>
        /// 获取excel数据的datatTable
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <returns>datatTable</returns>
        public static System.Data.DataTable QueryData(string fileName)
        {
            return QueryData(fileName, null);
        }

        /// <summary>
        /// 获取excel数据的datatTable
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="sheetName">指定sheet名称</param>
        /// <returns>datatTable</returns>
        public static System.Data.DataTable QueryData(string fileName, string sheetName)
        {         
            ApplicationClass excelApp = new ApplicationClass();
            try
            {
                Workbook workBook = excelApp.Workbooks.Open(fileName,
                                                             0,
                                                             true,
                                                             5,
                                                             "",
                                                             "",
                                                             true,
                                                             XlPlatform.xlWindows,
                                                             "\t",
                                                             false,
                                                             false,
                                                             0,
                                                             true,
                                                             1,
                                                             0);

                Worksheet workSheet = null;
                if (CheckUtility.IsEmpty(sheetName))
                {
                    workSheet = (Worksheet)workBook.ActiveSheet;
                }
                else
                {
                    workSheet = (Worksheet)workBook.Sheets[sheetName];
                }

                int rc = workSheet.UsedRange.Rows.Count;
                System.Data.DataTable data = new System.Data.DataTable();
                if (rc < 2)
                {
                    return data;
                }

                int cc = workSheet.UsedRange.Columns.Count;
                for (int i = 0; i < cc; i++)
                {
                    data.Columns.Add(i.ToString());
                }

                System.Data.DataRow dr = null;

                for (int i = 1; i <= rc; i++)
                {
                    dr = data.NewRow();
                    for (int j = 1; j <= cc; j++)
                    {
                        dr[j - 1] = ((Range)workSheet.Cells[i, j]).Value2;
                    }
                    data.Rows.Add(dr);
                }
                return data;
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                IntPtr t = new IntPtr(excelApp.Hwnd);
                int k = 0;
                GetWindowThreadProcessId(t, out k);
                System.Diagnostics.Process excelProcess = System.Diagnostics.Process.GetProcessById(k);
                excelProcess.Kill();
                excelApp.Quit();
            }
        }

    }
}



可是出错说:
未能从程序集“App_Code.5fztjfaq, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“DMP.Common.ExcelAccess”,因为方法“GetWindowThreadProcessId”没有实现(没有 RVA)。

问问高手们,是哪里的问题啊? --------------------编程问答-------------------- 自己顶一下,望高手指教 --------------------编程问答-------------------- up
--------------------编程问答-------------------- 不太清楚,我现在再考虑怎样才能打开excel
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,