• 大小: 41KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-19
  • 语言: 其他
  • 标签: Excel  Data  GridView  

资源简介

读取Excel文件到DataGridView,同时支持Excel2003和Excel2007。Excel的数据列是自适应的。 第一行需要写列名。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace ExcelDemo
{
    public partial class Form1 : Form
    {
        DataTable dt_Excel;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender EventArgs e)
        {
            OpenFileDialog openExcelFile = new OpenFileDialog();

            openExcelFile.Filter = “excel file (*.xls)|*.xls|All files (*.*)|*.*“;
            openExcelFile.RestoreDirectory = true;

            if (openExcelFile.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openExcelFile.FileName;
            }

        }
        private DataSet GetExcelData(string str)
        {
            string strCon;

            if (radioButton1.Checked)
            {
                //Excel 2003
                strCon = “ Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =“ + str + “;Extended Properties=Excel 8.0“;
            }
            else
            {
                //Excel 2007
                strCon = “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=“ + str + “;Extended Properties=\“Excel 12.0;HDR=YES\““;
            }
            OleDbConnection myConn = new OleDbConnection(strCon);
            string strCom = “ SELECT * FROM [Sheet1$]“;
            myConn.Open();
            OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom myConn);
            DataSet myDataSet = new DataSet();
            myCommand.Fill(myDataSet “[Sheet1$]“);
            myConn.Close();
            return myDataSet;
        }

        private void button2_Click(object sender EventArgs e)
        {
            DataSet ds_Excel = GetExcelData(textBox1.Text);
            dt_Excel = ds_Excel.Tables[0];
            dataGridView1.DataSource = dt_Excel;
        }

    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     目录          0  2015-02-06 10:54  ExcelDemo

     目录          0  2015-02-06 10:55  ExcelDemo\ExcelDemo

     目录          0  2015-02-06 10:54  ExcelDemo\ExcelDemo\bin

     目录          0  2015-02-06 10:56  ExcelDemo\ExcelDemo\bin\Debug

     文件      11264  2015-02-06 11:02  ExcelDemo\ExcelDemo\bin\Debug\ExcelDemo.exe

     文件      26112  2015-02-06 11:02  ExcelDemo\ExcelDemo\bin\Debug\ExcelDemo.pdb

     文件      11600  2015-02-06 11:03  ExcelDemo\ExcelDemo\bin\Debug\ExcelDemo.vshost.exe

     文件        490  2010-03-17 22:39  ExcelDemo\ExcelDemo\bin\Debug\ExcelDemo.vshost.exe.manifest

     文件       3677  2015-02-06 10:55  ExcelDemo\ExcelDemo\ExcelDemo.csproj

     文件       2030  2015-02-06 11:02  ExcelDemo\ExcelDemo\Form1.cs

     文件       5666  2015-02-06 11:02  ExcelDemo\ExcelDemo\Form1.Designer.cs

     文件       5817  2015-02-06 11:02  ExcelDemo\ExcelDemo\Form1.resx

     目录          0  2015-02-06 10:54  ExcelDemo\ExcelDemo\obj

     目录          0  2015-02-06 10:54  ExcelDemo\ExcelDemo\obj\x86

     目录          0  2015-02-06 11:02  ExcelDemo\ExcelDemo\obj\x86\Debug

     文件       2152  2015-02-06 10:54  ExcelDemo\ExcelDemo\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6402  2015-02-06 11:02  ExcelDemo\ExcelDemo\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       1249  2015-02-06 11:03  ExcelDemo\ExcelDemo\obj\x86\Debug\ExcelDemo.csproj.FileListAbsolute.txt

     文件      11264  2015-02-06 11:02  ExcelDemo\ExcelDemo\obj\x86\Debug\ExcelDemo.exe

     文件        180  2015-02-06 11:02  ExcelDemo\ExcelDemo\obj\x86\Debug\ExcelDemo.Form1.resources

     文件      26112  2015-02-06 11:02  ExcelDemo\ExcelDemo\obj\x86\Debug\ExcelDemo.pdb

     文件        180  2015-02-06 10:56  ExcelDemo\ExcelDemo\obj\x86\Debug\ExcelDemo.Properties.Resources.resources

     文件        488  2015-02-06 11:02  ExcelDemo\ExcelDemo\obj\x86\Debug\GenerateResource.read.1.tlog

     文件       1082  2015-02-06 11:02  ExcelDemo\ExcelDemo\obj\x86\Debug\GenerateResource.write.1.tlog

     目录          0  2015-02-06 10:54  ExcelDemo\ExcelDemo\obj\x86\Debug\TempPE

     文件        490  2015-02-06 10:54  ExcelDemo\ExcelDemo\Program.cs

     目录          0  2015-02-06 10:54  ExcelDemo\ExcelDemo\Properties

     文件       1350  2015-02-06 10:54  ExcelDemo\ExcelDemo\Properties\AssemblyInfo.cs

     文件       2866  2015-02-06 10:54  ExcelDemo\ExcelDemo\Properties\Resources.Designer.cs

     文件       5612  2015-02-06 10:54  ExcelDemo\ExcelDemo\Properties\Resources.resx

............此处省略7个文件信息

评论

共有 条评论