C# - Reading Excel

Asked

Viewed 45 times

0

I was able to read an excel file, but when showing console the information does not leave spaces, how can I give space in the middle? kkkk

inserir a descrição da imagem aqui

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;

namespace Leitura_Excel
{
    class Program
    {
        static void Main(string[] args)
        {
            OleDbConnection conexao = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0.0;
                                                            Data Source=C:\Users\re029391\Documents\Resource It\Documentos\Cargos.xlsx;
                                                            Extended Properties='Excel 12.0 Xml;HDR=YES'");

            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Planilha1$]", conexao);
            DataSet ds = new DataSet();

            try
            {
                conexao.Open();
                adapter.Fill(ds);
                Console.WriteLine("".PadRight(50, '-'));
                Console.WriteLine("Nome".PadRight(35) + "Cargo".PadLeft(15));
                Console.WriteLine("".PadRight(50, '-'));
                foreach (DataRow linha in ds.Tables[0].Rows)
                {
                    Console.WriteLine(linha["Nome"].ToString() + linha["Cargo"].ToString().PadRight(50));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Erro ao acessar os dados: " + ex.Message);
            }
            finally
            {
                conexao.Close();
            }
            Console.WriteLine("".PadRight(50, '-'));
            Console.WriteLine("Feito!");
            Console.ReadKey();
        }
    }
}
  • 1

    This Padright inside the foreach shouldn’t be on the line["Name"]. Tostring()? The same way you put it in the title.

  • You’re right! It worked, thanks!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.