【问题标题】:Error Loading Excel FIle Kindly Check Worksheet Name加载 Excel 文件时出错请检查工作表名称
【发布时间】:2017-03-30 06:33:46
【问题描述】:

我正在开发一个 C# 应用程序,需要从名为 SMS 的 Excel 工作表中获取数据。我尝试了下面的代码,但它给了我“加载 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;
            }




        }
    }


}

【问题讨论】:

标签: c# excel-2007


【解决方案1】:

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

【讨论】:

  • 是的,我的工作表名称是 SMS,工作表名称中没有空格。例外情况是 Microsoft.ACE.OLEDB.12.0 未在本地计算机上注册。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多