注:
fileupload.SaveAs("完整路径带文件名")
fileupload.postedFile.SaveAs("文件名")
Server.MapPath获得服务器的路径
本地浏览excel总是失败,找了半天原因,其实是一个文件夹没有共享出来,权限是EveryOne(可读写最好完全控制)
//DropDownList2是上传的excel内的sheet(表名称)
//fileupload是input控件
//Label1用来存储文件名
//不同主机上浏览IPDropDownList是可选IP地址
public void Excel2007_Click(object sender, EventArgs e)
{
if (this.fileupload.Value == "" && this.Label1.Text == "" && DropDownList2.SelectedValue == "")
{
Response.Write("<script>window.alert('请选择要导入的文件')</script>");
}
if (this.fileupload.Value != "" && this.DropDownList2.SelectedValue == "")
{
string tempsr = "";
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile postedFile = files[0];
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
tempsr = "\\\\" + IPDropDownList.SelectedValue + "\\文件\\" + fileName;
postedFile.SaveAs(tempsr);
}
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + tempsr + ";Extended Properties=Excel 8.0;";//试用Excel03-07版本
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable sheetNames = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow dr in sheetNames.Rows)
{
DropDownList2.Items.Add(dr[2].ToString());
}
this.Label1.Text = tempsr;
conn.Close();
}