复制代码 代码如下:
string strOdbcCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=D:/2010年图书销售情况.xls;Extended Properties=Excel 8.0";
复制代码 代码如下:
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();
}
复制代码 代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
string StyleSheet = "Sheet1";
LoadData(StyleSheet);
}
复制代码 代码如下:
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();
}
新闻热点
疑难解答
图片精选