• 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);
            }
  • 相关阅读:
    安卓打包名称
    安卓判断麦克风权限
    Redis基础了解
    Redis的安装
    SVN的日常使用和安装
    Maven基础2
    Maven基础1
    ResultSet的Type
    Hibernate高级查询QBC条件设置——Restrictions用法 引自:http://www.cnblogs.com/evon168/archive/2010/10/29/1863059.html
    Spring配置事务 http://www.cnblogs.com/leiOOlei/p/3725911.html
  • 原文地址:https://www.cnblogs.com/xifengyeluo/p/8276477.html
Copyright © 2020-2023  润新知