asp.net Excel轉(zhuǎn)換為SQL Server的方法
更新時(shí)間:2009年06月05日 00:47:37 作者:
辦公軟件Excel是一種常用的電子表格軟件,在編程項(xiàng)目中有需要將Excel轉(zhuǎn)換為SQL Server數(shù)據(jù)庫(kù)的需求,本文對(duì)此進(jìn)行一些介紹并給出設(shè)計(jì)代碼。
1.功能分析
通過(guò)Microsoft.Jet.OLEDB.4.0方式可實(shí)現(xiàn)使用ADO.NET訪問(wèn)Excel的目的,如以下示例代碼為連接Excel數(shù)據(jù)的字符串:
string strOdbcCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=D:\2010年圖書(shū)銷(xiāo)售情況.xls;Extended Properties=Excel 8.0";
2.實(shí)施方法
程序開(kāi)發(fā)步驟:
(1)新建一個(gè)網(wǎng)站,命名為25,其主頁(yè)默認(rèn)為Default.aspx。
(2)Default.aspx頁(yè)面中添加一個(gè)Table表格,用來(lái)布局頁(yè)面,然后在該Table表格中添加一個(gè)iframe框架、兩個(gè)Button控件和一個(gè)GridView控件,其中,iframe框架用來(lái)顯示原始Excel數(shù)據(jù)表中的數(shù)據(jù);Button控件分別用來(lái)將指定Excel中的數(shù)據(jù)表導(dǎo)入到SQL Server數(shù)據(jù)庫(kù)中和將導(dǎo)入SQL Server數(shù)據(jù)庫(kù)中的Excel數(shù)據(jù)綁定到GridView控件上;GridView控件用來(lái)顯示導(dǎo)入SQL Server數(shù)據(jù)庫(kù)中的Excel數(shù)據(jù)。
(3)程序主要代碼如下。
Default.aspx頁(yè)面中,首先自定義一個(gè)LoadData方法,該方法為無(wú)返回值類(lèi)型方法,主要用來(lái)將Excel數(shù)據(jù)表中的數(shù)據(jù)導(dǎo)入到SQL Server數(shù)據(jù)庫(kù)中。LoadData方法實(shí)現(xiàn)代碼如下:
public void LoadData(string StyleSheet)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + Server.MapPath
("usersdb.xls") + ";Extended Properties=Excel 8.0";
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open(); //打開(kāi)數(shù)據(jù)鏈接,得到一個(gè)數(shù)據(jù)集
DataSet myDataSet = new DataSet(); //創(chuàng)建DataSet對(duì)象
string StrSql = "select * from [" + StyleSheet + "$]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
myCommand.Fill(myDataSet, "[" + StyleSheet + "$]");
myCommand.Dispose();
DataTable DT = myDataSet.Tables["[" + StyleSheet + "$]"];
myConn.Close();
myCommand.Dispose();
string strConn = "Data Source=(local);DataBase=Usersdb;Uid=sa;Pwd=";
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
for (int j = 0; j < DT.Rows.Count; j++)
{
string UserID = DT.Rows[j][0].ToString();
string EmailAddress = DT.Rows[j][1].ToString();
string FirstName = DT.Rows[j][2].ToString();
string LastName = DT.Rows[j][3].ToString();
string Address1 = DT.Rows[j][4].ToString();
string Address2 = DT.Rows[j][5].ToString();
string City = DT.Rows[j][6].ToString();
string strSql = "insert into Usersdb(EmailAddress,FirstName,
LastName,Address1,Address2,City) ";
strSql = strSql + "values('" + EmailAddress + "','" + FirstName + "',
'" + LastName + "','" + Address1 + "','" + Address2 + "','" + City + "')";
SqlCommand comm = new SqlCommand(strSql, conn);
comm.ExecuteNonQuery();
if (j == DT.Rows.Count - 1)
{
Label1.Visible = true;
}
else
{
Label1.Visible = false;
}
}
conn.Close();
}
單擊【Excel數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù)中】按鈕,定義一個(gè)string類(lèi)型的變量,用來(lái)為L(zhǎng)oadData傳入?yún)?shù),然后調(diào)用LoadData自定義方法將指定的Excel中的數(shù)據(jù)表導(dǎo)入到SQL Server數(shù)據(jù)庫(kù)中。【Excel數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù)中】按鈕的Click事件代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
string StyleSheet = "Sheet1";
LoadData(StyleSheet);
}
單擊【顯示導(dǎo)入SQL的Excel數(shù)據(jù)】按鈕,將導(dǎo)入SQL Server數(shù)據(jù)庫(kù)中的Excel數(shù)據(jù)綁定到GridView控件上,顯示在網(wǎng)頁(yè)中?!撅@示導(dǎo)入SQL的Excel數(shù)據(jù)】按鈕的Click事件代碼如下:
protected void Button2_Click(object sender, EventArgs e)
{
string strConn = "Data Source=(local);DataBase=Usersdb;Uid=sa;Pwd=";
string sqlstr="select * from Usersdb";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr,conn);
DataSet ds = new DataSet();
conn.Open();
myda.Fill(ds, "Usersdb");
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
說(shuō)明:程序中進(jìn)行與Excel和SQL Server數(shù)據(jù)庫(kù)相關(guān)的操作時(shí),首先需要分別添加System.Data.OleDb和System.Data.SqlClient命名空間。
3.補(bǔ)充說(shuō)明
除了可以將Excel中數(shù)據(jù)導(dǎo)入到SQL Server數(shù)據(jù)庫(kù)外,還可以將其轉(zhuǎn)換為.txt文本文件格式,或者導(dǎo)入到Access或Oracle等數(shù)據(jù)庫(kù)中。
通過(guò)Microsoft.Jet.OLEDB.4.0方式可實(shí)現(xiàn)使用ADO.NET訪問(wèn)Excel的目的,如以下示例代碼為連接Excel數(shù)據(jù)的字符串:
復(fù)制代碼 代碼如下:
string strOdbcCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=D:\2010年圖書(shū)銷(xiāo)售情況.xls;Extended Properties=Excel 8.0";
2.實(shí)施方法
程序開(kāi)發(fā)步驟:
(1)新建一個(gè)網(wǎng)站,命名為25,其主頁(yè)默認(rèn)為Default.aspx。
(2)Default.aspx頁(yè)面中添加一個(gè)Table表格,用來(lái)布局頁(yè)面,然后在該Table表格中添加一個(gè)iframe框架、兩個(gè)Button控件和一個(gè)GridView控件,其中,iframe框架用來(lái)顯示原始Excel數(shù)據(jù)表中的數(shù)據(jù);Button控件分別用來(lái)將指定Excel中的數(shù)據(jù)表導(dǎo)入到SQL Server數(shù)據(jù)庫(kù)中和將導(dǎo)入SQL Server數(shù)據(jù)庫(kù)中的Excel數(shù)據(jù)綁定到GridView控件上;GridView控件用來(lái)顯示導(dǎo)入SQL Server數(shù)據(jù)庫(kù)中的Excel數(shù)據(jù)。
(3)程序主要代碼如下。
Default.aspx頁(yè)面中,首先自定義一個(gè)LoadData方法,該方法為無(wú)返回值類(lèi)型方法,主要用來(lái)將Excel數(shù)據(jù)表中的數(shù)據(jù)導(dǎo)入到SQL Server數(shù)據(jù)庫(kù)中。LoadData方法實(shí)現(xiàn)代碼如下:
復(fù)制代碼 代碼如下:
public void LoadData(string StyleSheet)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + Server.MapPath
("usersdb.xls") + ";Extended Properties=Excel 8.0";
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open(); //打開(kāi)數(shù)據(jù)鏈接,得到一個(gè)數(shù)據(jù)集
DataSet myDataSet = new DataSet(); //創(chuàng)建DataSet對(duì)象
string StrSql = "select * from [" + StyleSheet + "$]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
myCommand.Fill(myDataSet, "[" + StyleSheet + "$]");
myCommand.Dispose();
DataTable DT = myDataSet.Tables["[" + StyleSheet + "$]"];
myConn.Close();
myCommand.Dispose();
string strConn = "Data Source=(local);DataBase=Usersdb;Uid=sa;Pwd=";
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
for (int j = 0; j < DT.Rows.Count; j++)
{
string UserID = DT.Rows[j][0].ToString();
string EmailAddress = DT.Rows[j][1].ToString();
string FirstName = DT.Rows[j][2].ToString();
string LastName = DT.Rows[j][3].ToString();
string Address1 = DT.Rows[j][4].ToString();
string Address2 = DT.Rows[j][5].ToString();
string City = DT.Rows[j][6].ToString();
string strSql = "insert into Usersdb(EmailAddress,FirstName,
LastName,Address1,Address2,City) ";
strSql = strSql + "values('" + EmailAddress + "','" + FirstName + "',
'" + LastName + "','" + Address1 + "','" + Address2 + "','" + City + "')";
SqlCommand comm = new SqlCommand(strSql, conn);
comm.ExecuteNonQuery();
if (j == DT.Rows.Count - 1)
{
Label1.Visible = true;
}
else
{
Label1.Visible = false;
}
}
conn.Close();
}
單擊【Excel數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù)中】按鈕,定義一個(gè)string類(lèi)型的變量,用來(lái)為L(zhǎng)oadData傳入?yún)?shù),然后調(diào)用LoadData自定義方法將指定的Excel中的數(shù)據(jù)表導(dǎo)入到SQL Server數(shù)據(jù)庫(kù)中。【Excel數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù)中】按鈕的Click事件代碼如下:
復(fù)制代碼 代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
string StyleSheet = "Sheet1";
LoadData(StyleSheet);
}
單擊【顯示導(dǎo)入SQL的Excel數(shù)據(jù)】按鈕,將導(dǎo)入SQL Server數(shù)據(jù)庫(kù)中的Excel數(shù)據(jù)綁定到GridView控件上,顯示在網(wǎng)頁(yè)中?!撅@示導(dǎo)入SQL的Excel數(shù)據(jù)】按鈕的Click事件代碼如下:
復(fù)制代碼 代碼如下:
protected void Button2_Click(object sender, EventArgs e)
{
string strConn = "Data Source=(local);DataBase=Usersdb;Uid=sa;Pwd=";
string sqlstr="select * from Usersdb";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr,conn);
DataSet ds = new DataSet();
conn.Open();
myda.Fill(ds, "Usersdb");
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
說(shuō)明:程序中進(jìn)行與Excel和SQL Server數(shù)據(jù)庫(kù)相關(guān)的操作時(shí),首先需要分別添加System.Data.OleDb和System.Data.SqlClient命名空間。
3.補(bǔ)充說(shuō)明
除了可以將Excel中數(shù)據(jù)導(dǎo)入到SQL Server數(shù)據(jù)庫(kù)外,還可以將其轉(zhuǎn)換為.txt文本文件格式,或者導(dǎo)入到Access或Oracle等數(shù)據(jù)庫(kù)中。
相關(guān)文章
VS2010/VS2013項(xiàng)目創(chuàng)建 ADO.NET連接mysql/sql server詳細(xì)步驟
這篇文章主要介紹了VS2010/VS2013項(xiàng)目創(chuàng)建,及ADO.NET連接mysql/sql server詳細(xì)步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
詳解c# .net core 下的網(wǎng)絡(luò)請(qǐng)求
本篇文章主要介紹了詳解c# .net core 下的網(wǎng)絡(luò)請(qǐng)求,大致介紹下在.net core 下如何進(jìn)行http請(qǐng)求,主要仍然是GET和POST方法,有興趣的可以了解下2017-05-05
IIS應(yīng)用池回收造成Application_Start中定時(shí)執(zhí)行程序停止的問(wèn)題的解決方法
最近在做一個(gè)項(xiàng)目,需要在程序中定時(shí)不斷的執(zhí)行某些操作,結(jié)果發(fā)現(xiàn)每天7,8點(diǎn)過(guò)后到第二天9點(diǎn),定時(shí)程序經(jīng)常都沒(méi)有在執(zhí)行,后來(lái)才知道由于IIS的應(yīng)用池回收導(dǎo)致Application停止。2010-03-03
在ASP.NET Core5.0中訪問(wèn)HttpContext的方法步驟
這篇文章主要介紹了在ASP.NET Core5.0中訪問(wèn)HttpContext的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
讀取純真IP數(shù)據(jù)庫(kù)的公用組件接口QQWry.NET
這是一個(gè)讀取純真IP數(shù)據(jù)庫(kù)的公用組件接口,我是通過(guò)luma的《純真IP數(shù)據(jù)庫(kù)格式詳解》了解了純真IP數(shù)據(jù)庫(kù)數(shù)據(jù)格式,并且基于網(wǎng)絡(luò)上的一個(gè)IPLocation.dll源碼的基礎(chǔ)改編而來(lái)2013-06-06
客戶(hù)端用JavaScript填充DropDownList控件 服務(wù)器端讀不到值
今天遇到一個(gè)奇怪的問(wèn)題,某一頁(yè)面需要使用三級(jí)級(jí)聯(lián)下拉列表框。為提高用戶(hù)體驗(yàn),采用jQuery的cascadingDropDown插件調(diào)用后臺(tái)Web Services來(lái)實(shí)現(xiàn)ajax填充。2010-09-09
.NET/C#如何判斷某個(gè)類(lèi)是否是泛型類(lèi)型或泛型接口的子類(lèi)型詳解
這篇文章主要給大家介紹了關(guān)于.NET/C#如何判斷某個(gè)類(lèi)是否是泛型類(lèi)型或泛型接口的子類(lèi)型的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-09-09

