给你个我写的仅供参考:
protected void Button1_Click(object sender, EventArgs e)
{
string fullFileName = this.FileUpload1.PostedFile.FileName;//获取本地机文件的路径名
string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);//获取文件名
string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);//获取文件名中的扩展名
if (type == "xls")//判断扩展名
{
this.FileUpload1.PostedFile.SaveAs(Server.MapPath("up") + "\\" + fileName);//记录文件名到服务器相对应的文加中
string myCount = this.FileUpload1.PostedFile.ContentLength.ToString();//计算上传文件的长度
this.Label2.Text = "所上传的字节数:" + myCount + "k";
this.Label3.Text = "您的本地文件路径为" + fullFileName;
}
else
{
Response.Write("<script language='javascript'>alert('格式有误!')</script>");//脚本对话框框
return;
}
string strpath = Server.MapPath("up") + "\\" + fileName;
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source='" + strpath + "';" +
"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataSet ds = new DataSet();
myCommand.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
int rows = ds.Tables[0].Rows.Count;
int cols = ds.Tables[0].Columns.Count;
tbAdapter = new TableCodeTableAdapter();
for (int i = 0; i < rows; i++)
{
str1=ds.Tables[0].Rows[i].ItemArray[0].ToString();
str2=ds.Tables[0].Rows[i].ItemArray[1].ToString();
tbAdapter.Insert(str1, str2);
}