コード例 #1
0
    protected void btnImportar_Click(object sender, EventArgs e)
    {
        System.Text.StringBuilder sbScript = new System.Text.StringBuilder();

        List <SCE_CARTEL_MODELO_CAMPO_BE> lstcmc = new List <SCE_CARTEL_MODELO_CAMPO_BE>();

        if (fuImportPlantillas.HasFile)
        {
            Session["sName"] = System.IO.Path.GetFileNameWithoutExtension(fuImportPlantillas.FileName);
            sExt             = System.Configuration.ConfigurationManager.AppSettings["EXCEL_FILE_EXTENCION"];
            Session["sPath"] = System.Configuration.ConfigurationManager.AppSettings["PATH_SERVER"] + Session["sName"] + sExt;

            if (ValidaExtension(sExt))
            {
                if (fuImportPlantillas.PostedFile.ContentLength > 4096)
                {
                    // Carga el archivo de la plantilla selecionada en el servidor
                    fuImportPlantillas.SaveAs((string)Session["sPath"]);

                    // Verificar si el archivo de la plantilla Excel tiene mas de una Hoja de Calculo
                    if (CARTEL_MODELO_BL.HaveMoreOneSheetExcel((string)Session["sPath"]) == true)
                    {
                        Util.RegisterAsyncAlert(upnlImportador, "__Alerta__", "La plantilla ha sido mal configurada porque tiene mas de una pestaña en esta. Por favor eliminar las pestañas adicionales y quede solo la que contiene el diseño de la plantilla y volver a reconfigurarla en el sistema.");

                        return;
                    }

                    // Obtiene los resultados de comparar los campos del Excel con los campos de la BD
                    bool   errores = false;
                    string PosFCX  = string.Empty;
                    string PosFCY  = string.Empty;

                    // Llena la grilla con el informe de la comparación del Excel y la BD
                    grvCampos.DataSource = CARTEL_MODELO_BL.GetInforme((string)Session["sPath"],
                                                                       Convert.ToInt32(hidIdCartel.Value),
                                                                       Convert.ToInt32(hidIdModelo.Value),
                                                                       Convert.ToInt32(hidDigitos.Value),
                                                                       ref errores,
                                                                       ref PosFCX,
                                                                       ref PosFCY);

                    grvCampos.DataBind();

                    if (errores == true) // En caso de que haiga diferencias entre el Excel y la BD
                    {
                        lblCartel.Text    = hidCartelModelo.Value.ToString();
                        lblDigitos.Text   = hidNroDigitos.Value.ToString();
                        lblPlantilla.Text = Session["sName"].ToString();

                        Util.RegisterAsyncAlert(upnlImportador, "__Alerta__", System.Configuration.ConfigurationManager.AppSettings["MCP_PNI"]);

                        upnlDetalleConf.Update();

                        sbScript.AppendFormat("document.getElementById('{0}').style.visibility='hidden';", btnVerPlantilla.ClientID);
                        Util.RegisterScript(upnlImportador, "__DesabilitaCtrol__", sbScript.ToString());

                        pnlBtnConfigurar.Visible  = false;
                        pnlDialgPlantilla.Visible = false;
                        pnlDetalle.Visible        = true;
                    }
                    else // En caso de que no haiga diferencias entre el Excel y la BD
                    {
                        // Convierte en PDF la plantilla Excel
                        CARTEL_MODELO_BL.ConvertExcelToPdf((string)Session["sPath"],
                                                           (System.Configuration.ConfigurationManager.AppSettings["PATH_SERVER"] + Session["sName"] + ".pdf"));

                        // Actualiza las tablas SCE_CARTEL_MODELO (NOM_PLANTILLA) y SCE_CARTEL_MODELO_CAMPO (Campos POS X y POS Y)
                        foreach (GridViewRow Row in grvCampos.Rows)
                        {
                            int i = Convert.ToInt32(Row.RowIndex);

                            CARTEL_MODELO_CAMPO_BE = new SCE_CARTEL_MODELO_CAMPO_BE();

                            CARTEL_MODELO_CAMPO_BE.ID_CAMPO = Convert.ToInt32(CAMPO_BL.GetIdCampo(Server.HtmlDecode(grvCampos.Rows[i].Cells[3].Text)));
                            CARTEL_MODELO_CAMPO_BE.POSX     = Server.HtmlDecode(grvCampos.Rows[i].Cells[1].Text);
                            CARTEL_MODELO_CAMPO_BE.POSY     = Server.HtmlDecode(grvCampos.Rows[i].Cells[2].Text);
                            lstcmc.Add(CARTEL_MODELO_CAMPO_BE);
                        }

                        // Actualiza la tabla SCE_CARTEL_MODELO (Campo NOM_PLANTILLA)
                        CARTEL_MODELO_BL.ActualizarCMC1(Convert.ToInt32(hidIdCartel.Value),
                                                        Convert.ToInt32(hidIdModelo.Value),
                                                        Convert.ToInt32(hidDigitos.Value),
                                                        Session["sName"].ToString(),
                                                        lstcmc,
                                                        PosFCX,
                                                        PosFCY);

                        // Llena el objeto con los resultados de la importacion realizada correctamente
                        CARTEL_MODELO_BE = CARTEL_MODELO_BL.ObtenerPorID1(Convert.ToInt32(hidIdCartel.Value),
                                                                          Convert.ToInt32(hidIdModelo.Value),
                                                                          Convert.ToInt32(hidDigitos.Value));

                        // Llena los labeles y la grilla con los campos que estan en la BD o NO
                        lblCartel.Text    = CARTEL_MODELO_BE.DESCRIPCION.ToString().Trim();
                        lblDigitos.Text   = CARTEL_MODELO_BE.NRODIGITOS.ToString().Trim();
                        lblPlantilla.Text = CARTEL_MODELO_BE.NOM_PLANTILLA.ToString().Trim();

                        /***********************************************************************************/
                        /*COPIA LA PLANTILLA EXCEL AL FILE SERVER POR SUPLANTACION DE WINDOWS*/
                        /***********************************************************************************/
                        //string strPathServer = System.Configuration.ConfigurationManager.AppSettings["PATH_SERVER"];
                        //string strPathFs = System.Configuration.ConfigurationManager.AppSettings["PATH_PLANTILLAS_FS"];

                        //string username = System.Configuration.ConfigurationManager.AppSettings["LOGIN_FS"];
                        //string password = System.Configuration.ConfigurationManager.AppSettings["PASSWORD_FS"];
                        //string domain = System.Configuration.ConfigurationManager.AppSettings["DOMINIO_FS"];

                        /***********************************************************************************/
                        /***********************************************************************************/

                        //// Create Impersonation Object
                        //WI impersonation = new WI(domain, username, password);

                        //// Start Impersonation
                        //impersonation.Impersonate();

                        ///*Copiar la plantilla Excel Generada en la servidor.*/
                        //FileInfo file = new FileInfo(Convert.ToString(Session["sPath"]));
                        //file.CopyTo(strPathFs + Session["sName"].ToString() + sExt, true);

                        //// Stop Impersonation
                        //impersonation.Revert();

                        /***********************************************************************************/
                        /***********************************************************************************/

                        //System.Security.Principal.WindowsImpersonationContext impersonationContext;
                        //impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();

                        ///*Copiar la plantilla Excel Generada en la servidor.*/
                        //FileInfo file = new FileInfo(Session["sPath"].ToString());
                        //file.CopyTo(strPathFs + Session["sName"].ToString() + sExt, true);

                        //impersonationContext.Undo();

                        /***********************************************************************************/
                        /***********************************************************************************/

                        //if (impersonateValidUser(username, domain, password))
                        //{
                        //    //Insert your code that runs under the security context of the authenticating user here.
                        //    /*Copiar la plantilla Excel Generada en la servidor.*/
                        //    FileInfo file = new FileInfo(Convert.ToString(Session["sPath"]));
                        //    file.CopyTo(strPathFs + Session["sName"].ToString() + sExt, true);

                        //    undoImpersonation();
                        //}
                        //else
                        //{
                        //    //Your impersonation failed. Therefore, include a fail-safe mechanism here.
                        //}

                        /***********************************************************************************/
                        /***********************************************************************************/



                        /***********************************************************************************/
                        /***********************************************************************************/

                        // Llena la grilla de informe de importación
                        grvCampos.DataSource = CARTEL_MODELO_BE.CAMPOS;
                        grvCampos.DataBind();

                        Util.RegisterAsyncAlert(upnlImportador, "__Alerta__", System.Configuration.ConfigurationManager.AppSettings["MCP_PIC"]);

                        upnlDetalleConf.Update();

                        sbScript.AppendFormat("document.getElementById('{0}').style.visibility='visible';", btnVerPlantilla.ClientID);
                        Util.RegisterScript(upnlImportador, "__DesabilitaCtrol__", sbScript.ToString());

                        /************************/
                        CargaCartelesModelo();
                        upnlBusqueda.Update();
                        /************************/

                        pnlBtnConfigurar.Visible  = false;
                        pnlDialgPlantilla.Visible = false;
                        pnlDetalle.Visible        = true;
                    }
                }
                else
                {
                    Util.RegisterAsyncAlert(upnlImportador, "__Alerta__", "El archivo exede el tamaño permitido");

                    pnlBtnConfigurar.Visible  = false;
                    pnlDialgPlantilla.Visible = true;

                    upnlImportador.Update();
                }
            }
            else
            {
                Util.RegisterAsyncAlert(upnlImportador, "__Alerta__", "El archivo no es de tipo Excel");

                pnlBtnConfigurar.Visible  = false;
                pnlDialgPlantilla.Visible = true;

                upnlImportador.Update();
            }
        }
        else
        {
            Util.RegisterAsyncAlert(upnlImportador, "__Alerta__", "No ha seleccionado el archivo");

            pnlBtnConfigurar.Visible  = false;
            pnlDialgPlantilla.Visible = true;

            upnlImportador.Update();
        }
    }