????????μ????????Excel????е????????????????
?????????????????????Excel?е?????浽??????У????????????????????????????????????Excel??????о?????£???????????????????д??????????????д???????Excel?????????????Excel???????????????????Excel????????????????????????Excel??
???????濪??????????Excel?????????
????????????Excel?????????

??????????????????????Excel????????????????????
protected void btnImport_Click(object sender?? EventArgs e)
{
if (FileUpload1.HasFile == false)//HasFile???????FileUpload???????????
{
Response.Write("<script>alert('???????Excel???')</script> ");
return;//??????????????
}
string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension?????????????
if (IsXls != ".xls")
{
if(IsXls!=".xlsx")
{
Response.Write("<script>alert('????????Excel???')</script>");
return;//?????????Excel??????????
}
}
string filename = FileUpload1.FileName; //???Execle????? DateTime???????
string savePath = Server.MapPath(("upfiles\") + filename);//Server.MapPath ???????????????·??
FileUpload1.SaveAs(savePath); //SaveAs ??????????????????????????
DataSet ds = ExcelSqlConnection(savePath?? filename??IsXls); //????????巽??
DataRow[] dr = ds.Tables[0].Select(); //???????DataRow????
int rowsnum = ds.Tables[0].Rows.Count;
if (rowsnum == 0)
{
Response.Write("<script>alert('Excel?????????????!')</script>"); //??Excel????????????????????
}
else
{
for (int i = 0; i < dr.Length; i++)
{
//???????????????????“upfiles”???????????????????ù???????????????????????Excel?????????????Щ??????????????????????
string title = dr[i]["????"].ToString();
string linkurl = dr[i]["??????"].ToString();
string categoryname = dr[i]["????"].ToString();
//Response.Write("<script>alert('????????:" + ex.Message + "')</script>");
}
Response.Write("<script>alert('Excle???????!');</script>");
}
}
#region ????Excel ???Excel???? ??????DataSet???????
/// <summary>
/// ????Excel ???Excel???? ??????DataSet???????
/// </summary>
/// <param name="filepath">Excel??????·??</param>
/// <param name="tableName">Excel??????</param>
/// <returns></returns>
public static System.Data.DataSet ExcelSqlConnection(string filepath?? string tableName??string IsXls)
{
string strCon = "";
if(IsXls==".xls")
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
}
else
{
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
}
OleDbConnection ExcelConn = new OleDbConnection(strCon);
try
{
string strCom = string.Format("SELECT * FROM [Sheet1$]");
ExcelConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom?? ExcelConn);
DataSet ds = new DataSet();
myCommand.Fill(ds?? "[" + tableName + "$]");
ExcelConn.Close();
return ds;
}
catch
{
ExcelConn.Close();
return null;
}
}
#endregio
??????δ????????????????????????????????xls???????????xlsx??????????????????????????
????xls?????
????strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + serverFilename + "';Extended Properties='Excel 12.0;HDR=YES'";
????xlsx?????
????strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + serverFilename + "';Extended Properties='Excel 8.0;HDR=YES;'";
???????????????????????????????????????????