首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

未能找出路径“F:\批量导入\upfiles\20100825024205新建 Microsoft Excel 工作表.xls”的一部分

2012-12-29 
未能找到路径“F:\批量导入\upfiles\20100825024205新建 Microsoft Excel 工作表.xls”的一部分ASP.NET想把e

未能找到路径“F:\批量导入\upfiles\20100825024205新建 Microsoft Excel 工作表.xls”的一部分
ASP.NET想把excel导入到SQL数据库,在excel保存到服务器这一部出问题了,是在本机上测试的。后台代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    string strConn = "server=.;user id=sa;pwd=;DataBase=master"; //链接SQL数据库

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection(strConn);
        cn.Open();
        SqlDataAdapter sda = new SqlDataAdapter("select * from hhaspx_gz", cn);
        DataSet ds = new DataSet();
        sda.Fill(ds, "hhaspx_gz");
        this.GridView1.DataSource = ds.Tables["hhaspx_gz"];
        this.GridView1.DataKeyNames = new string[] { "hhaspx_id" };
        this.GridView1.DataBind();

    }
    /**/
    /// <summary>
    /// 查询EXCEL电子表格添加到DATASET
    /// </summary>
    /// <param name="filenameurl">服务器路径</param>
    /// <param name="table">表名</param>
    /// 
    /// <returns>更多内容请访问:www.hhaspx.com</returns>
    /// 红河ASP.NET(C#)学习网包含ASP.NET 教程,c#教程,Photoshop,ps教程,网页制作,图片素材,SEO优化

    public DataSet ExecleDs(string filenameurl, string table)
    {
        string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();
        DataSet ds = new DataSet();
        OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
        odda.Fill(ds, table);
        return ds;
    }
    protected void Button1_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")
        {
            Response.Write("<script>alert('只可以选择Excel文件')</script>");
            return;//当选择的不是Excel文件时,返回
        }
        SqlConnection cn = new SqlConnection(strConn);
        cn.Open();
        string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + FileUpload1.FileName;              //获取Execle文件名 DateTime日期函数
        string savePath = Server.MapPath("~\\upfiles\") + filename;//Server.MapPath 获得虚拟服务器相对路径
        FileUpload1.SaveAs(savePath);             //SaveAs 将上传的文件内容保存在服务器上
        DataSet ds = ExecleDs(savePath, filename);           //调用自定义方法
        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++)
            {
                string hhaspx_rq = dr[i]["日期"].ToString();//日期 excel列名【名称不能变,否则就会出错】
                string hhaspx_bh = dr[i]["编号"].ToString();//编号 列名 以下类似
                string hhaspx_xm = dr[i]["姓名"].ToString();
                string hhaspx_dx = dr[i]["底薪"].ToString();
                string hhaspx_kh = dr[i]["考核"].ToString();
                string hhaspx_jl = dr[i]["奖励"].ToString();
                string hhaspx_jt = dr[i]["津贴"].ToString();
                string hhaspx_jb = dr[i]["加班"].ToString();
                string hhaspx_zb = dr[i]["值班"].ToString();


                string hhaspx_jx = dr[i]["绩效"].ToString();
                string hhaspx_hj = dr[i]["合计"].ToString();
                string sqlcheck = "select count(*) from hhaspx_gz where hhaspx_rq='" + hhaspx_rq + "'And hhaspx_xm='" + hhaspx_xm + "'"; //检查用户是否存在
                SqlCommand sqlcmd = new SqlCommand(sqlcheck, cn);
                int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
                if (count < 1)
                {
                    string insertstr = "insert into hhaspx_gz (hhaspx_rq,hhaspx_bh,hhaspx_xm,hhaspx_dx,hhaspx_kh,hhaspx_jl,hhaspx_jt,hhaspx_jb,hhaspx_zb,hhaspx_jx,hhaspx_hj) values('" + hhaspx_rq + "','" + hhaspx_bh + "','" + hhaspx_xm + "','" + hhaspx_dx + "','" + hhaspx_kh + "','" + hhaspx_jl + "','" + hhaspx_jt + "','" + hhaspx_jb + "','" + hhaspx_zb + "','" + hhaspx_jx + "','" + hhaspx_hj + "')";

                    SqlCommand cmd = new SqlCommand(insertstr, cn);
                    try
                    {
                        cmd.ExecuteNonQuery();
                    }
                    catch (MembershipCreateUserException ex)       //捕捉异常
                    {
                        Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('内容重复!禁止导入');location='default.aspx'</script></script> ");
                    continue;


                }
            }
            Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>");
        }

        cn.Close();

    }
}


错误提示:

行 64:         string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + FileUpload1.FileName;              //获取Execle文件名 DateTime日期函数
行 65:         string savePath = Server.MapPath("~\\upfiles\") + filename;//Server.MapPath 获得虚拟服务器相对路径
行 66:         FileUpload1.SaveAs(savePath);             //SaveAs 将上传的文件内容保存在服务器上
行 67:         DataSet ds = ExecleDs(savePath, filename);           //调用自定义方法
行 68:         DataRow[] dr = ds.Tables[0].Select();            //定义一个DataRow数组
 

[解决办法]
看看NETWORK SERVICE(2003下)或者aspnet(xp下)有对F:\批量导入\upfiles\的写权限没?

在文件夹属性的安全页中。

热点排行