Exemple #1
0
        private void excelTabular()
        {
            cnx = new SqlConnection(cdn);
            cmd = new SqlCommand();
            cmd.Connection = cnx;
            nh = new CalculoNomina.Core.NominaHelper();
            nh.Command = cmd;

            CalculoNomina.Core.tmpPagoNomina pn = new CalculoNomina.Core.tmpPagoNomina();
            pn.idempresa = GLOBALES.IDEMPRESA;
            pn.fechainicio = dtpInicioPeriodo.Value;
            pn.fechafin = dtpFinPeriodo.Value;

            DataTable dt = new DataTable();
            try
            {
                cnx.Open();
                dt = nh.obtenerNominaTabular(pn,
                    int.Parse(cmbDeptoInicial.SelectedValue.ToString()),
                    int.Parse(cmbDeptoFinal.SelectedValue.ToString()),
                    int.Parse(cmbEmpleadoInicial.SelectedValue.ToString()),
                    int.Parse(cmbEmpleadoFinal.SelectedValue.ToString()),
                    tipoNomina,
                    netocero, orden, periodo);
                cnx.Close();
                cnx.Dispose();
            }
            catch (Exception error)
            {
                MessageBox.Show("Error: \r\n \r\n" + error.Message, "Error");
            }

            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("No es posible generar el reporte. \r\n \r\n Verifique los parametros del reporte.", "Error");
                return;
            }

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Workbooks.Add();

            Microsoft.Office.Interop.Excel._Worksheet workSheet = excel.ActiveSheet;

            excel.Cells[1, 1] = dt.Rows[0][0];
            excel.Cells[1, 6] = "Periodo";
            excel.Cells[2, 1] = "RFC:";
            excel.Cells[3, 1] = "REG. PAT:";

            excel.Cells[2, 2] = dt.Rows[0][1];
            excel.Cells[3, 2] = dt.Rows[0][2];

            excel.Cells[2, 6] = dt.Rows[0][3];
            excel.Cells[2, 7] = dt.Rows[0][4];

            //SE COLOCAN LOS TITULOS DE LAS COLUMNAS
            int iCol = 1;
            for (int i = 6; i < dt.Columns.Count; i++)
            {
                excel.Cells[5, iCol] = dt.Columns[i].ColumnName;
                iCol++;
            }
            //SE COLOCAN LOS DATOS
            int contadorDt = dt.Rows.Count;
            int contador = 0;
            int progreso = 0;
            iCol = 1;
            int iFil = 6;
            Microsoft.Office.Interop.Excel.Range rng;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                progreso = (contador * 100) / contadorDt;
                toolPorcentaje.Text = progreso.ToString() + "%";
                toolEtapa.Text = "Reporte a Excel";
                contador++;
                if (i != dt.Rows.Count - 1)
                {
                   for (int j = 6; j < dt.Columns.Count; j++)
                    {
                        excel.Cells[iFil, iCol] = dt.Rows[i][j];
                        iCol++;
                    }
                    iFil++;
                }
                else
                {
                    for (int j = 6; j < dt.Columns.Count; j++)
                    {
                        excel.Cells[iFil, iCol] = dt.Rows[i][j];
                        iCol++;
                    }
                }
                iCol = 1;
            }
            iFil++;

            for (int i = 6; i < iFil; i++)
            {
                rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[i, 2];
                rng.Columns.AutoFit();

                rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[i, 14];
                rng.NumberFormat = "#,##0.00";
                rng.Formula = string.Format("=C{0}+D{0}+E{0}+F{0}+G{0}+H{0}+I{0}+J{0}+K{0}+L{0}+M{0}", i);

                rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[i, 23];
                rng.NumberFormat = "#,##0.00";
                rng.Formula = string.Format("=O{0}+P{0}+Q{0}+R{0}+S{0}+T{0}+V{0}", i);

                rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[i, 24];
                rng.NumberFormat = "#,##0.00";
                rng.Formula = string.Format("=N{0}+U{0}-W{0}", i);
            }

            int suma = iFil - 1;
            iFil++;

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 3];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(C6:C{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 4];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(D6:D{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 5];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(E6:E{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 6];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(F6:F{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 7];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(G6:G{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 8];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(H6:H{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 9];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(I6:I{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 10];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(J6:J{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 11];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(K6:K{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 12];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(L6:L{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 13];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(M6:M{0})", suma.ToString());

            //TOTAL PERCEPCIONES
            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 14];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(N6:N{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 15];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(O6:O{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 16];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(P6:P{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 17];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(Q6:Q{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 18];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(R6:R{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 19];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(S6:S{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 20];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(T6:T{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 21];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(U6:U{0})", suma.ToString());

            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 22];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(V6:V{0})", suma.ToString());

            //TOTAL DEDUCCIONES
            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 23];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(W6:W{0})", suma.ToString());

            //TOTAL NETO
            rng = (Microsoft.Office.Interop.Excel.Range)excel.Cells[iFil, 24];
            rng.NumberFormat = "#,##0.00";
            rng.Font.Bold = true;
            rng.Formula = string.Format("=SUM(X6:X{0})", suma.ToString());

            excel.Range["A1", "G3"].Font.Bold = true;
            excel.Range["A5", "X5"].Font.Bold = true;
            excel.Range["B:X"].EntireColumn.AutoFit();
            excel.Range["A6"].Select();
            excel.ActiveWindow.FreezePanes = true;
            excel.Range["A5", "X5"].Interior.ColorIndex = 36;
            excel.Range["A5", "M5"].Font.ColorIndex = 1;
            excel.Range["O5", "V5"].Font.ColorIndex = 1;
            excel.Range["N5"].Font.ColorIndex = 32;
            excel.Range["W5"].Font.ColorIndex = 32;
            excel.Range["X5"].Font.ColorIndex = 32;
            excel.Range["B6", "X" + iFil.ToString()].NumberFormat = "#,##0.00";

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "Guardar como";
            sfd.Filter = "Archivo de excel (*.xlsx)|*.xlsx";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                workSheet.SaveAs(sfd.FileName);
                excel.Visible = true;
            }

            toolPorcentaje.Text = "100%";
            toolEtapa.Text = "Reporte a Excel";
        }