using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data; using System.Data.OleDb; namespace WindowsFormsApplication7 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static DataSet GetExcelData(string str) { string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + str + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;'"; 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 button1_Click(object sender, EventArgs e) { OpenFileDialog filedialog = new OpenFileDialog(); string FileName = ""; if (filedialog.ShowDialog() == DialogResult.OK) { FileName = filedialog.FileName; dGViewExcel.DataSource = GetExcelData(FileName); dGViewExcel.DataMember = "[Sheet1$]"; for (int count = 0; (count <= (dGViewExcel.Rows.Count - 1)); count++) { dGViewExcel.Rows[count].HeaderCell.Value = (count + 1).ToString(); } } } } }
C# 读取excel文件到DATAVIEW
原创
©著作权归作者所有:来自51CTO博客作者lixia2018的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
c#:winform读取excel,并显示在griddataview
using System;using System.Collections.Generic;using System.ComponentModel
C# winform excel读取 griddataview microsoft -
C#读取Excel文件并用datagridview显示
试过网上大部分方法
向下兼容 连接字符串 Click -
[C++][opencv][cmakelists]opencv读取图像并显示
main.cpp\n";return 0;waitKey(0);return 0;
opencv c++ 计算机视觉 OpenCV #include -
C#读取Excel数据资料
C#操作Excel(读取)
excel c# 数据库 c html -
C# 读取Excel数据 并对日期是数字的进行处理
//按钮事件protected void btnQueryExcel_Click(object sender, EventArgs e) {
excel c# dataset string properties