zoukankan      html  css  js  c++  java
  • 【转载】C# winform操作excel(打开、内嵌)

     

    C winform操作excel(打开、内嵌) - 静待〃花落 - 静待〃花落

    说明:显示的excel是利用模板创建的

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    //*************************************************
    // 先要添加对Excel的引用。选择项目-〉添加引用-〉COM-〉添加Microsoft Excel 9.0。(不同的office讲会有不同版本的dll文件)。
    //*************************************************
    using Excel;
    using System.Reflection; // For Missing.Value and BindingFlags
    using System.Runtime.InteropServices; // For COMException

    namespace ExcelTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();

            }
            //外部操作
            private void button1_Click(object sender, EventArgs e)
            {
                string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
                path = path.Substring(0, path.LastIndexOf('\'));
                //创建Excel对象
                Excel.Application ExcelApp = new Excel.Application();
                //新建工作簿
                //Excel.Workbook ExcelWb = ExcelApp.Workbooks.Add(true);                  
                Excel.Workbook ExcelWb = ExcelApp.Workbooks.Add(path + "\TestExcel");
                //新建工作表
                Excel.Worksheet ExcelWs = ExcelWb.ActiveSheet as Excel.Worksheet;
                Excel.Range ExcelRange = ExcelWs.Cells;
                //ExcelRange.Cells.Clear();           
                ExcelRange.Cells.set_Item(1, 6, "2010-03-03");
                ExcelRange.Cells.set_Item(1, 21, "9:30");
                ExcelRange.Cells.set_Item(1, 38, "会议室");
                ExcelRange.Cells.set_Item(2, 7, "王三");
                ExcelRange.Cells.set_Item(3, 7, "全体人员");
                ExcelRange.Cells.set_Item(5, 1, "关于作息制度的新安排");

                ExcelApp.Visible = true;
            }
            private void button2_Click(object sender, EventArgs e)
            {
            }
            //内嵌操作
            private void button3_Click(object sender, EventArgs e)
            {
                //-------------------------------------------------------------------------------
                // 1、右击工具箱,选择自定义工具箱,添加COM组件,选择“Microsoft Web 浏览器”(对应文件是winntsystem32shdocvw.dll),确定。在工具箱中将会出现文本为Explorer的WebBroser控件图标。
                // 2、在Form1中添加WebBrowser控件。(对象名却省是axWebBrowser1)
                //----------------------------------------------------------------------
                string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
                path = path.Substring(0, path.LastIndexOf('\'));
                Object refmissing = System.Reflection.Missing.Value;
                axWebBrowser1.Navigate(path + "\TestExcel.xlt", ref refmissing, ref refmissing, ref refmissing, ref refmissing);
            }
            private void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
            {
                Object refmissing = System.Reflection.Missing.Value;
                axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref   refmissing, ref   refmissing);

                Object o = e.pDisp;
                Object oDocument = o.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, o, null);
                Object oApplication = o.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oDocument, null);
                //Object   oName   =   o.GetType().InvokeMember("Name",BindingFlags.GetProperty   ,null,oApplication,null);  

                string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
                path = path.Substring(0, path.LastIndexOf('\'));

                //由于打开的是excel文件,所以这里的oApplication   其实就是Excel.Application   
                Excel.Application ExcelApp = (Excel.Application)oApplication;//这样就可以象上文中所述来操作Excel了
                Workbooks workbooks = ExcelApp.Workbooks;
                Excel.Workbook ExcelWb = workbooks.get_Item(1);
                Sheets sheets = ExcelWb.Worksheets;
                Worksheet ExcelWs = (Worksheet)sheets.get_Item(1);
                Excel.Range ExcelRange = ExcelWs.Cells;

                ExcelRange.Cells.set_Item(1, 6, "2010-03-03");
                ExcelRange.Cells.set_Item(1, 21, "9:30");
                ExcelRange.Cells.set_Item(1, 38, "会议室");
                ExcelRange.Cells.set_Item(2, 7, "王三");
                ExcelRange.Cells.set_Item(3, 7, "全体人员");
                ExcelRange.Cells.set_Item(5, 1, "关于作息制度的新安排");
               
            }
        }
    }

  • 相关阅读:
    post请求返回404
    启动网关服务报错 Unable to find GatewayFilterFactory with name RequestRateLimiter
    数据库远程连接linux报错2003 can't connect to MySQL server on ip (0) 防火墙没有开放端口3306
    idea下maven项目打包部署到tomcat服务器
    修改Idea背景色
    20种源代码测试工具
    作为一名测试工程师,需要具备哪些能力
    Android自动化测试工具——monkey简介及入门
    appium关于定位元素
    appium testcase2
  • 原文地址:https://www.cnblogs.com/chensuqian/p/9644862.html
Copyright © 2011-2022 走看看