asp.net postedFile.SaveAs和SaveAs,解决了本地查看excel找不到System.IO.Path.GetFileName文件问题

注:

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();
        }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值