• 态调用Excel避免因为版本不同而使用程序无法编辑或调试


    privatevoid button1_Click(object sender, EventArgs e)
            {
                object objApp;
                object objBook;
                object objBooks;
                object objSheets;
                object objSheet;
                object objRange;
                object objCells;
                object[] Parameters;
    
                try
                {
                    // 获取Excel类型并建立其实例                Type objExcelType = Type.GetTypeFromProgID("Excel.Application");
                    if (objExcelType ==null)
                    {
                        return;
                    }
                    objApp = Activator.CreateInstance(objExcelType);
                    if (objApp ==null)
                    {
                        return;
                    }
                    //获取Workbook集                objBooks = objApp.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, objApp, null);
    
                    //添加一个新的Workbook                objBook = objBooks.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, objBooks, null);
    
                    //获取Sheet集                objSheets = objBook.GetType().InvokeMember("Worksheets", BindingFlags.GetProperty, null, objBook, null);
    
                    //获取第一个Sheet对象                Parameters =new Object[1] { 1 };
                    objSheet = objSheets.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, objSheets, Parameters);
    
                    try
                    {
    
                        //获取操作范围for (int i =1; i <100; i++)
                        {
                            Parameters =new Object[2] { 1, i };
                            objCells = objSheet.GetType().InvokeMember("Cells", BindingFlags.GetProperty, null, objSheet, Parameters);
                            //向指定单元格填写内容值                        Parameters =new Object[1] { "Hello, World!" };
                            objCells.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, objCells, Parameters);
                        }
    
                    }
                    catch (Exception operException)
                    {
                        MessageBox.Show(operException.Message);
                    }
                    finally
                    {
                        //不提示保存                    Parameters =new Object[1] { false };
                        objApp.GetType().InvokeMember("DisplayAlerts", BindingFlags.SetProperty, null, objApp, Parameters);
    
                        //保存文件并退出                    Parameters =new Object[1] { @"D:\a.xls" };
                        objBook.GetType().InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, objBook, Parameters);
                        objApp.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, objApp, null);
                        GC.Collect();
                    }
                }
                catch (Exception theException)
                {
                    String errorMessage;
                    errorMessage ="Error: ";
                    errorMessage = String.Concat(errorMessage, theException.Message);
                    errorMessage = String.Concat(errorMessage, " Line: ");
                    errorMessage = String.Concat(errorMessage, theException.Source);
    
                    MessageBox.Show(errorMessage, "Error");
                }
            }
  • 相关阅读:
    Android 黑科技保活实现原理揭秘
    Flutter +携程=?
    图解设计模式-Prototype模式
    图解设计模式-Singleton模式
    比较B-tree索引与Hash索引
    类元数据Class Metadata
    DriverManager类
    XMLMapperBuilder类
    PooledDataSource类
    Java并发编程的艺术(笔记)
  • 原文地址:https://www.cnblogs.com/jinmingjie/p/2560969.html
Copyright © 2020-2023  润新知