FileHelpers.ExcelNPOIStorage.ExcelNPOIStorage.ExtractRecords C# (CSharp) Method

ExtractRecords() public method

Returns the records extracted from Excel file.
public ExtractRecords ( ) : object[]
return object[]
        public override object[] ExtractRecords()
        {
            if (String.IsNullOrEmpty(FileName))
                throw new ExcelBadUsageException("You need to specify the WorkBookFile of the ExcelDataLink.");

            var res = new ArrayList();

            CultureInfo oldCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            try {
                int cRow = StartRow;

                int recordNumber = 0;
                OnProgress(new ProgressEventArgs(recordNumber, -1));

                var colValues = new object[RecordFieldCount];

                InitExcel();
                OpenWorkbook(FileName);

                while (ShouldStopOnRow(cRow) == false) {
                    try {
                        if (ShouldReadRowData(cRow)) {
                            recordNumber++;
                            OnProgress(new ProgressEventArgs(recordNumber, -1));

                            colValues = RowValues(cRow, StartColumn, RecordFieldCount);

                            object record = ValuesToRecord(colValues);
                            res.Add(record);
                        }
                    }
                    catch (Exception ex) {
                        switch (mErrorManager.ErrorMode) {
                            case ErrorMode.ThrowException:
                                throw;
                            case ErrorMode.IgnoreAndContinue:
                                break;
                            case ErrorMode.SaveAndContinue:
                                AddError(cRow, ex, ColumnsToValues(colValues));
                                break;
                        }
                    }
                    finally {
                        cRow++;
                    }
                }
            }
            catch {
                throw;
            }
            finally {
                CloseAndCleanUp();
                Thread.CurrentThread.CurrentCulture = oldCulture;
            }

            return (object[]) res.ToArray(RecordType);
        }

Usage Example

        private void LeerExcel()
        {
            try
            {
                ProductosExcel[] resCab;
                if (FormatoCSVRadioButton.Checked)
                {
                    FileStorage provider = new FileStorage(typeof(ProductosExcel), Server.MapPath("TempExcel\\" + ((Entidades.Sesion)Session["Sesion"]).Cuit.Nro + "-" + Session.SessionID + "-Precios.csv"));
                    //ExcelNPOIStorage provider = new ExcelNPOIStorage(typeof(ProductosExcel));
                    //provider..StartRow = 0;
                    //provider.StartColumn = 0;
                    resCab = (ProductosExcel[])provider.ExtractRecords();
                }
                else
                {
                    ExcelNPOIStorage provider = new ExcelNPOIStorage(typeof(ProductosExcel));
                    provider.StartRow = 0;
                    provider.StartColumn = 0;
                    provider.FileName = Server.MapPath("TempExcel\\" + ((Entidades.Sesion)Session["Sesion"]).Cuit.Nro + "-" + Session.SessionID + "-Precios.xls");
                    resCab = (ProductosExcel[])provider.ExtractRecords();
                }

                List<Entidades.ListaPrecio> listasPrecio = new List<Entidades.ListaPrecio>();
                Entidades.Sesion sesion = (Entidades.Sesion)Session["Sesion"];
                
                //if (resCab.Length > 1)
                //{
                //    MensajeLabel.Text = "Proceso cancelado: La cabecera del archivo excel debe tener un solo renglón.";
                //    return;
                //}
                if (resCab[0].Lista01 == null || resCab[0].Lista01.Trim().Equals(string.Empty))
                {
                    MensajeLabel.Text = "Proceso cancelado: No está informada la primer lista de precios en la planilla excel.";
                    return;
                }
                else
                {
                    try
                    {
                        listasPrecio.Add(new Entidades.ListaPrecio(resCab[0].Lista01.Trim()));
                        if (!resCab[0].Lista02.Trim().Equals(String.Empty)) { listasPrecio.Add(new Entidades.ListaPrecio(resCab[0].Lista02.Trim())); }
                        if (!resCab[0].Lista03.Trim().Equals(String.Empty)) { listasPrecio.Add(new Entidades.ListaPrecio(resCab[0].Lista03.Trim())); }
                        if (!resCab[0].Lista04.Trim().Equals(String.Empty)) { listasPrecio.Add(new Entidades.ListaPrecio(resCab[0].Lista04.Trim())); }
                        if (!resCab[0].Lista05.Trim().Equals(String.Empty)) { listasPrecio.Add(new Entidades.ListaPrecio(resCab[0].Lista05.Trim())); }
                        if (!resCab[0].Lista06.Trim().Equals(String.Empty)) { listasPrecio.Add(new Entidades.ListaPrecio(resCab[0].Lista06.Trim())); }
                        if (!resCab[0].Lista07.Trim().Equals(String.Empty)) { listasPrecio.Add(new Entidades.ListaPrecio(resCab[0].Lista07.Trim())); }
                        if (!resCab[0].Lista08.Trim().Equals(String.Empty)) { listasPrecio.Add(new Entidades.ListaPrecio(resCab[0].Lista08.Trim())); }
                        if (!resCab[0].Lista09.Trim().Equals(String.Empty)) { listasPrecio.Add(new Entidades.ListaPrecio(resCab[0].Lista09.Trim())); }
                        if (!resCab[0].Lista10.Trim().Equals(String.Empty)) { listasPrecio.Add(new Entidades.ListaPrecio(resCab[0].Lista10.Trim())); }
                    }
                    catch
                    {
                    }
                }

                if (listasPrecio.Count == 0)
                {
                    MensajeLabel.Text = "Proceso cancelado: No hay ninguna Lista de precios definida.";
                    return;
                }

                ViewState["ListasPrecio"] = listasPrecio;
                
                //Leer detalle del excel
                //ExcelNPOIStorage provider = new ExcelNPOIStorage(typeof(ProductosExcel));
                //provider.StartRow = 3;
                //provider.StartColumn = 1;
                //provider.FileName = Server.MapPath("Temp\\ExcelAProcesar.xlsx");
                //ProductosExcel[] resDet = (ProductosExcel[])provider.ExtractRecords();
                
                //Completar la Matriz de Precios
                CrearYCompletarMatrizDePrecios(listasPrecio, resCab);

                //Actualizar los precios de la MatrizDePrecios
                DataTable dt = (DataTable)ViewState["MatrizDePrecios"];
                List<Entidades.ListaPrecio> listasPrecioNew = (List<Entidades.ListaPrecio>)ViewState["ListasPrecio"];
                RN.Precio.ImpactarMatriz(listasPrecioNew, dt, sesion);
                MensajeLabel.Enabled = true;
                MensajeLabel.Text = "PROCESO CONCLUIDO SATISFACTORIAMENTE.\n";
                string listasImp = "Listas importadas: ";
                foreach (Entidades.ListaPrecio lp in listasPrecio)
                {
                    if (listasImp != "Listas importadas: ")
                    {
                        listasImp += ", ";
                    }
                    listasImp += lp.Id;
                }
                MensajeLabel.Text += listasImp + "\n";
                MensajeLabel.Text += "Cantidad total de artículos: " + dt.Rows.Count;
                MensajeLabel.Enabled = false;
            }
            catch (Exception ex)
            {
                MensajeLabel.Text = EX.Funciones.Detalle(ex);
            }

        }
All Usage Examples Of FileHelpers.ExcelNPOIStorage.ExcelNPOIStorage::ExtractRecords