Excel数据导入到sql数据库
public partial class FromExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button1_click(object sender, EventArgs e)
{
//创建sql连接
string conStr = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
//对sql数据源执行sql语句,将excel数据插入到该表中
string sql = "select * from Customers";
SqlCommand com = new SqlCommand(sql,conn);
SqlTransaction tran = conn.BeginTransaction();
com.Transaction = tran;
SqlDataAdapter da = new SqlDataAdapter(com);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
//从Excel中提取数据
DataTable tb = this.getExcelDate();
//逐行插入到sql数据库中
for (int i = 0; i < tb.Rows.Count; i++)
{
DataRow dr = ds.Tables[0].NewRow();
dr[0] = tb.Rows[i][0];
dr[1] = tb.Rows[i][1];
dr[2] = tb.Rows[i][2];
ds.Tables[0].Rows.Add(dr);
}
try
{
da.Update(ds);
tran.Commit();
}
catch
{
tran.Rollback();
}
conn.Dispose();
}
//读取excel数据存储到dataset的table里
public DataTable getExcelDate()
{
string strExcelFileName = "D:\\sheet.xls";
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";
string sql = "select * from [Data$]";
OleDbConnection xlsConn = new OleDbConnection(strcon);
OleDbCommand cmdOle = new OleDbCommand(sql, xlsConn);
xlsConn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(cmdOle);
DataSet ds = new DataSet();
da.Fill(ds);
xlsConn.Close();
xlsConn.Dispose();
return ds.Tables[0];
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button1_click(object sender, EventArgs e)
{
//创建sql连接
string conStr = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
//对sql数据源执行sql语句,将excel数据插入到该表中
string sql = "select * from Customers";
SqlCommand com = new SqlCommand(sql,conn);
SqlTransaction tran = conn.BeginTransaction();
com.Transaction = tran;
SqlDataAdapter da = new SqlDataAdapter(com);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
//从Excel中提取数据
DataTable tb = this.getExcelDate();
//逐行插入到sql数据库中
for (int i = 0; i < tb.Rows.Count; i++)
{
DataRow dr = ds.Tables[0].NewRow();
dr[0] = tb.Rows[i][0];
dr[1] = tb.Rows[i][1];
dr[2] = tb.Rows[i][2];
ds.Tables[0].Rows.Add(dr);
}
try
{
da.Update(ds);
tran.Commit();
}
catch
{
tran.Rollback();
}
conn.Dispose();
}
//读取excel数据存储到dataset的table里
public DataTable getExcelDate()
{
string strExcelFileName = "D:\\sheet.xls";
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";
string sql = "select * from [Data$]";
OleDbConnection xlsConn = new OleDbConnection(strcon);
OleDbCommand cmdOle = new OleDbCommand(sql, xlsConn);
xlsConn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(cmdOle);
DataSet ds = new DataSet();
da.Fill(ds);
xlsConn.Close();
xlsConn.Dispose();
return ds.Tables[0];
}
}
将Excel数据表看作是数据源,直接用OleDb进行读取,不同之处在于连接字符串“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=excelfilepath;Extended Properties='Excel 8.0;HDR=YES;IMEX=1';”
其中,Microsoft.Jet.OLEDB表示驱动。excelfilepath表示excel文件的物理路径,目前不能提供对excel 2007版本的文件读取。Extended Properties属性值中的HDR表示表中首行为标题行,若表中不包含标题,可将其设为no,IMEX=1表示将excel中混合型数据转换成文本型。
在excel的sql语句中的[Data$],Data表示excel文件中要读取的sheet的名称