private string obtenerDatos(string sDesde, string sHasta, string sNivelEstructura,
                                string sCategoria, string sCualidad, string sClientes,
                                string sResponsables, string sNaturalezas, string sHorizontal, string sModeloContrato,
                                string sContrato, string sIDEstructura, string sSectores, string sSegmentos,
                                string sComparacionLogica,
                                string sCNP, string sCSN1P, string sCSN2P, string sCSN3P, string sCSN4P, string sPSN)
    {
        int           nWidthTabla     = 470; //370+100
        int           nColumnasACrear = 0;
        StringBuilder sb = new StringBuilder();
        StringBuilder sbColgroupTitulo = new StringBuilder();
        StringBuilder sbTitulo         = new StringBuilder();
        StringBuilder sbResultado      = new StringBuilder();
        string        sTablaTitulo     = "";
        string        sTablaContenido  = "";
        string        sTablaResultado  = "";
        decimal       nRatio           = 0;

        string sColor = "";

        try
        {
            sbColgroupTitulo.Append("<colgroup>");
            sbColgroupTitulo.Append("<col style='width:350px;' />");
            sbColgroupTitulo.Append("<col style='width:120px;' />");

            DataSet ds = ConsultasPGE.ObtenerDatosFichaEconomica((int)Session["UsuarioActual"],
                                                                 int.Parse(sDesde), int.Parse(sHasta),
                                                                 (sNivelEstructura == "0")? null:(int?)int.Parse(sNivelEstructura),
                                                                 sCategoria,
                                                                 sCualidad,
                                                                 sClientes,
                                                                 sResponsables,
                                                                 sNaturalezas,
                                                                 sHorizontal,
                                                                 sModeloContrato,
                                                                 sContrato,
                                                                 sIDEstructura,
                                                                 sSectores,
                                                                 sSegmentos,
                                                                 (sComparacionLogica == "1")? true:false,
                                                                 sCNP,
                                                                 sCSN1P,
                                                                 sCSN2P,
                                                                 sCSN3P,
                                                                 sCSN4P,
                                                                 sPSN,
                                                                 Session["MONEDA_VDC"].ToString()
                                                                 );

            bool bTitulos = false;
            int  i        = 0;
            foreach (DataRow oFila in ds.Tables[0].Rows)
            {
                if (!bTitulos)
                {
                    sbTitulo.Append("<tr class='TBLINI'>");
                    sbTitulo.Append("<td></td>");
                    for (int x = 4; x < ds.Tables[0].Columns.Count; x++)
                    {
                        if (x == 4)
                        {
                            string[] aNomMeses = Regex.Split(ds.Tables[0].Columns[x].ColumnName, "-");
                            sbTitulo.Append("<td>");
                            sbTitulo.Append(Fechas.AnnomesAFechaDescCorta(int.Parse(aNomMeses[0].ToString())));
                            sbTitulo.Append(" - ");
                            sbTitulo.Append(Fechas.AnnomesAFechaDescCorta(int.Parse(aNomMeses[1].ToString())));
                            sbTitulo.Append("</td>");
                        }
                        else
                        {
                            sbColgroupTitulo.Append("<col style='width:100px;' />");
                            sbTitulo.Append("<td>" + Fechas.AnnomesAFechaDescLarga(int.Parse(ds.Tables[0].Columns[x].ColumnName)) + "</td>");
                            nColumnasACrear++;
                        }
                    }
                    sbTitulo.Append("</tr>");
                    bTitulos = true;
                }

                if (oFila["t454_idformula"].ToString() != "3" &&
                    oFila["t454_idformula"].ToString() != "1" &&
                    oFila["t454_idformula"].ToString() != "2" &&
                    oFila["t454_idformula"].ToString() != "8"
                    )
                {
                    switch (oFila["nivel"].ToString())
                    {
                    case "1":
                        sb.Append("<tr id='" + oFila["t454_idformula"].ToString() + "' ");
                        //sb.Append(" style='display:block; height: 20px' nivel='1' desplegado='1'>");
                        sb.Append(" style='display:table-row; height: 20px' nivel='1' desplegado='1'>");
                        if (oFila["t454_idformula"].ToString() != "5")
                        {
                            sb.Append("<td style='text-align:left;'><IMG class=NSEG1 onclick=mostrar(this) src='../../../../images/plus.gif' style='cursor:pointer;'>");
                        }
                        else
                        {
                            sb.Append("<td style='text-align:left;'><IMG class=NSEG1 src='../../../../images/imgSeparador.gif' style='width:9px;'>");
                        }

                        sb.Append("<nobr class='NBR' style='width:320px;'>" + oFila["t454_literal"].ToString() + "</nobr></td>");
                        break;

                    case "2":
                        sb.Append("<tr id='" + oFila["t454_idformula"].ToString() + "' ");
                        sb.Append(" style='display:none; height: 20px' nivel='2' desplegado='1'>");
                        if (oFila["t454_idformula"].ToString() == "33" || oFila["t454_idformula"].ToString() == "34")
                        {
                            sb.Append("<td style='text-align:left;'><IMG class=NSEG2 onclick=mostrar(this) src='../../../../images/plus.gif' style='cursor:pointer;'>");
                        }
                        else
                        {
                            sb.Append("<td style='text-align:left;'><IMG class=NSEG2 src='../../../../images/imgSeparador.gif' style='width:9px;'>");
                        }

                        sb.Append("<nobr class='NBR' style='width:300px;'>" + oFila["t454_literal"].ToString() + "</nobr></td>");
                        break;

                    case "3":
                        sb.Append("<tr id='" + oFila["t454_idformula"].ToString() + "' ");
                        sb.Append(" style='display:none; height:20px;' nivel='3'>");
                        sb.Append("<td style='text-align:left;'><IMG class=NSEG3 src='../../../../images/imgSeparador.gif'>");
                        sb.Append("<nobr class='NBR' style='width:280px;'>" + oFila["t454_literal"].ToString() + "</nobr></td>");
                        break;
                    }

                    for (int x = 4; x < ds.Tables[0].Columns.Count; x++)
                    {
                        if (decimal.Parse(oFila.ItemArray[x].ToString()) < 0)
                        {
                            sColor = "red";
                        }
                        else
                        {
                            sColor = "black";
                        }
                        sb.Append("<td style='color:" + sColor + "'>" + decimal.Parse(oFila.ItemArray[x].ToString()).ToString("N") + "</td>");
                    }
                    sb.Append("</tr>");
                }
                else
                {
                    if (oFila["t454_idformula"].ToString() == "8")
                    {
                        sbResultado.Append("<tr id='" + oFila["t454_idformula"].ToString() + "' style='height:17px' class='TBLFIN'>");
                        sbResultado.Append("<td style='text-align:left;'><nobr class='NBR W320 NSEG1'>Ratio</nobr></td>");
                        for (int x = 4; x < ds.Tables[0].Columns.Count; x++)
                        {
                            if (decimal.Parse(oFila.ItemArray[x].ToString()) == 0)
                            {
                                nRatio = 0;
                            }
                            else
                            {
                                nRatio = decimal.Parse(ds.Tables[0].Rows[i - 1].ItemArray[x].ToString()) * 100 / decimal.Parse(oFila.ItemArray[x].ToString());
                            }
                            if (nRatio < 0)
                            {
                                sColor = "red";
                            }
                            else
                            {
                                sColor = "black";
                            }
                            sbResultado.Append("<td style='color:" + sColor + "'>" + nRatio.ToString("N") + " %</td>");
                        }
                        sbResultado.Append("</tr>");
                    }
                    else
                    {
                        sbResultado.Append("<tr id='" + oFila["t454_idformula"].ToString() + "' style='HEIGHT: 17px' class='TBLFIN'>");
                        if (oFila["t454_idformula"].ToString() == "3")
                        {
                            sbResultado.Append("<td style='text-align:left;'><nobr class='NSEG1'>" + oFila["t454_literal"].ToString() + " / </nobr><span style='color:red'>Facturación anticipada</span></td>");
                        }
                        else
                        {
                            sbResultado.Append("<td style='text-align:left;'><nobr class='NBR W320 NSEG1'>" + oFila["t454_literal"].ToString() + "</nobr></td>");
                        }
                        for (int x = 4; x < ds.Tables[0].Columns.Count; x++)
                        {
                            if (decimal.Parse(oFila.ItemArray[x].ToString()) < 0)
                            {
                                sColor = "red";
                            }
                            else
                            {
                                sColor = "black";
                            }
                            sbResultado.Append("<td style='color:" + sColor + "'>" + decimal.Parse(oFila.ItemArray[x].ToString()).ToString("N") + "</td>");
                        }
                        sbResultado.Append("</tr>");
                    }
                }

                i++;
            }
            ds.Dispose();

            nWidthTabla      = nWidthTabla + nColumnasACrear * 100;
            sTablaContenido  = "<table id='tblDatos' class='texto' style='width:" + nWidthTabla.ToString() + "px; ' cellpadding='0'>";
            sTablaContenido += sbColgroupTitulo.ToString();
            sTablaContenido += "</colgroup>";
            sTablaContenido += sb.ToString();
            //sTablaContenido += "</tbody>";
            sTablaContenido += "</table>";

            sTablaTitulo  = "<table id='tblTitulo' class='texto' style='width:" + nWidthTabla.ToString() + "px;' cellpadding='0'>";
            sTablaTitulo += sbColgroupTitulo.ToString();
            sTablaTitulo += "</colgroup>";
            sTablaTitulo += sbTitulo.ToString();
            sTablaTitulo += "</table>";

            sTablaResultado  = "<table id='tblResultado' class='texto' style='width:" + nWidthTabla.ToString() + "px;' cellpadding='0'>";
            sTablaResultado += sbColgroupTitulo.ToString();
            sTablaResultado += "</colgroup>";
            sTablaResultado += sbResultado.ToString();
            sTablaResultado += "</table>";

            return("OK@#@" + sTablaTitulo + "@#@" + sTablaContenido + "@#@" + sTablaResultado);
        }
        catch (Exception ex)
        {
            return("Error@#@" + Errores.mostrarError("Error al obtener los datos económicos.", ex));
        }
    }