C#导入excel2003数据表

protected void Button1_Click(object sender, EventArgs e)
    {
        excel = new ExcelImport();

        if (FileUpload1.HasFile)
        {
        string filename = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("//"));

        string filetype = filename.Substring(filename.LastIndexOf(".") + 1); //获取上传文件的类型
        string filename1 = filename.Substring(filename.LastIndexOf("//") + 1);
       
            if (filetype.Equals("xls"))
            {
                if (filename1.Equals("HZWSSZ_Import.xls"))
                {
                    FileUpload1.PostedFile.SaveAs(Server.MapPath("~//temp") + filename);//上传文件

                    string p_filepath = Server.MapPath("~//temp") + filename;
                    excel.ExcelToDate(p_filepath);

                    FileInfo file = new FileInfo(Server.MapPath("~//temp") + filename);
                    if (file.Exists)
                    {//删除文件
                        file.Delete();
                    }
                    Response.Write("");
                }
                else
                {
                    Response.Write("");
                }
            }
            else
            {
                Response.Write("");
            }
        }
        else
        {
            Response.Write("");
        }
       
    }

 

 

后台读取代码:

string mystring = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = '" + p_FilePath + "';Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
            OleDbConnection cnnxls = new OleDbConnection(mystring);
            OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls);
            DataSet myDs = new DataSet();
            myDa.Fill(myDs);

你可能感兴趣的:(c#,excel,c#,dataset,string,properties,button)