当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net Excel转换为SQL Server的方法

ASP.NET
asp.net Execl的添加,更新操作实现代码
asp.net 生成曲线图实现代码
从外部的js文件中获取ASPX页面的控件ClientID
asp.net 因为数据库正在使用的解决方法
asp.net Repeater 自递增
asp.net 实现防迅雷等下载工具盗链
ASP.NET封装的SQL数据库访问类
asp.net 通过指定IP地址得到当前的网络上的主机的域名
aspx 服务器架设问题解决
ASP.NET Session使用详解
Asp.net 5种页面转向方法
ASP.NET 用户多次登录的解决方法
ASP.NET下母版页和内容页中的事件发生顺序整理
asp.net 程序性能优化的七个方面 (c#(或vb.net)程序改进)
从客户端检测到有潜在危险的Request.Form值的asp.net代码
asp.net CommunityServer中的wwwStatus
在应用程序级别之外使用注册为allowDefinition=''MachineToApplication''的节是错误的
.net开发人员常犯的错误分析小结
ASP.net Substitution 页面缓存而部分不缓存的实现方法
asp.net 生成静态时的过滤viewstate的实现方法

ASP.NET 中的 asp.net Excel转换为SQL Server的方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 59 ::
收藏到网摘: n/a

办公软件Excel是一种常用的电子表格软件,在编程项目中有需要将Excel转换为SQL Server数据库的需求,本文对此进行一些介绍并给出设计代码。 1.功能分析
通过Microsoft.Jet.OLEDB.4.0方式可实现使用ADO.NET访问Excel的目的,如以下示例代码为连接Excel数据的字符串:
复制代码 代码如下:

string strOdbcCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=D:\2010年图书销售情况.xls;Extended Properties=Excel 8.0";

2.实施方法
程序开发步骤:
(1)新建一个网站,命名为25,其主页默认为Default.aspx。
(2)Default.aspx页面中添加一个Table表格,用来布局页面,然后在该Table表格中添加一个iframe框架、两个Button控件和一个GridView控件,其中,iframe框架用来显示原始Excel数据表中的数据;Button控件分别用来将指定Excel中的数据表导入到SQL Server数据库中和将导入SQL Server数据库中的Excel数据绑定到GridView控件上;GridView控件用来显示导入SQL Server数据库中的Excel数据。
(3)程序主要代码如下。
Default.aspx页面中,首先自定义一个LoadData方法,该方法为无返回值类型方法,主要用来将Excel数据表中的数据导入到SQL Server数据库中。LoadData方法实现代码如下:
复制代码 代码如下:

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(); //打开数据链接,得到一个数据集
DataSet myDataSet = new DataSet(); //创建DataSet对象
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数据写入数据库中】按钮,定义一个string类型的变量,用来为LoadData传入参数,然后调用LoadData自定义方法将指定的Excel中的数据表导入到SQL Server数据库中。【Excel数据写入数据库中】按钮的Click事件代码如下:
复制代码 代码如下:

protected void Button1_Click(object sender, EventArgs e)
{
string StyleSheet = "Sheet1";
LoadData(StyleSheet);
}

单击【显示导入SQL的Excel数据】按钮,将导入SQL Server数据库中的Excel数据绑定到GridView控件上,显示在网页中。【显示导入SQL的Excel数据】按钮的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();
}

说明:程序中进行与Excel和SQL Server数据库相关的操作时,首先需要分别添加System.Data.OleDb和System.Data.SqlClient命名空间。
3.补充说明
除了可以将Excel中数据导入到SQL Server数据库外,还可以将其转换为.txt文本文件格式,或者导入到Access或Oracle等数据库中。