private void Relatorio_PreencherDados(NPOI.HSSF.UserModel.HSSFWorkbook xlPackage, System.Data.DataTable dados, String nomePlanilha, Boolean gerarCabecalho, Int32 primeiraLinha)
    {
        #region Declaração de variáveis

        NPOI.SS.UserModel.ISheet worksheet = null;
        Int32 colunasQtd = 0;
        Int32 linhasQtd = 0;
        Int32 registrosQtd = 0;
        List<Int32> colunasDate = null;
        List<Int32> colunasVarcharMax = null;

        #endregion

        try
        {
            #region Inicialização de objetos

            if (xlPackage.GetSheet(nomePlanilha) != null)
            {
                worksheet = xlPackage.GetSheet(nomePlanilha);
            }
            else
            {
                worksheet = xlPackage.CreateSheet(nomePlanilha);
            }

            #endregion

            #region Preenche os dados do relatório

            colunasQtd = dados.Columns.Count;
            linhasQtd = dados.Rows.Count;

            #region CABEÇALHO

            if (gerarCabecalho)
            {
                for (Int32 i = 0; i <= colunasQtd - 1; i++)
                {
                    String nomeColuna = String.Empty;
                    nomeColuna = dados.Columns[i].Caption;

                    NPOI.SS.UserModel.IRow row = worksheet.GetRow(2);
                    if (row == null) row = worksheet.CreateRow(2);

                    NPOI.SS.UserModel.ICell cell = row.GetCell(i);
                    if (cell == null) cell = row.CreateCell(i);

                    cell.SetCellValue(nomeColuna);
                }
            }

            #endregion

            #region DADOS

            colunasDate = new List<Int32>();
            colunasVarcharMax = new List<Int32>();
            registrosQtd = 1;
            Int32 rowIndex = 0;
            for (Int32 nRow = primeiraLinha; registrosQtd <= linhasQtd; nRow++)
            {
                for (Int32 nCol = 0; nCol < colunasQtd; nCol++)
                {
                    NPOI.SS.UserModel.IRow row = worksheet.GetRow(nRow);
                    if (row == null) row = worksheet.CreateRow(nRow);

                    NPOI.SS.UserModel.ICell cell = row.GetCell(nCol);
                    if (cell == null) cell = row.CreateCell(nCol);

                    String valor = dados.Rows[rowIndex][nCol].ToString().Replace("'", "´");

                    if (dados.Columns[nCol].DataType.ToString() == "System.Decimal")
                    {
                        if (valor.Trim() != String.Empty)
                        {
                            cell.SetCellValue(Double.Parse(valor));
                        }
                    }
                    else if (dados.Columns[nCol].DataType.ToString() == "System.Int32")
                    {
                        if (valor.Trim() != String.Empty)
                        {
                            cell.SetCellValue(Int32.Parse(valor));
                        }
                    }
                    else
                    {
                        cell.SetCellValue(valor);
                    }
                }
                registrosQtd++;
                rowIndex++;
            }

            #endregion

            #endregion
        }
        catch (Exception ex)
        {
            #region Tratamento de erro

            throw ex;

            #endregion
        }
    }
Example #2
0
        private void RelatorioGerencial_PreencherDados(NPOI.HSSF.UserModel.HSSFWorkbook xlPackage, System.Data.DataTable dados, String nomePlanilha)
        {
            #region Declaração de variáveis

            NPOI.SS.UserModel.ISheet worksheet = null;
            Int32 colunasQtd = 0;
            Int32 linhasQtd = 0;
            List<Int32> colunasDate = null;
            List<Int32> colunasVarcharMax = null;

            #endregion

            try
            {
                #region Inicialização de objetos

                if (xlPackage.GetSheet(nomePlanilha) != null)
                {
                    worksheet = xlPackage.GetSheet(nomePlanilha);
                }
                else
                {
                    worksheet = xlPackage.CreateSheet(nomePlanilha);
                }

                #endregion

                #region Preenche os dados do relatório

                colunasQtd = dados.Columns.Count;
                linhasQtd = dados.Rows.Count;

                #region CABEÇALHO

                for (Int32 i = 0; i <= colunasQtd - 1; i++)
                {
                    String nomeColuna = String.Empty;
                    nomeColuna = dados.Columns[i].Caption;

                    NPOI.SS.UserModel.IRow row = worksheet.GetRow(2);
                    if (row == null) row = worksheet.CreateRow(2);

                    NPOI.SS.UserModel.ICell cell = row.GetCell(i);
                    if (cell == null) cell = row.CreateCell(i);

                    cell.SetCellValue(nomeColuna);
                }

                #endregion

                #region DADOS

                colunasDate = new List<Int32>();
                colunasVarcharMax = new List<Int32>();
                for (Int32 nRow = 2; nRow <= (linhasQtd + 1); nRow++)
                {
                    for (Int32 nCol = 0; nCol <= colunasQtd - 1; nCol++)
                    {
                        NPOI.SS.UserModel.IRow row = worksheet.GetRow(nRow + 1);
                        if (row == null) row = worksheet.CreateRow(nRow + 1);

                        NPOI.SS.UserModel.ICell cell = row.GetCell(nCol);
                        if (cell == null) cell = row.CreateCell(nCol);

                        String valor = dados.Rows[nRow - 2][nCol].ToString().Replace("'", "´");

                        if (dados.Columns[nCol].DataType.ToString() == "System.Decimal")
                        {
                            if (valor.Trim() != String.Empty)
                            {
                                cell.SetCellValue(Double.Parse(valor));
                            }
                        }
                        else if (dados.Columns[nCol].DataType.ToString() == "System.Int32")
                        {
                            if (valor.Trim() != String.Empty)
                            {
                                cell.SetCellValue(Int32.Parse(valor));
                            }
                        }
                        else
                        {
                            cell.SetCellValue(valor);
                        }
                    }
                }

                #endregion

                #endregion
            }
            catch (Exception ex)
            {
                #region Tratamento de erro

                throw new Exception("MMAA.Pedidos.Web.WS.Pedido.Relatorio_PreencherDados: " + ex.Message);

                #endregion
            }
        }