Beispiel #1
0
        public string Descarga_repartoExcel(int servicio, string tipoRecibo, string cicloFacturacion, int Estado, string fecha_ini, string fecha_fin, string suministro, string medidor, int operario)
        {
            int       _fila = 2;
            string    _ruta;
            string    nombreServicio = "";
            string    FileExcel      = "";
            int       usuario        = 0;
            DataTable _lista         = new DataTable();

            try
            {
                NLectura obj_negocio = new DSIGE.Negocio.NLectura();
                _lista = obj_negocio.N_ListandoReparto_Tomadas(servicio, tipoRecibo, cicloFacturacion, Estado, fecha_ini, fecha_fin, suministro, medidor, operario);

                if (_lista.Rows.Count == 0)
                {
                    return(_Serialize("0|No hay informacion para mostrar.", true));
                }

                usuario        = ((Sesion)Session["Session_Usuario_Acceso"]).usuario.usu_id;
                nombreServicio = usuario + "LISTADO_REPARTO.xlsx";
                _ruta          = Path.Combine(Server.MapPath("~/Temp/") + "\\" + nombreServicio);

                string rutaServer = ConfigurationManager.AppSettings["Archivos"];
                FileExcel = rutaServer + nombreServicio;

                FileInfo _file = new FileInfo(_ruta);
                if (_file.Exists)
                {
                    _file.Delete();
                    _file = new FileInfo(_ruta);
                }

                using (Excel.ExcelPackage oEx = new Excel.ExcelPackage(_file))
                {
                    Excel.ExcelWorksheet oWs = oEx.Workbook.Worksheets.Add("listado_reparto");
                    oWs.Cells.Style.Font.SetFromFont(new Font("Tahoma", 9));

                    oWs.Cells[1, 1].Value = "Suministro";
                    oWs.Cells[1, 2].Value = "Ciclo";
                    oWs.Cells[1, 3].Value = "Fecha Recojo";
                    oWs.Cells[1, 4].Value = "Fecha Entrega";
                    oWs.Cells[1, 5].Value = "Unidad Lectura";
                    oWs.Cells[1, 6].Value = "Operario";
                    oWs.Cells[1, 7].Value = "Estado";

                    foreach (DataRow oBj in _lista.Rows)
                    {
                        oWs.Cells[_fila, 1].Value = oBj["suministro"].ToString();
                        oWs.Cells[_fila, 2].Value = oBj["ciclo"].ToString();
                        oWs.Cells[_fila, 3].Value = oBj["fecha_recojo"].ToString();
                        oWs.Cells[_fila, 4].Value = oBj["fecha_entrega"].ToString();

                        oWs.Cells[_fila, 5].Value = oBj["unidad_lectura"].ToString();
                        oWs.Cells[_fila, 6].Value = oBj["operario"].ToString();
                        oWs.Cells[_fila, 7].Value = oBj["estado"].ToString();

                        _fila++;
                    }

                    //------definir el tamaño de todo el documento
                    oWs.Cells.Style.Font.Size = 8;

                    oWs.Row(1).Style.Font.Bold           = true;
                    oWs.Row(1).Style.HorizontalAlignment = Style.ExcelHorizontalAlignment.Center;
                    oWs.Row(1).Style.VerticalAlignment   = Style.ExcelVerticalAlignment.Center;

                    for (int i = 1; i <= 5; i++)
                    {
                        oWs.Column(i).AutoFit();
                    }
                    oEx.Save();
                }
                return(_Serialize("1|" + FileExcel, true));
            }
            catch (Exception ex)
            {
                return(_Serialize("0|" + ex.Message, true));
            }
        }