C#读取Excel.xlsx数据绑定DataGridView控件
摘要
C# .net开发 ,基于Oldb读取excel文件表格的数据,并且使用datatable虚表,绑定到datagridview控件。
案例简单,没有太多复杂操作,有需要可以了解学习下,这里以我的实际案例为例。
实现步骤:
步骤一:界面控件
界面拖动一个Label,Button,DataGridView控件。
步骤二:Button事件下(以下内容为代码,配注释加以说明) :
/*点击按钮导入数据到datagridview*/
//Openexcel 是Button按钮的name属性名称
//Openexcel_Click 是Button按钮的事件
private void Openexcel_Click(object sender, EventArgs e)
{
Openexcel.Enabled = false;
try
{
//start of 在datagridview控件中定义DataGridView 的Checkbutton勾选按钮
DataGridViewCheckBoxColumn checkColum = new DataGridViewCheckBoxColumn();
checkColum.HeaderText = " ";
checkColum.Name = "选择";//名称,后面需要用到
checkColum.DataPropertyName = "Column1";
checkColum.ReadOnly = false;
checkColum.TrueValue = true;
checkColum.FalseValue = false;
dataGridView1.DataSource = null; //每次打开清空内容
GetReadExcel getReadExcel = new GetReadExcel();
DataTable dt = getReadExcel.getData(filepathlb).Tables[0];//这里调用了getData()方法,参数是Label 类型的控件,dt是一张虚表
this.dataGridView1.Columns.Add(checkColum);
this.dataGridView1.DataSource = dt;//将表格赋给datagridview ,操作数据利用datagridview操作
}
catch(Exception ex)
{
Openexcel.Enabled = true;
return;
}
finally {
Openexcel.Enabled = true;
}
}
****getData()方法的代码如下:****
public DataSet getData(Label label)
{
//打开文件
OpenFileDialog file = new OpenFileDialog();
file.Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls";
file.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
file.Multiselect = false;
if (file.ShowDialog() == DialogResult.Cancel)
{
return null;
}
//判断文件后缀
var path = file.FileName;
label.Text = path;//这里定义的Label类型形参控件,这里需要用来显示文件路径
string fileSuffix = System.IO.Path.GetExtension(path);//文件的后缀
if (string.IsNullOrEmpty(fileSuffix)) {
return null;
}
using (DataSet ds = new DataSet())//创建虚表
{
//判断Excel文件是2003版本还是2007版本
string connString = "";
if (fileSuffix == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\""; //4.0 office2007之前版本
}
else
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";//12.0 office 2010版本 容易报错
//如果出现Microsoft.ACE.OLEDB.12.0 类错误,需要去下载驱动
//我这里安装了AccessDatabaseEngine_x64.exe
}
//读取文件
string sql_select = " SELECT * FROM [Sheet1$]";
using (OleDbConnection conn = new OleDbConnection(connString))
using (OleDbDataAdapter cmd = new OleDbDataAdapter(sql_select, conn))
{
conn.Open();//如果没安装,这里会报错
cmd.Fill(ds);
}
if (ds == null || ds.Tables.Count <= 0) return null;
return ds;//返回结果是数据集,里面存放的是表
}
步骤三:操作遍历datagridview
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)//行数据
{
if(this.dataGridView1.Rows[i].Cells["选择"].Value != null)//这里判断是否有勾选框选中
{
if((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)//判断第 i行的复选框是否选,Cells[0]是复选框添加在了每行的第一列 下标为 “0”。
{
//这里就是执行具体的事件任务。
}
}
}
步骤四:设置全选的效果和反转效果,添加一个checkbutton控件,在事件中添加以下代码:
private void chball_CheckedChanged(object sender, EventArgs e)
{
if (chball.Checked)
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
this.dataGridView1.Rows[i].Cells["选择"].Value = 1;//这里用到定义的列名“选择”
}
}
else
{
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
this.dataGridView1.Rows[i].Cells["选择"].Value = 0;
}
}
}
效果图:
总结:
没有总结。下班了。