using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds;
ds = GetOleData("D://excel.xls");
try
{
SqlConnection conn = new SqlConnection("Server=localhost;database=GestDB;uid=sa;pwd=;");
conn.Open();
SqlCommand comm = new SqlCommand();
for (int i = 0; i < ds.Tables["MyRecords"].Rows.Count; i++)
{
string[] strArray ={ ds.Tables["MyRecords"].Rows[i].ItemArray[0].ToString(), ds.Tables["MyRecords"].Rows[i].ItemArray[1].ToString(), ds.Tables["MyRecords"].Rows[i].ItemArray[2].ToString() };
string sqlstr = "insert into test values('" + strArray[0].ToString() + "','" + strArray[1].ToString() + "','" + strArray[2].ToString() + "')";
comm.CommandText = sqlstr;
comm.Connection = conn;
comm.ExecuteNonQuery();
}
conn.Close();
}
catch (Exception ee)
{
throw ee;
}
}
private DataSet GetOleData(string FileAddr)
{
OleDbConnection objConn = null;
DataSet ds = new DataSet();
string strConn = "Provider=Microsoft.Jet.OleDB.4.0;" + "Data Source=D://excel.xls;Extended Properties=Excel 8.0;";
objConn = new OleDbConnection(strConn);
objConn.Open();
string strSql = "select * from [Sheet1$A1:C5]";
OleDbCommand objCmd = new OleDbCommand(strSql, objConn);
OleDbDataAdapter sqlada = new OleDbDataAdapter();
sqlada.SelectCommand = objCmd;
sqlada.Fill(ds, "MyRecords");
objConn.Close();
return ds;
}
}