Error while trying to read excel file C#

Asked

Viewed 312 times

2

public partial class FrmEditar : Form
{
    private OleDbConnection _olecon;
    private OleDbCommand _oleCmd;
    private static string _arquivo = string.Empty;
    private string _stringConexao = string.Empty;
    private string _consulta;

    public FrmEditar()
    {
        InitializeComponent();
    }

    private void btnSelecionarExcel_Click(object sender, EventArgs e)
    {

        OpenFileDialog vAbreArq = new OpenFileDialog
        {
            //Filter = "*.xls | Microsoft Excel| *.xlsx | Microsoft Excel",
            Title = Resources.frmEditar_btnSelecionarExcel_Click_Selecione_o_Arquivo,
            RestoreDirectory = true
        };

        if (vAbreArq.ShowDialog() == DialogResult.OK)
        {
            try
            {
                _arquivo = vAbreArq.FileName;
                _stringConexao = $@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={_arquivo};Extended Properties='Excel 12.0 Xml;HDR=YES;ReadOnly=False';";
                _olecon = new OleDbConnection(_stringConexao);
                _olecon.Open();

                _oleCmd = new OleDbCommand
                {
                    Connection = _olecon,
                    CommandType = CommandType.Text
                };


                txtExcel.Text = vAbreArq.FileName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }       

    private void btnFechar_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void btnBuscar_Click(object sender, EventArgs e)
    {
        try
        {
            _oleCmd.CommandText = "SELECT CodFunci, NomeFunci FROM [Empregados$] Where CodFunci = " + txtCodigo.Text;
            OleDbDataReader reader = _oleCmd.ExecuteReader(); \\Erro acontece nessa linha.

            while (reader != null && reader.Read())
            {
                txtCodigo.Text = reader.GetValue(0).ToString();
                txtNome.Text = reader.GetString(1);
            }

            reader?.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

The mistake that happens is:

Empregados$ is a not Valid name. Make sure that it does not include invalid characters or punctuation and that it is not Too long.

The name of the spreadsheet I’m using is Employees.xlsx

1 answer

5


Empregados$ must be the name of the spreadsheet within file and not the name of file, ex:

inserir a descrição da imagem aqui

In the example file above, you would have to use Planilha1$

  • What an urge to give you a kiss bro!! It was worth more. Okay it was stupid I did not reason this, but it was worth more.

  • instead of the kiss, give a vote on my answer here on the left. will be well received kkkkkk ;)

  • 1

    I was not allowed to vote, but I gave the right answer, that’s all I can do

Browser other questions tagged

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