C# 使用自带Microsoft.Office.Interop.Excel简单操作Excel文件

项目添加应用 Microsoft.Office.Interop.Excel.dll 文件

引用命名空间:

using Excel = Microsoft.Office.Interop.Excel;

简单操作Excel文件:

        /// <summary>
        /// 简单操作Excel文件
        /// </summary>
        /// <param name="excelPath">excel 文件路径</param>
        /// <returns></returns>
        public void ExcelOp(string excelPath)
        {
            string ExcelFilePath = excelPath.Trim();
            //set columns
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("订单号", "A");//
            dic.Add("数量", "B");

            Excel.Application excel = new Excel.Application();
            Excel.Workbook wb = null;
            excel.Visible = false;//设置调用引用的 Excel文件是否可见
            excel.DisplayAlerts = false;
            wb = excel.Workbooks.Open(ExcelFilePath);
            Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1]; //索引从1开始 //(Excel.Worksheet)wb.Worksheets["SheetName"];
            int rowCount = 0;//有效行,索引从1开始
            try
            {
                rowCount = ws.UsedRange.Rows.Count;//赋值有效行

                string ordernum = string.Empty;
                string count = string.Empty;
                //循环行
                for (int i = 1; i <= rowCount; i++)//
                {
                    if (ws.Rows[i] != null)
                    {
                        ordernum = ws.Cells[i, dic["订单号"]].Value2.ToString();//取单元格值
                        count = ws.Cells[i, dic["数量"]].Value2.ToString();//ws.Cells[i, 2].Value2.ToString();
                    }
                }
                //循环列
                for (int i = 1; i <= ws.UsedRange.Columns.Count; i++)
                {
                    //ws.Columns[i]
                }
            }
            catch (Exception ex) { XtraMessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            finally
            {
                ClosePro(excelPath, excel, wb);
            }
        }

关闭Excel进程:

        /// <summary>
        /// 关闭Excel进程
        /// </summary>
        /// <param name="excelPath"></param>
        /// <param name="excel"></param>
        /// <param name="wb"></param>
        public void ClosePro(string excelPath, Excel.Application excel, Excel.Workbook wb)
        {
            Process[] localByNameApp = Process.GetProcessesByName(excelPath);//获取程序名的所有进程
            if (localByNameApp.Length > 0)
            {
                foreach (var app in localByNameApp)
                {
                    if (!app.HasExited)
                    {
                        #region
                        ////设置禁止弹出保存和覆盖的询问提示框   
                        //excel.DisplayAlerts = false;
                        //excel.AlertBeforeOverwriting = false;

                        ////保存工作簿   
                        //excel.Application.Workbooks.Add(true).Save();
                        ////保存excel文件   
                        //excel.Save("D:" + "\test.xls");
                        ////确保Excel进程关闭   
                        //excel.Quit();
                        //excel = null; 
                        #endregion
                        app.Kill();//关闭进程  
                    }
                }
            }
            if (wb != null)
                wb.Close(true, Type.Missing, Type.Missing);
            excel.Quit();
            // 安全回收进程
            System.GC.GetGeneration(excel);
        }
原文地址:https://www.cnblogs.com/xifengyeluo/p/8276477.html