欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#利用Excel直接读取数据到DataGridView

程序员文章站 2024-02-22 09:52:14
在winform里拖入一个datagridview控件,跟一个openfiledialog控件复制代码 代码如下:using system;using system.col...

在winform里拖入一个datagridview控件,跟一个openfiledialog控件

复制代码 代码如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using microsoft.office.core;
using excel=microsoft.office.interop.excel;
using system.windows.forms;
using system.reflection;

 
namespace excelproject
{
    public partial class excelform : form
    {

        private exceloperate eo;
        private string excelpath;
        private excel.application excel1;
        private excel.workbooks wbs = null;
        private excel.workbook wb = null;
        private excel.sheets  wss;
        private excel.worksheet ws = null;
        private excel.range range1 = null;

 
        public excelform()
        {
            initializecomponent();
            this.excel1 = new excel.application();
            if (excel1 == null)
            {
                messagebox.show("error");
                system.windows.forms.application.exit();
            }
            excel1.visible = true;

        }

 

        #region excel文件打开关闭操作
        private void 打开_click(object sender, eventargs e)
        {
            openfiledialog1 = new openfiledialog();
            openfiledialog1.title = "打开excel文件";
            openfiledialog1.filter = "excel03文件(*.xls)|*.xls|excel07文件(*.xlsx)|*.xlsx";
            openfiledialog1.initialdirectory = @"c:\users\administrator\desktop";
            openfiledialog1.restoredirectory = true;
            if (openfiledialog1.showdialog() == dialogresult.ok)
            {
                //打开文件对话框选择的文件
                excelpath = openfiledialog1.filename;
                eo = new exceloperate();
                readexcel(excelpath);

            }
        }

        void readexcel(string path)
        {
            object miss = system.reflection.missing.value;
            excel1.usercontrol = true;
            excel1.displayalerts = false;
            excel1.application.workbooks.open(excelpath, miss, miss, miss, miss,
                                             miss, miss, miss, miss,
                                             miss, miss, miss, miss,
                                             miss, miss);
            wbs = excel1.workbooks;
            wss = wbs[1].worksheets;
            ws = (excel.worksheet) wss.get_item(1);
            int rownum = ws.usedrange.cells.rows.count;
            int colnum = ws.usedrange.cells.columns.count;
            string cellstr = null;
            char ch = 'a';
            for (int i = 0; i < colnum; i++)
            {
                datagridview1.columns.add(i.tostring(), ch.tostring());
                datagridview1.rows.add(rownum);
                for (int j = 0; j <rownum; j++)
                {
                    cellstr = ch.tostring() + (j + 1).tostring();
                    datagridview1[i, j].value = ws.usedrange.cells.get_range(cellstr, miss).text.tostring();
                }
                ch++;
            }

 
        }

        #endregion

    }
}