使用OLEDB将excel文件导入C# [英] Import excel file to C# using OLEDB
本文介绍了使用OLEDB将excel文件导入C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在按下按钮时在datagridview中显示excel文件。以下是按钮点击事件的代码:
I want to show an excel file in a datagridview when a button is pressed. Here is the code for the button click event:
private void button1_Click(object sender, EventArgs e)
{
string fileName = "C:\test.xlsx";
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
con.Open();
OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter("select * from [SHEET1$]", con);
DataSet excelDataSet = new DataSet();
cmd.Fill(excelDataSet);
xl.DataSource = excelDataSet.Tables[0];
con.Close();
}
但是我总是得到一个OleDbexception,上面写着创建文件失败,将黄色箭头指向'con.Open(); 'line
请帮我解决一下。
But always I get an OleDbexception which says "Failure Creating File" pointing a yellow arrow to the 'con.Open();' line
Please help me what can I do.
推荐答案
,con);
DataSet excelDataSet = new DataSet();
cmd.Fill(excelDataSet);
xl.DataSource = excelDataSet.Tables [ 0 ];
con.Close();
}
", con); DataSet excelDataSet = new DataSet(); cmd.Fill(excelDataSet); xl.DataSource = excelDataSet.Tables[0]; con.Close(); }
但我总是得到OleDbexception其中写着创建文件失败,将黄色箭头指向con.Open();行
请帮助我该怎么办。
But always I get an OleDbexception which says "Failure Creating File" pointing a yellow arrow to the 'con.Open();' line
Please help me what can I do.
On第一眼看,连接字符串是错误的。
请阅读: http://connectionstrings.com / excel-2007 [ ^ ]
On the first look of eye, the connection string is wrong.
Read about it at: http://connectionstrings.com/excel-2007[^]
这篇关于使用OLEDB将excel文件导入C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文