public void ExportExcel()
        {
            if ((sesion = SessionDB.start(Request, Response, false, db)) == null)
            {
                return;
            }



            ProgressBarCalc progressbar = new ProgressBarCalc(sesion, "ReportedePago");

            progressbar.prepare();



            List <string> lPadre          = new List <string>();
            List <string> lPadreBloq      = new List <string>();
            List <string> lHijoBloq       = new List <string>();
            List <string> lPensionados    = new List <string>();
            List <string> lSubPadre       = new List <string>();
            List <string> lSubPadreBloq   = new List <string>();
            List <string> lSubHijoBloq    = new List <string>();
            List <string> lSubPensionados = new List <string>();

            try
            {
                System.Data.DataTable tbl = new System.Data.DataTable();
                tbl.Columns.Add("Campus", typeof(string));
                tbl.Columns.Add("Periodo", typeof(string));
                tbl.Columns.Add("ID", typeof(string));
                tbl.Columns.Add("Docente", typeof(string));
                tbl.Columns.Add("Parte del periodo", typeof(string));
                tbl.Columns.Add("Esquema", typeof(string));
                tbl.Columns.Add("Concepto", typeof(string));
                tbl.Columns.Add("Monto", typeof(string));
                tbl.Columns.Add("IVA", typeof(string));
                tbl.Columns.Add("IVA Ret", typeof(string));
                tbl.Columns.Add("ISR Ret", typeof(string));
                tbl.Columns.Add("Banco", typeof(string));
                tbl.Columns.Add("Fecha de pago", typeof(string));
                tbl.Columns.Add("F. Recibo", typeof(string));
                tbl.Columns.Add("F. Dispersión", typeof(string));
                tbl.Columns.Add("F. Depósito", typeof(string));
                tbl.Columns.Add("F. Solicitado", typeof(string));
                tbl.Columns.Add("Tipo de pago", typeof(string));
                tbl.Columns.Add("Centro de costos", typeof(string));
                tbl.Columns.Add("Usuario", typeof(string));
                tbl.Columns.Add("F.Modificación", typeof(string));


                List <string> filtros = new List <string>();

                ResultSet res = getRowsTable(Request, filtros, 1);

                int count = res.Count;

                progressbar.init(count + 1);//99

                int    row    = 2;
                string fil_ED = Request.Params["filtro_"];

                while (res.Next())
                {
                    if (res.Get("PADRE") == "0")
                    {
                        if (fil_ED != "PD")
                        {
                            getRowsTableExcel(tbl, res);
                            lPadre.Add("A" + row + ":U" + row);
                        }


                        var bloqueoContrato = res.GetBool("BLOQUEOCONTRATO");//BLOQUEOCONTRATO es 1(bloqueado)
                        if (((bloqueoContrato && string.IsNullOrWhiteSpace(res.Get("FECHADEENTREGA"))) || res.GetInt("BLOQUEOS") > 0) && fil_ED != "PD")
                        {
                            lPadreBloq.Add("G" + row + ":G" + row);
                        }

                        if (fil_ED != "PD")
                        {
                            row++;
                        }

                        TABLECONDICIONSQLD = " ID_ESTADODECUENTA = " + res.Get("ID_ESTADODECUENTA");
                        res2 = getRowsTable(Request, filtros, 2);

                        //Detalle Estado de Cuenta
                        if (fil_ED != "PE")
                        {
                            while (res2.Next())
                            {
                                getRowsTableExcelECDetalle(tbl, res2);


                                bloqueoContrato = res2.GetBool("BLOQUEOCONTRATO");//BLOQUEOCONTRATO es 1(bloqueado)
                                if ((bloqueoContrato && string.IsNullOrWhiteSpace(res2.Get("FECHADEENTREGA"))) || res2.GetInt("BLOQUEOS") > 0)
                                {
                                    lHijoBloq.Add("G" + row + ":G" + row);
                                }

                                row++;
                            }
                        }
                        // termina el listado de PADRE 0  y sus detalles


                        /** -  LA BÚSQUEDA DE PENSIONADOS - **/

                        ResultSet resP = this.getRowsTableP(sesion.db, res.Get("ID_ESTADODECUENTA"));
                        if (fil_ED != "PE")
                        {
                            while (resP.Next())
                            {
                                getRowsTableExcelECDetalle(tbl, resP);
                                lPensionados.Add("B" + row + ":U" + row);
                                row++;
                            }
                        }


                        /** - LA BÚSQUEDA DE SUBPADRES - **/
                        ResultSet resH = this.getRowsTableH(sesion.db, res.Get("ID_ESTADODECUENTA"));

                        if (fil_ED != "PE")
                        {
                            while (resH.Next())
                            {
                                getRowsTableExcel(tbl, resH);
                                lSubPadre.Add("A" + row + ":U" + row);

                                bloqueoContrato = resH.GetBool("BLOQUEOCONTRATO");//BLOQUEOCONTRATO es 1(bloqueado)
                                if ((bloqueoContrato && string.IsNullOrWhiteSpace(resH.Get("FECHADEENTREGA"))) || resH.GetInt("BLOQUEOS") > 0)
                                {
                                    lSubPadreBloq.Add("G" + row + ":G" + row);
                                }

                                row++;

                                /* Comienza la búsqueda de los SUBHIJOS de cada SUBPADRE */
                                /*********************************************************/
                                TABLECONDICIONSQLD = " ID_ESTADODECUENTA = " + resH.Get("ID_ESTADODECUENTA");
                                res3 = this.getRowsTable(Request, filtros, 2);
                                while (res3.Next())
                                {
                                    getRowsTableExcelECDetalle(tbl, res3);

                                    bloqueoContrato = res3.GetBool("BLOQUEOCONTRATO");//BLOQUEOCONTRATO es 1(bloqueado)
                                    if ((bloqueoContrato && string.IsNullOrWhiteSpace(res3.Get("FECHADEENTREGA"))) || res3.GetInt("BLOQUEOS") > 0)
                                    {
                                        lSubHijoBloq.Add("G" + row + ":G" + row);
                                    }

                                    row++;
                                }

                                /** - COMIENZA LA BÚSQUEDA DE SUB-PENSIONADOS - **/
                                ResultSet resP2 = this.getRowsTableSP(sesion.db, res.Get("ID_ESTADODECUENTA"), resH.Get("ID_ESTADODECUENTA"));
                                while (resP2.Next())
                                {
                                    getRowsTableExcelECDetalle(tbl, resP2);
                                    lSubPensionados.Add("B" + row + ":U" + row);
                                    row++;
                                }
                            }
                        }
                    }

                    progressbar.progress();
                }

                using (ExcelPackage pck = new ExcelPackage())
                {
                    //Create the worksheet
                    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Estado de Cuenta");

                    //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
                    ws.Cells["A1"].LoadFromDataTable(tbl, true);
                    ws.Cells["A1:U1"].AutoFitColumns();

                    //Format the header for column 1-3
                    using (ExcelRange rng = ws.Cells["A1:U1"])
                    {
                        rng.Style.Font.Bold        = true;
                        rng.Style.Fill.PatternType = ExcelFillStyle.Solid;                      //Set Pattern for the background to Solid
                        rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));  //Set color to dark blue
                        rng.Style.Font.Color.SetColor(Color.White);
                    }

                    foreach (var x in lPadre)
                    {
                        using (ExcelRange rngX = ws.Cells[x])
                        {
                            rngX.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            rngX.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(201, 223, 240));
                            rngX.Style.Font.Color.SetColor(Color.Black);
                        }
                    }

                    foreach (var x in lPadreBloq)
                    {
                        using (ExcelRange rngX = ws.Cells[x])
                        {
                            rngX.Style.Font.Strike      = true;
                            rngX.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            rngX.Style.Font.Color.SetColor(Color.Red);
                        }
                    }

                    foreach (var x in lPensionados)
                    {
                        using (ExcelRange rngX = ws.Cells[x])
                        {
                            rngX.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            rngX.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(240, 237, 184));
                            rngX.Style.Font.Color.SetColor(Color.Black);
                        }
                    }

                    foreach (var x in lHijoBloq)
                    {
                        using (ExcelRange rngX = ws.Cells[x])
                        {
                            rngX.Style.Font.Strike      = true;
                            rngX.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            rngX.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 255, 255));
                            rngX.Style.Font.Color.SetColor(Color.Red);
                        }
                    }

                    foreach (var x in lSubPadre)
                    {
                        using (ExcelRange rngX = ws.Cells[x])
                        {
                            rngX.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            rngX.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(226, 231, 235));
                            rngX.Style.Font.Color.SetColor(Color.Black);
                        }
                    }

                    foreach (var x in lSubPadreBloq)
                    {
                        using (ExcelRange rngX = ws.Cells[x])
                        {
                            rngX.Style.Font.Strike      = true;
                            rngX.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            rngX.Style.Font.Color.SetColor(Color.Red);
                        }
                    }

                    foreach (var x in lSubHijoBloq)
                    {
                        using (ExcelRange rngX = ws.Cells[x])
                        {
                            rngX.Style.Font.Strike      = true;
                            rngX.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            rngX.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 255, 255));
                            rngX.Style.Font.Color.SetColor(Color.Red);
                        }
                    }

                    foreach (var x in lSubPensionados)
                    {
                        using (ExcelRange rngX = ws.Cells[x])
                        {
                            rngX.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            rngX.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(240, 237, 184));
                            rngX.Style.Font.Color.SetColor(Color.Black);
                        }
                    }

                    //Example how to Format Column 1 as numeric
                    using (ExcelRange col = ws.Cells[2, 1, 2 + tbl.Rows.Count, 1])
                    {
                        col.Style.Numberformat.Format = "#,##0.00";
                        col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    }

                    //Write it back to the client
                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    Response.AddHeader("content-disposition", "attachment;  filename=EstadodeCuenta.xlsx");
                    Response.BinaryWrite(pck.GetAsByteArray());
                }

                Log.write(this, "Start", LOG.CONSULTA, "Exporta Excel Estado de Cuenta", sesion);
            }
            catch (Exception e)
            {
                ViewBag.Notification = Notification.Error(e.Message);
                Log.write(this, "Start", LOG.ERROR, "Exporta Excel Estado de Cuenta" + e.Message, sesion);
            }


            progressbar.complete();
        }
コード例 #2
0
        public bool ValidarExcel(string fileName, SessionDB sesion, System.Data.DataTable tbl, List <NominaExcelModel> listModels)
        {
            ProgressBarCalc progressbar = new ProgressBarCalc(sesion, "NominaExcel");

            progressbar.prepare();

            NominaExcelModel auxModel = new NominaExcelModel();

            auxModel.sesion = sesion;
            auxModel.clean();
            auxModel.cargaListas();

            tbl.Columns.Add("CVE_SEDE", typeof(string));
            tbl.Columns.Add("PARTEDELPERIODO", typeof(string));

            tbl.Columns.Add("PERIODO", typeof(string));
            tbl.Columns.Add("ID_CENTRODECOSTOS", typeof(string));
            tbl.Columns.Add("IDSIU", typeof(string));
            tbl.Columns.Add("ID_PERSONA", typeof(string));

            tbl.Columns.Add("TIPODEDOCENTE", typeof(string));
            tbl.Columns.Add("NOCURSOS", typeof(string));
            tbl.Columns.Add("HORASAPAGAR", typeof(string));

            tbl.Columns.Add("OPCIONDEPAGO", typeof(string));
            tbl.Columns.Add("CVE_TIPODEPAGO", typeof(string));
            tbl.Columns.Add("TABULADOR", typeof(string));
            tbl.Columns.Add("ID_ESQUEMA", typeof(string));
            tbl.Columns.Add("MONTOAPAGAR", typeof(string));

            tbl.Columns.Add("CVE_ESCUELA", typeof(string));
            tbl.Columns.Add("CAMPUS_INB", typeof(string));
            tbl.Columns.Add("MATERIA", typeof(string));
            tbl.Columns.Add("CURSO", typeof(string));
            tbl.Columns.Add("NOMBREMATERIA", typeof(string));

            int    paso    = 1;
            NOMINA current = NOMINA.CVE_SEDE;

            // Cargar el excel en los modelos.
            try
            {
                using (ExcelPackage xlPackage = new ExcelPackage(new FileInfo(fileName)))
                {
                    paso = 1;
                    // 1.- Get the first worksheet in the workbook
                    ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];

                    Dictionary <string, int>    col       = new Dictionary <string, int>();
                    Dictionary <NOMINA, object> dataValid = new Dictionary <NOMINA, object>();

                    int start = worksheet.Dimension.Start.Column;
                    int end   = worksheet.Dimension.End.Column;
                    int y     = worksheet.Dimension.Start.Row;
                    for (int x = start; x <= end; x++)
                    {
                        string head = worksheet.Cells[y, x].Text.ToUpper();
                        col.Add(head, x);
                    }
                    start = 1 + worksheet.Dimension.Start.Row;                      // se le suma 1 por las cabeceras
                    end   = worksheet.Dimension.End.Row;
                    progressbar.init(end - start + 2);
                    DateTime dt_1 = DateTime.Now;
                    for (int row = start; row <= end; row++)
                    {
                        // ------------------- Parche para excluir las lineas vacias -------------------
                        bool emptyLine = true;
                        for (int i = 1; i <= 14; i++)
                        {
                            if (string.IsNullOrWhiteSpace(worksheet.Cells[row, i].Text) == false)
                            {
                                emptyLine = false; break;
                            }
                        }
                        if (emptyLine)
                        {
                            continue;
                        }
                        // -----------------------------------------------------------------------------

                        NominaExcelModel model = new NominaExcelModel();
                        model.sesion = sesion;
                        paso         = 2;
                        // 2.- Se asignan los valores al modelo
                        current        = NOMINA.CVE_SEDE;
                        model.CVE_SEDE = worksheet.Cells[row, col["CVE_SEDE"]].Text;

                        current            = NOMINA.PARTEPERIODO;
                        model.PARTEPERIODO = worksheet.Cells[row, col["PARTEDELPERIODO"]].Text;//

                        current       = NOMINA.PERIODO;
                        model.PERIODO = worksheet.Cells[row, col["PERIODO"]].Text;

                        current = NOMINA.ID_CENTRODECOSTOS;
                        model.ID_CENTRODECOSTOS = worksheet.Cells[row, col["ID_CENTRODECOSTOS"]].Text;

                        current     = NOMINA.IDSIU;
                        model.IDSIU = worksheet.Cells[row, col["IDSIU"]].Text;

                        current = NOMINA.ID_PERSONA;
                        long.TryParse(worksheet.Cells[row, col["ID_PERSONA"]].Text, out model.ID_PERSONA);

                        current             = NOMINA.TIPODEDOCENTE;
                        model.TIPODEDOCENTE = worksheet.Cells[row, col["TIPODEDOCENTE"]].Text;

                        current = NOMINA.NOCURSOS;
                        int.TryParse(worksheet.Cells[row, col["NOCURSOS"]].Text, out model.NOCURSOS);

                        current = NOMINA.HORASAPAGAR;
                        double.TryParse(worksheet.Cells[row, col["HORASAPAGAR"]].Text, out model.HORASAPAGAR);

                        current            = NOMINA.OPCIONDEPAGO;
                        model.OPCIONDEPAGO = worksheet.Cells[row, col["OPCIONDEPAGO"]].Text;

                        current = NOMINA.CVE_TIPODEPAGO;
                        model.CVE_TIPODEPAGO = worksheet.Cells[row, col["CVE_TIPODEPAGO"]].Text;

                        current         = NOMINA.TABULADOR;
                        model.TABULADOR = worksheet.Cells[row, col["TABULADOR"]].Text;

                        current          = NOMINA.ID_ESQUEMA;
                        model.ID_ESQUEMA = worksheet.Cells[row, col["ID_ESQUEMA"]].Text;

                        current = NOMINA.MONTOAPAGAR;
                        double.TryParse(worksheet.Cells[row, col["MONTOAPAGAR"]].Text, out model.MONTOAPAGAR);

                        current           = NOMINA.CVE_ESCUELA;
                        model.CVE_ESCUELA = worksheet.Cells[row, col["CVE_ESCUELA"]].Text;

                        current          = NOMINA.CAMPUS_INB;
                        model.CAMPUS_INB = worksheet.Cells[row, col["CAMPUS_INB"]].Text;

                        current       = NOMINA.MATERIA;
                        model.MATERIA = worksheet.Cells[row, col["MATERIA"]].Text;

                        current     = NOMINA.CURSO;
                        model.CURSO = worksheet.Cells[row, col["CURSO"]].Text;

                        current             = NOMINA.NOMBREMATERIA;
                        model.NOMBREMATERIA = worksheet.Cells[row, col["NOMBREMATERIA"]].Text;


                        //carga esquema
                        auxModel.cargarEsquema(model.CVE_SEDE, model.PERIODO, model.ID_ESQUEMA);

                        paso = 3;
                        // 3.- Se validan
                        model.copiaListasDesde(auxModel);
                        model.validate();

                        // 4.- Se guarda en la tabla temporal.
                        model.Add_TMP();
                        listModels.Add(model);

                        // 5.- Se agregan los datos al datatable.
                        tbl.Rows.Add(model.getArrayObject(dataValid));

                        progressbar.progress();
                    }
                    DateTime dt_2 = DateTime.Now;
                    Debug.WriteLine("span:" + (dt_2 - dt_1));
                }                 // the using statement calls Dispose() which closes the package.

                sesion.vdata.Remove("NominaExcelError");
                sesion.saveSession();

                progressbar.complete();
                return(true);
            }
            catch (Exception)
            {
                if (paso == 1)
                {
                    sesion.vdata["NominaExcelError"] = "Error en archivo de Excel";
                }
                else if (paso == 2)
                {
                    sesion.vdata["NominaExcelError"] = "No se encuentra la columna '" + current + "'";
                }
                else if (paso == 3)
                {
                    sesion.vdata["NominaExcelError"] = "Error validando Excel";
                }
                sesion.saveSession();

                progressbar.complete();
                return(false);
            }
        }