//注意数据库导出到excle中后 需要修改工作簿把默认Sheet1 Sheet2 Sheet3 删除然后把导入到excel中的表明改为Sheet1
然后执行 导入事件
//导入事件
if (FileUpload1.PostedFile.ContentLength > 0)
{
string fullpathname = FileUpload1.PostedFile.FileName;
exceltosqlserver(fullpathname);
Response.Write("<script>window.alert('上传成功并excel导入数据库成功')</script>");
}
protected void exceltosqlserver(string fullpathname)
{
string conn = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + fullpathname + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
OleDbConnection thisconnection = new OleDbConnection(conn);
thisconnection.Open();
string Sql = "select * from [Sheet1$]";
OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
DataSet ds = new DataSet();
mycommand.Fill(ds, "[Sheet1$]");
thisconnection.Close();
int count = ds.Tables["[Sheet1$]"].Rows.Count;
for (int i = 0; i < count; i++)
{
string EnterID, FitType, LevelType;
EnterID = ds.Tables["[Sheet1$]"].Rows[i]["RestNo"].ToString().Trim();
FitType = ds.Tables["[Sheet1$]"].Rows[i]["Fit"].ToString().Trim();
LevelType = ds.Tables["[Sheet1$]"].Rows[i]["RestTypeId"].ToString().Trim();
string excelsql = "insert into [tb_rest_info] (EnterID,FitType,LevelType,Style,DishSerie,TraitDish,LowestCousmer,RoomCount,ParkCount,SeatCount,BussiTime,BussiArea,OrderDays,CanOrderSeat) values ('" + EnterID + "','" + FitType + "','" + LevelType + "','" + Style + "','" + DishSerie + "','" + TraitDish + "','" + LowestCousmer + "','" + RoomCount + "','" + ParkCount + "','" + SeatCount + "','" + BussiTime + "','" + BussiArea + "','" + OrderDays + "','" + CanOrderSeat + "')";
插入基础类(按照个人基础类方法 本人是sqlhelper) SqlHelper.SqlExecNQ(excelsql);
}
}