Exemplo n.º 1
0
 //----------------------------------------------------------//
 //	Name : Calculate										//
 //	Description : Lance le calcul des feuilles				//
 //	Arguments :												//
 //	Rédacteur : Scherrer Ludovic							//
 //----------------------------------------------------------//
 public bool Calculate()
 {
     try
     {
         exApp.CalculateFull();
         return(true);
     }
     catch
     {
         LastMessage = "Error on calculation";
         status.Add(LastMessage);
         return(false);
     }
 }
Exemplo n.º 2
0
 public void ReplaceText(string what, string replacement, int row, int col, string sheetName, bool isSheetName, int sheetIndex)
 {
     try
     {
         _application.EnableEvents   = false;
         _application.ScreenUpdating = false;
         _application.Calculation    = Excel.XlCalculation.xlCalculationManual;
         Excel.Worksheet worksheet = _workbook.Worksheets[sheetIndex];
         if (isSheetName && !String.IsNullOrEmpty(replacement) && !Regex.IsMatch(replacement, "[\\[\\]*?]"))
         {
             if (replacement.Length > 31)
             {
                 replacement = replacement.Substring(0, 31);
             }
             worksheet.Name = replacement;
         }
         else
         {
             Excel.Range xlRange = worksheet.UsedRange;
             if (row > 0 && col > 0 && !string.IsNullOrEmpty(replacement))
             {
                 string formulaCell = ((object[, ])xlRange.Formula)[row, col].ToString();
                 try
                 {
                     formulaCell = formulaCell.Replace(what, replacement);
                     ((Excel.Range)xlRange.Cells[row, col]).Formula = formulaCell;
                 }
                 catch (Exception ex)
                 {
                     ((Excel.Range)xlRange.Cells[row, col]).Formula = " " + formulaCell;
                     //trường hợp formula sửa thành hàm lỗi cho thêm dấu cách trước đầu
                 }
             }
         }
         Marshal.ReleaseComObject(worksheet);
         _application.EnableEvents   = true;
         _application.ScreenUpdating = true;
         _application.Calculation    = Excel.XlCalculation.xlCalculationAutomatic;
         _application.CalculateFull();
     }
     catch (Exception ex)
     {
         this.Dispose();
         throw ex;
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Returns a cell value as object, using A1, A2, B1 syntax.
        /// </summary>
        /// <remarks>Throws an exception if operation fails</remarks>
        /// <param name="sheet">Sheetname, string</param>
        /// <param name="cell">Cell, string (A1, A2, B1 syntax)</param>
        /// <returns>Cellvalue as object</returns>
        public object GetCellValue(string sheet, string cell)
        {
            try
            {
                if (_worksheet == null || _worksheet.Name != sheet)
                {
                    _worksheet = _workbook.Worksheets[sheet];
                }

                _excelApp.CalculateFull();

                //celltype = ws.Range[cell].Value.GetType();
                return(_worksheet.Range[cell].Value);
            }
            catch (Exception)
            {
                throw new Exception(string.Format("Could not read worksheet:{0} cell:{1}", sheet, cell));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Counts the number of failed sheets and write the failed cells to a file in
        /// <see cref="tempOutputPath"/> whose name is contained in <see cref="ERROR_OUTPUT_FILE"/>
        /// </summary>
        /// <returns>the number of failed sheets.</returns>
        internal int Check()
        {
            sheetErrorInfo = new List <string>();
            sheetsAndFuncs = new Dictionary <string, HashSet <string> >();
            int failedSheets = 0;

            Excel.Application excelApp = new Excel.Application();
            excelApp.RegisterXLL(xllPath);

            // Iterate over files in folder
            DirectoryInfo directory = new DirectoryInfo(exampleSheetPath);

            FileInfo[] files = directory.GetFiles();
            //string[] fileEntries = Directory.GetFiles(exampleSheetPath);
            foreach (string fileName in files.Where(
                         fileinfo => fileinfo.Extension.Equals(".xlsx") &&
                         !fileinfo.Attributes.HasFlag(FileAttributes.Hidden)).Select(fileinfo => fileinfo.FullName))
            {
                Excel.Workbook wb = excelApp.Workbooks.Open(fileName);
                wb.ForceFullCalculation = true;
                excelApp.CalculateFull();
                if (!WorkbookIsOK(wb))
                {
                    failedSheets++;
                }
                wb.Close(false);
            }

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

            foreach (string sheetname in sheetsAndFuncs.Keys)
            {
                foreach (string funcName in sheetsAndFuncs[sheetname])
                {
                    sheetAndFuncsContent.Add(sheetname + "," + funcName);
                }
            }
            File.WriteAllLines(Path.Combine(tempOutputPath, SHEET_AND_FUNCS_FILE), sheetAndFuncsContent.ToArray());
            File.WriteAllLines(Path.Combine(tempOutputPath, ERROR_OUTPUT_FILE), sheetErrorInfo.ToArray());

            return(failedSheets);
        }
Exemplo n.º 5
0
        public void CrearBoletaExcel(cPrestamo p)
        {
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

            try
            {
                object   m  = Type.Missing;
                Workbook wb = app.Workbooks.Open(
                    @"C:\EfectivoInmediato\Boleta2M.xlsx",
                    m, false, m, m, m, m, m, m, m, m, m, m, m, m);

                Worksheet ws = (Worksheet)wb.ActiveSheet;

                Range r = (Range)ws.UsedRange;

                bool success;

                //Se escribe el nombre de la sucursal.
                success = (bool)r.Replace(
                    @"\sucursal\",
                    "MATRIZ",
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el horario de la sucursal.
                success = (bool)r.Replace(
                    @"\hor\",
                    "9:00 a.m. A 7:00 p.m.",
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe la fecha del documento.
                success = (bool)r.Replace(
                    @"\fecha\",
                    p.FechaPrestamo,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el número de contrato.
                success = (bool)r.Replace(
                    @"\contrato\",
                    p.Contrato,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el nombre del cliente.
                success = (bool)r.Replace(
                    @"\cliente\",
                    "[" + cliente.IdCliente + "] " + cliente.NombreCompleto,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el tipo de idenficación del cliente.
                success = (bool)r.Replace(
                    @"\tipoIDCli\",
                    cliente.TipoIdentificacion,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el número de idenficación del cliente.
                success = (bool)r.Replace(
                    @"\numIDCli\",
                    cliente.ClaveIdentificacion,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el domicilio del cliente.
                success = (bool)r.Replace(
                    @"\domCli\",
                    cliente.Domicilio,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el teléfono del cliente.
                success = (bool)r.Replace(
                    @"\telCli\",
                    cliente.Telefono1,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                /*
                 * //Se escribe el correo electrónico del cliente.
                 * success = (bool)r.Replace(
                 *  @"\emailCliente\",
                 *  cliente.CorreoElectronico,
                 *  XlLookAt.xlPart,
                 *  XlSearchOrder.xlByRows,
                 *  true, m, m, m);
                 */
                // Se escribe el nombre del cotitular del cliente.
                success = (bool)r.Replace(
                    @"\cotitular\",
                    cliente.NombreCotitular,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el CAT.
                success = (bool)r.Replace(
                    @"\cat\",
                    (float.Parse(interes.CalcularCAT()) / 100).ToString(),
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe la tasa de interes anual.
                success = (bool)r.Replace(
                    @"\tan\",
                    (((float.Parse(interes.Financiamiento) + float.Parse(interes.Administracion)) / 100) * 12).ToString(),
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe la cantidad para desempeñar.
                success = (bool)r.Replace(
                    @"\desemp\",
                    pagos[pagos.Count - 1].TotalDesempeno,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el porcentaje de almacenaje.
                success = (bool)r.Replace(
                    @"\pAlmcnj\",
                    (float.Parse(interes.Almacenaje) / 100).ToString(),
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe la cantidad por reposición de contrato.
                success = (bool)r.Replace(
                    @"\repos\",
                    "$ 15.00",
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe la cantidad por reclamo extemporaneo.
                success = (bool)r.Replace(
                    @"\intExtempo\",
                    (float.Parse(interes.ReclamoExtemporaneoCantidad) / 100).ToString(),
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el porcentaje de gastos de administración.
                success = (bool)r.Replace(
                    @"\pAdmon\",
                    interes.Administracion,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe la fecha de vencimiento.
                success = (bool)r.Replace(
                    @"\fecVen\",
                    pagos[pagos.Count - 1].FechaPago,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el interes final.
                success = (bool)r.Replace(
                    @"\finan\",
                    pagos[pagos.Count - 1].Intereses,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el almacenaje final.
                success = (bool)r.Replace(
                    @"\almcnj\",
                    pagos[pagos.Count - 1].Almacenaje,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el IVA final.
                success = (bool)r.Replace(
                    @"\iva\",
                    pagos[pagos.Count - 1].IVA,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el refrendo final.
                success = (bool)r.Replace(
                    @"\refren\",
                    pagos[pagos.Count - 1].TotalRefrendo,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe nombre de la prenda.
                success = (bool)r.Replace(
                    @"\prenda1\",
                    prenda.Descripcion + " " + prenda.Marca + " " + prenda.Modelo + " " + prenda.Serie + " " + prenda.Observaciones,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el avalúo de la prenda.
                success = (bool)r.Replace(
                    @"\ava1\",
                    prenda.Avaluo,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe la pureza del metal.
                success = (bool)r.Replace(
                    @"\pureza1\",
                    prenda.Pureza,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el peso del metal.
                success = (bool)r.Replace(
                    @"\peso1\",
                    prenda.PesoMetal,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el color de la piedra.
                success = (bool)r.Replace(
                    @"\color1\",
                    prenda.ColorPiedra,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe la claridad de la piedra.
                success = (bool)r.Replace(
                    @"\claridad1\",
                    prenda.ClaridadOPureza,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                // Se escribe el préstamo de la prenda.
                success = (bool)r.Replace(
                    @"\pres1\",
                    prenda.Prestamo,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el avalúo de la prenda.
                success = (bool)r.Replace(
                    @"\avaluoTotal\",
                    prenda.Avaluo,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el prestamo de la prenda.
                success = (bool)r.Replace(
                    @"\pres\",
                    prenda.Prestamo,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el pago mínimo.
                success = (bool)r.Replace(
                    @"\pagoMin\",
                    interes.PagoMinimo,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe la fecha de comecialización.
                success = (bool)r.Replace(
                    @"\fecCom\",
                    DateTime.Parse(pagos[pagos.Count - 1].FechaPago).AddDays(int.Parse(interes.ReclamoExtemporaneoDias)).ToShortDateString(),
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe la fecha de refrendo máximo.
                success = (bool)r.Replace(
                    @"\fecRef\",
                    DateTime.Parse(pagos[pagos.Count - 1].FechaPago).AddDays(int.Parse(interes.DiasDeGracia)).ToShortDateString(),
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el tipo de periodo de cobro.
                success = (bool)r.Replace(
                    @"\periodo\",
                    interes.Periodo,
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escriben las observaciones.
                success = (bool)r.Replace(
                    @"\observ1\",
                    "",
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el número del contrato con PROFECO.
                success = (bool)r.Replace(
                    @"\contratoProfeco\",
                    "8361-2018",
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el domicilio del establecimiento.
                success = (bool)r.Replace(
                    @"\domProp\",
                    "GIRASOL Num. 15 COL. CIUDAD INDUSTRIAL.",
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el teléfono del establecimiento.
                success = (bool)r.Replace(
                    @"\telProp\",
                    "311 160 8815",
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe el correo del establecimiento.
                success = (bool)r.Replace(
                    @"\emailProp\",
                    "-",
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                //Se escribe la dirección de la página del establecimiento.
                success = (bool)r.Replace(
                    @"\paginaProp\",
                    "-",
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                // Se escribe el nombre de la ciudad.
                success = (bool)r.Replace(
                    @"\ciudad\",
                    "TEPIC, NAYARIT",
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                // Se escribe el día.
                success = (bool)r.Replace(
                    @"\dia\",
                    p.FechaPrestamo.Substring(0, 2),
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                // Se escribe el mes.
                success = (bool)r.Replace(
                    @"\mes\",
                    p.FechaPrestamo.Substring(3, 2),
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                // Se escribe el año.
                success = (bool)r.Replace(
                    @"\año\",
                    p.FechaPrestamo.Substring(6, 4),
                    XlLookAt.xlPart,
                    XlSearchOrder.xlByRows,
                    true, m, m, m);

                app.CalculateFull();

                wb.SaveAs(@"C:\EfectivoInmediato\Boletas\Boleta_" + p.IdPrestamo);
                app.Quit();
                //app = null;

                Marshal.ReleaseComObject(app);

                ruta = @"C:\EfectivoInmediato\Boletas\Boleta_" + p.IdPrestamo + ".xlsx";
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
            finally
            {
            }
        }