在C#中如何实现用代码将excel导入到sql数据库中
4个回答
展开全部
/// <summary>
/// 从Excel读取数据
/// </summary>
/// <param name="filePath">路径</param>
/// <returns>DataSet</returns>
public DataSet ImportFromExcel(string filePath)
{
DataSet ds = new DataSet();
string connString = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
DataTable table = OleDbHelper.GetExcelTables(connString);
if(table == null || table.Rows.Count <= 0)
{
return null;
}
foreach(DataRow dr in table.Rows)
{
string cmdText = "select * from [" + dr["TABLE_NAME"].ToString() + "]";
DataTable dt = OleDbHelper.FillDataTable(connString, cmdText);
dt.TableName = dr["TABLE_NAME"].ToString();
ds.Tables.Add(dt);
}
return ds;
}
接下来只要把DataSet写入数据库
/// 从Excel读取数据
/// </summary>
/// <param name="filePath">路径</param>
/// <returns>DataSet</returns>
public DataSet ImportFromExcel(string filePath)
{
DataSet ds = new DataSet();
string connString = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
DataTable table = OleDbHelper.GetExcelTables(connString);
if(table == null || table.Rows.Count <= 0)
{
return null;
}
foreach(DataRow dr in table.Rows)
{
string cmdText = "select * from [" + dr["TABLE_NAME"].ToString() + "]";
DataTable dt = OleDbHelper.FillDataTable(connString, cmdText);
dt.TableName = dr["TABLE_NAME"].ToString();
ds.Tables.Add(dt);
}
return ds;
}
接下来只要把DataSet写入数据库
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
下面是一个我以前写的程序代码,自己看看吧。有什么不懂得就说。
using System.Data.SqlClient;
using System.Data.Common;//use for converting excel file to SQL table by OleDb
using System.Data.OleDb;
private void btnConvert_Click(object sender, EventArgs e)
{
//convert Excel to SQL using OLEDB
string path = txtExcel.Text;
string execelConnectionStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=book_edited2.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
using (OleDbConnection conn = new OleDbConnection(execelConnectionStr))
{
OleDbCommand cmd = new OleDbCommand("select Typlabel, Product,ProdIndex FROM [Sheet1$]", conn);
conn.Open();
using (DbDataReader dr = cmd.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=yy;Initial Catalog=wincorhighpot ;Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "ModelTable";
bulkCopy.WriteToServer(dr);
}
}
}
MessageBox.Show("Update ModelTable Completed.");
}
using System.Data.SqlClient;
using System.Data.Common;//use for converting excel file to SQL table by OleDb
using System.Data.OleDb;
private void btnConvert_Click(object sender, EventArgs e)
{
//convert Excel to SQL using OLEDB
string path = txtExcel.Text;
string execelConnectionStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=book_edited2.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
using (OleDbConnection conn = new OleDbConnection(execelConnectionStr))
{
OleDbCommand cmd = new OleDbCommand("select Typlabel, Product,ProdIndex FROM [Sheet1$]", conn);
conn.Open();
using (DbDataReader dr = cmd.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=yy;Initial Catalog=wincorhighpot ;Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "ModelTable";
bulkCopy.WriteToServer(dr);
}
}
}
MessageBox.Show("Update ModelTable Completed.");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
转换吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询