有关使用net来实现将EXCEL表中的数据导入dataGridView并显示数据的解决方法

    今天写了一个将Excel表里的数据导入dataGridView的一个小程序,在网上找了很多材料,结果不是这有问题,就是那无法进行下去,真是一波三折呀,也让我长了很多见识。现在将遇到的问题和解决办法记录下来,供以后复习也供大家参考。

     实现功能:

      1、选择文件地址

      2、导入Excel表中的其中一张指定的表

      3、将指定的表里的内容显示到dataGridView中

     现将代码原型展示如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Drawing;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 using System.Data.OleDb;
  9 using System.Data;
 10 
 11 namespace CreatData
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19         /// <summary>
 20         /// 读取文件路径到textBox中
 21         /// </summary>
 22         /// <param name="sender"></param>
 23         /// <param name="e"></param>
 24         private void btn1_Click(object sender, EventArgs e)
 25         {
 26             OpenFileDialog odXls = new OpenFileDialog(); 
 27             odXls.InitialDirectory = "C://";  
 28             // 设置文件格式  
 29             odXls.Filter = "Excel files (*.xls)|*.xls";  
 30             odXls.FilterIndex = 2;  
 31             odXls.RestoreDirectory = true;
 32             if (odXls.ShowDialog() == DialogResult.OK & odXls.FileName.Length == 0)
 33             {
 34                 MessageBox.Show("请选择要导入的文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 35                 return;
 36             }
 37             else
 38             {
 39                 txtFilePath.Text = odXls.FileName.ToString();
 40             }
 41         }
 42         /// <summary>
 43         /// 将excel里的数据填充到dataGridView中
 44         /// </summary>
 45         /// <param name="sender"></param>
 46         /// <param name="e"></param>
 47         private void btn2_Click(object sender, EventArgs e)
 48         {
 49             DataTable dt = ReadExcelToGrid();
 50             for (int i = 0; i < dt.Columns.Count; i++)
 51             {
 52                 comboBox1.Items.Add(dt.Columns[i].ToString());
 53             }   
 54         }
 55 
 56         private DataTable ReadExcelToGrid()
 57         {
 58             OleDbConnection ole = null;
 59             OleDbDataAdapter da = null;
 60             DataTable dt = null;
 61             string strConn = "Provider=microsoft.ACE.oledb.12.0;"
 62                                 + "Data Source=" + txtFilePath.Text.Trim() + ";"
 63                                 + "Extended Properties=Excel 12.0";
 64             string sTableName = tableName.Text.Trim();
 65             string strExcel = "select * from [" + sTableName + "$]";
 66             try
 67             {
 68                 ole = new OleDbConnection(strConn);
 69                 ole.Open();
 70                 da = new OleDbDataAdapter(strExcel, ole);
 71                 dt = new DataTable();
 72                 da.Fill(dt);
 73                 dataGridView1.DataSource = dt;
 74                 for (int i = 0; i < dt.Columns.Count; i++)
 75                 {
 76                     for (int j = 0; j < dt.Rows.Count; j++)
 77                     {
 78                         dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
 79                     }
 80                 }
 81 
 82                 ole.Close();
 83             }
 84             catch (Exception Ex)
 85             {
 86                 MessageBox.Show(Ex.Message);
 87             }
 88             finally
 89             {
 90                 if (ole != null)
 91                     ole.Close();
 92             }
 93             return dt;
 94         }
 95 
 96         private void CreatDateBtn_Click(object sender, EventArgs e)
 97         {
 98             string BiginData = textBox1.Text.Trim();
 99             int CountData = Convert.ToInt16(CountBox.Text.Trim());
100             string LongData = LongBox.Text.Trim();
101             string Temp = null;
102            if (radioButton2.Checked ==true)
103             {
104                 for (int i = 0; i < CountData; i++)
105                 {
106                     Temp = BiginData.Trim() + "1"; 
107                 }
108             }
109             if (radioButton1.Checked == true)
110             {
111                 for (int i = 0; i < CountData; i++)
112                 {
113                     Temp = BiginData.Trim() + new Random().ToString();
114                 }
115             }
116         }
117 
118     }
119 }

运行后的界面:

 

遇到的问题:

1、   选择好文件后,在“文件路径”的文本框内不显示文件路径

2、 在将Excel表中的数据填入dataGridView时,显示了三个异常:a、Microsoft Jet 数据库引擎找不到对象'Sheet1$'。请确定对象是否存在; b、“未在本地计算机上注册“Microsoft. ACB.OLEDB.12.0”提供程序。”; C、“Microsoft Office Access 数据库引擎找不到对象“通讯录”

对于问题1:是因为我将显示路径的地址放错了,要放在else里面

对于问题2:a、是因为我之前使用的Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/1.xls;Extended Properties='Excel 8.0',但是我电脑是安装的Excel2007以上的版本,导致了Provider驱动无法使用,所以将驱动改成: Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/1.xlsx;Extended Properties='Excel 12.0'。一定要将“Jet”换成“ACB”,因为Jet自从4.0以后就已经停产了,不支持64位程序。所以使用“ACB”来代替。

当我将“Jet 4.0”换成“ACB 12.0”以后,又发生了异常b,这是因为我的计算机上没有安装Microsoft.ACE.OLEDB.12.0的驱动。在微软以下地址就可以下载驱动:http://www.microsoft.com/downloads/details.aspx?FamilyID=c06b8369-60dd-4b64-a44b-84b371ede16d&displayLang=zh-cn  (已验证,可以安心下载) 安装以后,重启以下程序就可以了。

之后又发生了异常c。这是因为对于表名,必须在后边加上 $ 如:Sheet1$ 否则会发生以下异常:Microsoft Office Access 数据库引擎找不到对象“Sheet1”。请确定该对象存在,并正确拼写其名称和路径名

二、提取Excel表中的第一行作为ComboBox的Items

1 for (int i = 0; i < dt.Columns.Count; i++)
2 
3      {
4 
5          comboBox1.Items.Add(dt.Columns[i].ToString());
6 
7      }

关键是comboBox1.Items.Add()方法和定位Excel表的第一行。

 

原文地址:https://www.cnblogs.com/xiaomeinvyimei/p/2853160.html