zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Kinect 开发 —— 硬件设备解剖
    Kinect 开发 —— 引言
    (转)OpenCV 基本知识框架
    OpenCV —— 摄像机模型与标定
    OpenCV —— 跟踪与运动
    OpenCV —— 图像局部与分割(二)
    OpenCV —— 图像局部与部分分割(一)
    OpenCV —— 轮廓
    OpenCV —— 直方图与匹配
    OpenCV —— 图像变换
  • 原文地址:https://www.cnblogs.com/javalinux/p/14789546.html
Copyright © 2011-2022 走看看