加载Excel文件时出错请检查工作表名称

时间:2017-03-30 06:33:46

标签: c# excel-2007

我正在开发一个C#应用程序,需要从Excel Sheet中获取名为SMS的数据。我尝试了下面的代码但是它给了我带来的错误#34;错误加载Excel文件请检查工作表名称"。

以下是代码:

private void button3_Click_1(object sender, EventArgs e)
{

    int rows_counting, column_counting1 = 0;

    OpenFileDialog dialog = new OpenFileDialog { };

    dialog.Filter = "SMS Sending File(*.xlsx;*.xls)|*.xlsx;*.xls";

    dialog.Title = "Select Excel File For SMS";

    DialogResult dlgresult = dialog.ShowDialog();

    if (dlgresult == DialogResult.Cancel)
    {
        MessageBox.Show("You Press Cancelled :-) !!!");
    }
    else
    {
        string sms_filename = dialog.FileName;

        if (System.IO.File.Exists(sms_filename))
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", sms_filename);

                string query = String.Format("select * from [{0}$]", "SMS");

                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);

                dataSet = new DataSet();

                dataAdapter.Fill(dataSet);

                dataGridView1.DataSource = dataSet.Tables[0];

                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;

                rows_counting = dataGridView1.RowCount - 1;                                            

                column_counting1 = dataGridView1.ColumnCount;

                if (column_counting1 < 2 || column_counting1 > 2)
                {
                    MessageBox.Show("Kindly Check Column Count in Excel Sheet !!!\r\n\nThere Should Be Only Two Columns in Sheet Like Below\r\n\nCELL NUMBER | MESSAGE", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                            
                    return;
                }

                if (    dataGridView1.Columns[0].Name.ToString().ToUpper() == "CELL NUMBER" &&
                        dataGridView1.Columns[1].Name.ToString().ToUpper() == "MESSAGES")

                {
                    label25.Text = "Total SMS In Excel File " + rows_counting;
                    MessageBox.Show("Data Imported Successfully...!!!\r\n\nCheck Imported Values & SEND SMS.....!!!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    button7.Enabled = true;
                    button4.Enabled = true;

                    Cursor.Current = Cursors.Default;
                }
                else
                {
                    MessageBox.Show("Column Names Are Not In Specified Format !!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }


            }
            catch (Exception E6)
            {
                MessageBox.Show("Error Loading Excel File\r\n\nKindly Check Worksheet Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }




        }
    }


}

1 个答案:

答案 0 :(得分:0)

请确认您的工作表名称是SMS。 另请检查工作表名称中是否没有空格。