.net读excel文件

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

        [DllImport("kernel32.dll")]
        public static extern int OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);


        public const int PROCESS_ALL_ACCESS = 0x1F0FFF;
        public const int PROCESS_VM_READ = 0x0010;
        public const int PROCESS_VM_WRITE = 0x0020;

      

 

public List<CaipiaoData>  ReadDataFromXls(string path, int dataNum)
        {
              //定义句柄变量
            IntPtr hwnd;

            //定义进程ID变量
            int pid = 0;

            //读取excel
            // int indexSheet = 1;

            Microsoft.Office.Interop.Excel.ApplicationClass Myexcel =
                new Microsoft.Office.Interop.Excel.ApplicationClass();
            if (Myexcel == null)
            {
                return null;
            }

            Myexcel.Visible = false ;
            Myexcel.DisplayAlerts = false;

            Myexcel.UserControl = true;

            object Nothing = System.Type.Missing;
            Myexcel.Application.Workbooks.Open(path, Nothing, Nothing, Nothing, Nothing, Nothing,
                Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
            Microsoft.Office.Interop.Excel.Workbooks wbs = Myexcel.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook wb = wbs[1];
            Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets["Sheet1"];
            List<CaipiaoData> datalist = new List<CaipiaoData>();
            for (int i = 1; i < ws.Rows.Count; i++)
            {
                Microsoft.Office.Interop.Excel.Range rng3 = ws.get_Range("B" + i, Nothing);
                if (rng3.Value2 == null)
                {
                    break;
                }
                string str = rng3.Value2.ToString().Trim();
               
                CaipiaoData cd = SlipData(str);
                datalist.Add(cd);
            }


            //获取Excel App的句柄
            hwnd = new IntPtr(Myexcel.Hwnd);
            //通过Windows API获取Excel进程ID
            GetWindowThreadProcessId(hwnd, out pid);

            try
            {

                wb.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlDoNotSaveChanges,
                    path, false);
                Myexcel.Quit();
            }
            catch (Exception)
            {
                //正常退出Excel失败,可以记录一下日志.
            }
            finally
            {
                //强制结束Excel进程
                if (Myexcel != null && pid > 0)
                {
                    int ExcelProcess;
                    ExcelProcess = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, false, pid);
                    //判断进程是否仍然存在
                    if (ExcelProcess > 0)
                    {
                        try
                        {
                            //通过进程ID,找到进程
                            System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(pid);
                            //Kill 进程
                            process.Kill();
                          
                        }
                        catch (Exception)
                        {
                           
                        }
                    }
                  
                }
            }

            return datalist;
        }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值