[英]Reading excel file in c#.net cell by cell
I'm new to c#.net
我是c#.net
新手
I have excel sheet and I want to import into database
. 我有Excel表格,我想导入database
。
I want to read it cell by cell and want to insert value in database
. 我想逐个单元地读它并想在database
插入值。
this.openFileDialog1.FileName = "*.xls";
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
string path = openFileDialog1.FileName;
string connectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=no;IMEX=1;""", openFileDialog1.FileName);
string query = String.Format("select * from [{0}$]", "Sheet3");
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
Check out NPOI 查看NPOI
http://npoi.codeplex.com/ http://npoi.codeplex.com/
It's the .NET version of Apache's POI Excel implementation. 它是Apache的POI Excel实现的.NET版本。 It'll easily do what you need it to do, and will help avoid some of the problems ( ie local copy of Excel, or worse, copy of Excel on the server ) that you'll face when using the Jet provider. 它可以轻松地完成您需要它做的事情,并且有助于避免在使用Jet提供程序时您将面临的一些问题(即Excel的本地副本,或者更糟糕的是,服务器上的Excel副本)。
I assume that after you execute the code in your question, you can see the values within dataGridView1
. 我假设在您执行问题中的代码后,您可以看到dataGridView1
的值。
The actual reading from the excel sheet is done when calling dataAdapter.Fill
. 调用dataAdapter.Fill
时,excel表中的实际读数完成。 So, in your case, reading the cells comes down to indexing columns and rows in dataSet.Tables[0]
. 因此,在您的情况下,读取单元格归结为dataSet.Tables[0]
索引列和行。
For example: 例如:
for (int row = 0; row < dataSet.Tables[0].Rows.Count; row++)
{
DataRow r = dataSet.Tables[0].Rows[row];
}
Accessing the cells in row r
is trivial (like the sample above, just for cell). 访问行r
的单元格是微不足道的(如上面的示例,仅适用于单元格)。
EDIT 编辑
I forgot to describe the "insert the values into a database" part. 我忘了描述“将值插入数据库”部分。 I presume that the database is SQL Server (may be Express edition, too). 我认为数据库是SQL Server(也可能是Express版)。
First: create a database connection. 第一:创建数据库连接。 Instead of manually composing the connection string, use the SqlConnectionStringBuilder
: 而不是手动编写连接字符串,使用SqlConnectionStringBuilder
:
SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
csb.DataSource = <your server instance, e.g. "localhost\sqlexpress">;
csb.InitialCatalog = <name of your database>;
csb.IntegratedSecurity = <true if you use integrated security, false otherwise>;
if (!csb.IntegratedSecurity)
{
csb.UserId = <User name>;
csb.Password = <Password>;
}
Then, create and open a new SqlConnection
with the connection string: 然后,使用连接字符串创建并打开一个新的SqlConnection
:
using (SqlConnection conn = new SqlConnection(csb.ConnectionString))
{
conn.Open();
Iterate over all the values you want to insert and execute a respective insert command: 迭代要插入的所有值并执行相应的insert命令:
for (...)
{
SqlCommand cmd = new SqlCommand("INSERT INTO ... VALUES (@param1, ..., @paramn)", conn);
cmd.Parameters.AddWithValue("@param1", value1);
...
cmd.Parameters.AddWithValue("@paramn", valuen);
cmd.ExecuteNonQuery();
}
This closes the connection, as the using
block ends: 这将关闭连接,因为using
块结束:
}
And there you go. 你去吧 Alternatively, you could use a data adapter with a special insert-command. 或者,您可以使用带有特殊insert-command的数据适配器。 Then, inserting the values would come down to a one-liner, however, your database table must have the same structure as the Excel-sheet (respectively: as the data table you obtained in the code you posted. 然后,插入值将归结为单行,但是,您的数据库表必须具有与Excel表相同的结构(分别为:您在所发布的代码中获得的数据表)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.