0

hago la carga de un archivo excel, y tengo dos textbox donde quiero mostrar el numero total de regitros del archivo y en el otro quiero que me vaya indicando la cantidad de registros se han insertado en la BD, actualmente lo hace pero al final del proceso, ¿Como puedo hacer para que me muestre estos datos cuando se esta procesando el archivo?

 private void CargaArchivos()
        {
            string Provider = "Microsoft.ACE.OLEDB.12.0";
            try
            {
                if (fuExcel.PostedFile.FileName.Length > 0)
                {

                    String ConnExcel = "";
                    string HojaExcel = "";
                    string pathOriginal = Server.MapPath("Files/" + fuExcel.PostedFile.FileName);
                    if (File.Exists(pathOriginal))
                        File.Delete(pathOriginal);
                    fuExcel.SaveAs(pathOriginal);

                    string nombreArchivo = fuExcel.PostedFile.FileName.ToString();

                    if (fuExcel.PostedFile.FileName.Contains("xlsx"))
                        ConnExcel = "Provider=" + Provider + "; Data Source = " + pathOriginal + "; Extended Properties = \"Excel 12.0;HDR=YES;READONLY=FALSE\";";
                    else
                        ConnExcel = "Provider=" + Provider + "; Data Source = " + pathOriginal + "; Extended Properties = \"Excel 8.0;HDR=YES;READONLY=FALSE\";";

                    OleDbConnection ConnLocal = new OleDbConnection(ConnExcel);
                    ConnLocal.Open();
                    OleDbCommand cmdLocal = new OleDbCommand("", ConnLocal);
                    System.Data.DataTable dtExcelSheetName = ConnLocal.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    foreach (DataRow drtabla in dtExcelSheetName.Rows)
                        if (drtabla["Table_Name"].ToString().ToUpper().Contains("LAYOUT") || drtabla["Table_Name"].ToString().ToUpper().Contains("SHEET1") || drtabla["Table_Name"].ToString().ToUpper().Contains("TABLE1"))
                        {
                            HojaExcel = drtabla["Table_Name"].ToString();
                            break;
                        }
                    if (HojaExcel == "")
                        HojaExcel = "Hoja1$";

                    OleDbDataAdapter dta = new OleDbDataAdapter("Select * from [" + HojaExcel + "]", ConnLocal);

                    DataTable dtExcel = new System.Data.DataTable();
                    dta.Fill(dtExcel);


                    Session["Total"] = dtExcel.Rows.Count;
                    

                    int filas = 1;
                    int c = 0;  

                    System.Data.DataTable dtSalida = new System.Data.DataTable();
                    dtSalida.Columns.Add("A", typeof(DateTime));
                    dtSalida.Columns.Add("B", typeof(string));
                    dtSalida.Columns.Add("C", typeof(int));
                    dtSalida.Columns.Add("D", typeof(int));
                    dtSalida.Columns.Add("E", typeof(string));
                    dtSalida.Columns.Add("F", typeof(DateTime));
                    dtSalida.Columns.Add("G", typeof(int));
                    dtSalida.Columns.Add("H", typeof(string));
                    dtSalida.Columns.Add("I", typeof(string)).AllowDBNull = true;
                    dtSalida.Columns.Add("J", typeof(float)).AllowDBNull = true;
                    dtSalida.Columns.Add("K", typeof(string)).AllowDBNull = true;
                    dtSalida.Columns.Add("L", typeof(int)).AllowDBNull = true;
                    dtSalida.Columns.Add("M", typeof(int)).AllowDBNull = true;
                    dtSalida.Columns.Add("N", typeof(float)).AllowDBNull = true;
                    dtSalida.Columns.Add("O", typeof(string)).AllowDBNull = true;
                    dtSalida.Columns.Add("P", typeof(int)).AllowDBNull = true;
                    dtSalida.Columns.Add("Q", typeof(string)).AllowDBNull = true;
                    dtSalida.Columns.Add("R", typeof(DateTime)).AllowDBNull = true;
                    dtSalida.Columns.Add("S", typeof(int));
                    dtSalida.Columns.Add("T", typeof(float)).AllowDBNull = true;
                    dtSalida.Columns.Add("U", typeof(float)).AllowDBNull = true;
                    dtSalida.Columns.Add("V", typeof(string)).AllowDBNull = true;

                    Session["Total"] = dtExcel.Rows.Count;
                    txtTotal.Text = Session["Total"].ToString();

                    float res_dou;
                    int i = 0;

                    foreach (DataRow dr in dtExcel.Rows)
                    {

                        try
                        {
                            DataRow row2 = dtSalida.NewRow();
                            if (float.TryParse(dr["IMPORT_MCMEP"].ToString(), out res_dou) == false)
                            {
                                throw new Exception("Error al formatear");
                            }
                            row2["A"] = Convert.ToDateTime(Convert.ToDateTime(dr["B"].ToString().Replace("a", "").Replace("m", "").Replace("p", "").Replace(".", "")).ToString("dd/MM/yyyy"));
                            row2["B"] = dr["B"].ToString().Replace(":", "");
                            row2["C"] = dr["C"].ToString();
                            row2["D"] = dr["D"].ToString();
                            row2["E"] = dr["E"].ToString();
                            row2["F"] = Convert.ToDateTime(Convert.ToDateTime(dr["F"].ToString().Replace("a", "").Replace("m", "").Replace("p", "").Replace(".", "")).ToString("dd/MM/yyyy"));
                            row2["G"] = dr["G"].ToString();
                            row2["H"] = dr["H"].ToString();
                            row2["I"] = dr["I"].ToString();
                            row2["J"] = DBNull.Value;
                            row2["K"] = dr["K"].ToString();
                            row2["L"] = DBNull.Value;
                            row2["M"] = DBNull.Value;
                            row2["N"] = DBNull.Value;
                            row2["O"] = DBNull.Value;
                            row2["P"] = Convert.ToInt32(dr["P"]);
                            row2["Q"] = DBNull.Value;
                            row2["R"] = Convert.ToDateTime(dr["R"].ToString().Replace("a", "").Replace("m", "").Replace("p", "").Replace(".", "")).ToString("dd/MM/yyyy HH:mm:ss");
                            row2["S"] = dr["S"].ToString();
                            row2["T"] = DBNull.Value;
                            row2["U"] = res_dou;
                            row2["V"] = nombreArchivo;
                            dtSalida.Rows.Add(row2);

                            i++;

                            Session["Procesado"] = i;
                            txtProcesado.Text = Session["Procesado"].ToString();

                            Console.Write(i);
                        }
                        catch (Exception ex)
                        {
                            lstMensaje.Items.Add("Error Fila: "+i+1+" -> "+ex.Message);
                        }

5
  • Eso es winforms o asp? porque son dos cosas totlamente distintas y la forma de resolverlo tambien.
    – gbianchi
    el 6 feb. 2019 a las 18:35
  • @gbianchi son web forms aspx
    – Ivxn
    el 6 feb. 2019 a las 18:45
  • lo que planteas no se puede realizar en un ambiente web, porque no hay notificacion al browser hasta que no termine el request, salvo que quieras implementar uan libreria de websocket como ser SignalR, pero lo para esto que planteas no es recomendable, porque no le muestras un icono generalmente gif indicando que estas trabajando en el servidor, pero progreso real no vas poder visualizar el 6 feb. 2019 a las 19:00
  • @LeandroTuttini a menos que lo haga por separado. Es decir primero cuenta sus rows en su dt y luego crea el archivo con un método async entonces se produciría la sensación de que primero indica la cantidad de filas y luego cuando el servidor termine de crear el archivo se lo entrega al browser.
    – Fran Islas
    el 6 feb. 2019 a las 19:12
  • @FranIslas el tema es que la cuenta la realiza del lado del servidor ya que es alli donde tiene el archivo, salvo que pueda procesarlo con alguna libreria javascript en el browser antes del submit, pero no lo recomoendo por temas de seguridad. Igual tampoco tendria un avance de las rows procesadas el 6 feb. 2019 a las 19:18

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.