コード例 #1
0
        public void ProcessRequest(HttpContext context)
        {
            var cnn = context.Request.QueryString["cnn"];


            var mvx = new CMvx();

            mvx.RegisterSingleton <IConfiguracionDeConexion, frmMainMenu.ConfiguracionDeConexion>(new frmMainMenu.ConfiguracionDeConexion(cnn));

            mvx.RegisterType <IBaseDeDatosServicio, BaseDeDatosServicio>();
            mvx.RegisterType <IOrdenDeVentaServicio, OrdenDeVentaServicio>();

            XLWorkbook   workbook  = new XLWorkbook();
            IXLWorksheet worksheet = workbook.Worksheets.Add("Detalle de Orden de Venta");

            worksheet.Protect("Mobility2016$$");

            var servicio      = mvx.Resolve <IOrdenDeVentaServicio>();
            var templateExcel = servicio.GenerarColumnasParaTemplate();
            int i             = 1;

            foreach (var columna in templateExcel)
            {
                worksheet.Cell(1, i).Value = columna.VALUE;
                worksheet.Column(i).Style.Protection.SetLocked(false);
                worksheet.Column(i).Width = 25;
                if (columna.VALUE == "Fecha de entrega")
                {
                    worksheet.Column(i).Style.NumberFormat.Format = "dd/mm/yyyy";
                }
                if (columna.VALUE == "Cantidad")
                {
                    worksheet.Column(i).Style.NumberFormat.Format = "##.######";
                }

                i++;
            }



            worksheet.Row(1).Style.Protection.SetLocked(true);

            MemoryStream s = (MemoryStream)GetStream(workbook);

            HttpResponse response = HttpContext.Current.Response;

            response.ClearContent();
            response.Clear();
            response.ContentType = "text/plain";
            response.AddHeader("Content-Disposition",
                               "attachment; filename=sale_order.xlsx;");
            //response.TransmitFile(HttpContext.Current.Server.MapPath("~/UploadedFiles/SalesOrders/sale_order.xlsx"));
            response.BinaryWrite(s.ToArray());
            response.Flush();
            response.End();
        }
コード例 #2
0
        private void ProtectSheet(IXLWorksheet ws)
        {
            var passw = GetParameter("Password");

            if (string.IsNullOrEmpty(passw))
            {
                passw = Guid.NewGuid().ToString();
            }
            ws.Protect(passw);
        }
コード例 #3
0
        private void BtnExport_Click(object sender, EventArgs e)
        {
            if (DtgPersonList.DataSource == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(fullPath))
            {
                return;
            }

            IXLWorkbook  workbook  = new XLWorkbook();
            IXLWorksheet worksheet = workbook.AddWorksheet("Veriler");

            IXLRow row = worksheet.Row(1);

            row.Cell(1).Value = "No";
            row.Cell(2).Value = "Ad Soyad";
            row.Cell(3).Value = "Doğum Tarihi";

            row = row.RowBelow();

            foreach (DataGridViewRow item in DtgPersonList.Rows)
            {
                row.Cell("A").Value = item.Cells["Id"].Value;
                row.Cell("B").Value = item.Cells["Name"].Value;
                row.Cell("C").Value = item.Cells["BirthDate"].Value;
                row = row.RowBelow();
            }

            /*foreach (Person item in people)
             * {
             *  row.Cell("A").Value = item.Id;
             *  row.Cell("B").Value = item.Name;
             *  row.Cell("C").Value = item.BirthDate;
             *  row = row.RowBelow();
             * }*/

            var firstRow = worksheet.FirstRow();

            firstRow.RowUsed().SetAutoFilter(true);
            firstRow.Cells().Style.Font.Bold = true;
            firstRow.Height = firstRow.Height * 2;

            var column = worksheet.Column("C");

            column.Width = 11.5;


            worksheet.Protect("12345");
            workbook.SaveAs(fullPath);
            System.Diagnostics.Process.Start(fullPath);
        }
コード例 #4
0
        private void GenerarExcelDetalle()
        {
            var poliza = Polizas.FirstOrDefault(x => x.IS_SELECTED);

            if (poliza == null)
            {
                InteraccionConUsuarioServicio.Mensaje("Debe seleccionar una poliza para generar la plantilla.");
                return;
            }
            SaveFileDialog dialogoGuardar = new SaveFileDialog();

            dialogoGuardar.Filter           = "Excel xlsx (*.xlsx)|*.xlsx";
            dialogoGuardar.FilterIndex      = 2;
            dialogoGuardar.RestoreDirectory = true;

            if (dialogoGuardar.ShowDialog() == DialogResult.OK)
            {
                path = dialogoGuardar.FileName;

                XLWorkbook   workbook  = new XLWorkbook();
                IXLWorksheet worksheet = workbook.Worksheets.Add("Detalle");
                worksheet.Protect("Mobility2016$$");

                worksheet.Cell(2, 2).Value = "Póliza";
                worksheet.Cell(2, 3).Value = poliza.CODIGO_POLIZA;

                worksheet.Cell(2, 2).Style.Font.Bold = true;

                worksheet.Cell(3, 2).Value           = "Fecha Generación";
                worksheet.Cell(3, 3).Value           = DateTime.Now;
                worksheet.Cell(3, 2).Style.Font.Bold = true;


                int inicioEncabezadoX = 5;
                int inicioEncabezadoY = 2;

                worksheet.Column(inicioEncabezadoY + 0).Width = 20;
                worksheet.Column(inicioEncabezadoY + 1).Width = 60;
                worksheet.Column(inicioEncabezadoY + 2).Width = 20;
                worksheet.Column(inicioEncabezadoY + 3).Width = 20;
                worksheet.Column(inicioEncabezadoY + 4).Width = 10;

                //Encabezado Tabla
                for (int j = 0; j <= UiGridVistaDetalle.Columns.Count - 1; j++)
                {
                    worksheet.Cell(inicioEncabezadoX, inicioEncabezadoY + j).Value                      = UiGridVistaDetalle.Columns[j].Caption;
                    worksheet.Cell(inicioEncabezadoX, inicioEncabezadoY + j).Style.Font.Bold            = true;
                    worksheet.Cell(inicioEncabezadoX, inicioEncabezadoY + j).Style.Fill.BackgroundColor = XLColor.BabyBlueEyes;
                    worksheet.Cell(inicioEncabezadoX, inicioEncabezadoY + j).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);
                }

                //Datos
                for (int i = 0; i <= UiGridVistaDetalle.RowCount - 1; i++)
                {
                    for (int j = 0; j <= UiGridVistaDetalle.Columns.Count - 1; j++)
                    {
                        //Celda de total
                        if (j == 4)
                        {
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).FormulaR1C1 = "=RC[-2]*RC[-1]";
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).Style.NumberFormat.Format = " #,##0.00";
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).DataType = XLCellValues.Number;
                        }
                        else
                        {
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).Value =
                                (UiGridVistaDetalle.GetRowCellValue(i, UiGridVistaDetalle.Columns[j]).ToString());
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY)
                            .Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Left);
                        }

                        if (j == 3) //Celda de costo unitario
                        {
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).DataType = XLCellValues.Number;
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).Style.NumberFormat.Format = " #,##0.00";
                            worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY)
                            .Style.Protection.SetLocked(false);
                            // worksheet.Cell(i + 1 + inicioEncabezadoX, j + inicioEncabezadoY).DataValidation.Decimal.Between(0, double.MaxValue - 1);
                        }
                    }
                }

                //Convertir en tabla de excel
                IXLRange rngTable   = worksheet.Range(worksheet.Cell(inicioEncabezadoX, inicioEncabezadoY), worksheet.Cell(inicioEncabezadoX + UiGridVistaDetalle.RowCount, inicioEncabezadoY + UiGridVistaDetalle.Columns.Count - 1));
                IXLTable excelTable = rngTable.CreateTable();
                excelTable.ShowTotalsRow = true;

                excelTable.Field(4).TotalsRowFunction = XLTotalsRowFunction.Sum;
                excelTable.Field(3).TotalsRowFunction = XLTotalsRowFunction.Sum;
                excelTable.Field(2).TotalsRowFunction = XLTotalsRowFunction.Sum;
                excelTable.Field(1).TotalsRowFunction = XLTotalsRowFunction.Count;
                excelTable.Field(0).TotalsRowLabel    = "Total:";
                excelTable.ShowAutoFilter             = false;

                //Guardar Excel
                if ((File.Exists(path)))
                {
                    System.IO.File.Delete(path);
                }
                workbook.SaveAs(path);
                Action <bool> abrirExcel = AbrirExcel;

                InteraccionConUsuarioServicio.Confirmar("Archivo generado exitosamente, ¿Desea abrir el archivo?",
                                                        abrirExcel); //MessageBox.Show("Archivo generado exitosamente, ¿Desea abrir el archivo?", "Operación exitosa", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            var cnn = context.Request.QueryString["cnn"];

            var mvx = new CMvx();

            mvx.RegisterSingleton <IConfiguracionDeConexion, frmMainMenu.ConfiguracionDeConexion>(new frmMainMenu.ConfiguracionDeConexion(cnn));

            mvx.RegisterType <IBaseDeDatosServicio, BaseDeDatosServicio>();
            mvx.RegisterType <IManifiestoServicio, ManifiestoServicio>();

            XLWorkbook   workbook  = new XLWorkbook();
            IXLWorksheet worksheet = workbook.Worksheets.Add("Cargar Manifiesto");

            worksheet.Protect("Mobility2016$$");

            var servicio      = mvx.Resolve <IManifiestoServicio>();
            var templateExcel = servicio.GenerarColumnasParaTemplate();
            int i             = 1;

            foreach (var columna in templateExcel)
            {
                XLCellValues tipo         = XLCellValues.Text;
                object       defaultValue = string.Empty;
                if (columna.VALUE.Trim() == "Fecha visita")
                {
                    tipo         = XLCellValues.DateTime;
                    defaultValue = DateTime.Now.Date;
                }
                else if (columna.VALUE == "Hora recomendada visita")
                {
                    tipo         = XLCellValues.TimeSpan;
                    defaultValue = new TimeSpan();
                }
                else if (columna.VALUE == "Cantidad")
                {
                    tipo         = XLCellValues.Number;
                    defaultValue = 0;
                }
                else if (columna.VALUE == "Número documento")
                {
                    tipo         = XLCellValues.Number;
                    defaultValue = 0;
                }
                else if (columna.VALUE == "GPS cliente")
                {
                    tipo         = XLCellValues.Text;
                    defaultValue = "";
                }
                for (int j = 2; j < 3; j++)
                {
                    worksheet.Cell(j, i).DataType = tipo;
                    worksheet.Cell(j, i).Value    = defaultValue;
                }
                worksheet.Cell(1, i).DataType                   = XLCellValues.Text;
                worksheet.Cell(1, i).Value                      = columna.VALUE;
                worksheet.Cell(1, i).Style.Font.Bold            = true;
                worksheet.Cell(1, i).Style.Fill.BackgroundColor = XLColor.BabyBlueEyes;
                worksheet.Cell(1, i).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);
                worksheet.Column(i).Style.Protection.SetLocked(false);
                worksheet.Column(i).Width = 20;
                i++;
            }

            worksheet.Row(1).Style.Protection.SetLocked(true);

            MemoryStream s = (MemoryStream)GetStream(workbook);

            HttpResponse response = HttpContext.Current.Response;

            response.ClearContent();
            response.Clear();
            response.ContentType = "text/plain";
            response.AddHeader("Content-Disposition",
                               "attachment; filename=Load_Manifest.xlsx;");
            response.BinaryWrite(s.ToArray());
            response.Flush();
            response.End();
        }