日期:2014-05-18  浏览次数:20839 次

各位看官,过往的神灵先停停吧,excel导入数据库!
C# code
if (FileUpload1.FileName == "")
        {
            Page.RegisterStartupScript("是","<script>alert('请选择文件');</script>");

            return null;

        }

        string fileExtenSion;

        fileExtenSion = Path.GetExtension(FileUpload1.FileName);

        if (fileExtenSion.ToLower() != ".xls" && fileExtenSion.ToLower() != ".xlsx")
        {

            Page.RegisterStartupScript("是", "<script>alert('上传文件格式不正确!');</script>");

            return null;

        }

        try
        {

            string FileName = "App_Data/" + Path.GetFileName(FileUpload1.FileName);

            if (File.Exists(Server.MapPath(FileName)))
            {

                File.Delete(Server.MapPath(FileName));

            }

            FileUpload1.SaveAs(Server.MapPath(FileName));


这段代码我断点了一下,在if (File.Exists(Server.MapPath(FileName)))
  {

  File.Delete(Server.MapPath(FileName));

  }
这个地方就没有执行直接执行下面的FileUpload1.SaveAs(Server.MapPath(FileName));在这一句直接报错跳出来了,请各位帮忙看看错误在哪

下面使整个导入数据区的代码
C# code

 private DataTable xsldata()
    {

        if (FileUpload1.FileName == "")
        {
            Page.RegisterStartupScript("是","<script>alert('请选择文件');</script>");

            return null;

        }

        string fileExtenSion;

        fileExtenSion = Path.GetExtension(FileUpload1.FileName);

        if (fileExtenSion.ToLower() != ".xls" && fileExtenSion.ToLower() != ".xlsx")
        {

            Page.RegisterStartupScript("是", "<script>alert('上传文件格式不正确!');</script>");

            return null;

        }

        try
        {

            string FileName = "App_Data/" + Path.GetFileName(FileUpload1.FileName);

            if (File.Exists(Server.MapPath(FileName)))
            {

                File.Delete(Server.MapPath(FileName));

            }

            FileUpload1.SaveAs(Server.MapPath(FileName));

            //HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES 

            string connstr2003 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";

            string connstr2007 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties=\"Excel 12.0;HDR=YES\"";

            OleDbConnection conn;

            if (fileExtenSion.ToLower() == ".xls")
            {

                conn = new OleDbConnection(connstr2003);

            }

            else
            {

                conn = new OleDbConnection(connstr2007);

            }

            conn.Open();

            string sql = "select * from [Sheet1$]";

            OleDbCommand cmd = new OleDbCommand(sql, conn);

            DataTable dt = new DataTable();

            OleDbDataReader sdr = cmd.ExecuteReader();



            dt.Load(sdr);

            sdr.Close();

            conn.Close();

            //删除服务器里上传的文件 

            if (File.Exists(Server.MapPath(FileName)))
            {

                File.Delete(Server.MapPath(FileName));

            }

            return dt;

        }

        catch (Exception e)
        {

            return null;

        }

    } 


    protected void Button1_Click(object sender, EventArgs e)
    {
       try{ 

 

                DataTable dt = xsldata(); 

 

                //dataGridView2.Da