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);
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM