コード例 #1
0
    public void fileuploadMachine()
    {
        SqlConnection conn = new SqlConnection(constr);

        try
        {
            if (FileUpload6.HasFile)
            {
                FileUpload6.Dispose();
                string fileName = FileUpload5.ResolveClientUrl(FileUpload6.PostedFile.FileName);

                DataTable datatble = new DataTable();
                DataSet   ds       = new DataSet();

                string file       = FileUpload6.PostedFile.FileName;
                string serverpath = Server.MapPath("~/uploads/" + file);
                if (File.Exists(serverpath))
                {
                    File.Delete(serverpath);
                }
                FileUpload6.SaveAs(Server.MapPath("~/uploads/" + file));
                string filePath = Server.MapPath("~/uploads/" + file);

                bool   hasHeaders = false;
                string HDR        = hasHeaders ? "Yes" : "No";
                // string HDR = "Yes";
                string strConn;
                if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx")
                {
                    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
                }
                else
                {
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
                }
                OleDbConnection con = new OleDbConnection(strConn);
                con.Open();
                DataTable schemaTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                DataRow   schemaRow   = schemaTable.Rows[0];
                string    sheet       = schemaRow["TABLE_NAME"].ToString();
                if (!sheet.EndsWith("_"))
                {
                    string           query = "SELECT * FROM [" + sheet + "]";
                    OleDbDataAdapter data  = new OleDbDataAdapter(query, con);
                    data.Fill(datatble);
                    DataTable filteredRows = datatble.Rows.Cast <DataRow>()
                                             .Where(row => !row.ItemArray.All(field => field is System.DBNull))
                                             .CopyToDataTable();
                    ds.Tables.Add(filteredRows);
                }
                SqlDataAdapter daa1 = new SqlDataAdapter("Select * from Machine ", conn);
                DataSet        dss  = new DataSet();
                daa1.Fill(dss);

                if (dss.Tables[0].Rows.Count > 0)
                {
                    dss.Locale = System.Globalization.CultureInfo.InvariantCulture;
                    SqlCommand     cmd1 = new SqlCommand("Truncate table Machine", conn);
                    SqlDataAdapter daa3 = new SqlDataAdapter(cmd1);
                    conn.Open();
                    cmd1.ExecuteNonQuery();
                    conn.Close();
                }
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string cell    = Convert.ToString(ds.Tables[0].Rows[i]["Cell"].ToString());
                    string machine = Convert.ToString(ds.Tables[0].Rows[i]["Machine"].ToString());
                    ds.Locale = System.Globalization.CultureInfo.InvariantCulture;
                    SqlCommand     cmd = new SqlCommand("insert into Machine(Cell,Machine)values ('" + cell + "','" + machine + "')", conn);
                    SqlDataAdapter daa = new SqlDataAdapter(cmd);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                con.Close();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Machine File Uploaded Successfully !');", true);
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Invalid file !');", true);
        }
        finally
        {
            //conn.Close();
            FileUpload6.Dispose();
        }
    }