在DataGridView C#中显示excel工作簿的值

时间:2017-01-23 13:26:01

标签: c# excel datagridview

我需要一些帮助才能在datagridview中显示excel的值。 我设法显示值,但缺少一些值(列和行的值).i在我的Excel文件中有1000行,数据网格视图只显示其中的333个项目。我有148个列,但它只显示一些。谁能告诉我问题是什么。

这是我的代码:

     public partial class MainBagsakan : Form
     [enter image description here][1]
      String WOmain=@"C:\Users\tjjtabije\Desktop\TestExcelUpdater\TestUnoREFARM.xlsx";
      private string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\t-jjtabije\\Desktop\\TestExcelUpdater\\TestUnoREFARM.xlsx;Extended Properties='Excel 12.0 Xml;IMEX=1;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text'";
      private void WorkOrderTab()
     {

        string filePath = Path.GetFullPath(WOmain);
        string extension = Path.GetExtension(filePath);
        string conStr, sheetName;
        conStr = string.Empty;

        //Get the name of the First Sheet.
        using (OleDbConnection con = new OleDbConnection(Excel07ConString))
        {
            using (OleDbCommand cmd = new OleDbCommand())
            {
                cmd.Connection = con;
                con.Open();
                DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                con.Close();
            }
        }
        using (OleDbConnection con = new OleDbConnection(Excel07ConString))
        {
            using (OleDbCommand cmd = new OleDbCommand())
            {
                using (OleDbDataAdapter oda = new OleDbDataAdapter())
                {
                    DataTable dt = new DataTable();
                    cmd.Connection = con;
                    cmd.CommandText = "SELECT * [" +sheetName+ "]";
                    con.Open();
                    oda.SelectCommand = cmd;
                    oda.Fill(dt);
                    con.Close();

                    //Populate DataGridView.
                    WorkLoadDisp.DataSource = dt;
                    label1.Text = dt.Rows.Count.ToString();
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

没有必要像使用两个单独的using子句一样打开和关闭连接两次。第一个只是获取工作表类型的名称,因为命名范围也可以返回。我只是将所有代码放入一个组中,它似乎按预期工作。

在select语句中添加了缺少的FROM

cmd.CommandText = "SELECT * FROM [" + sheetName + "]";

我所做的更改低于......

using (OleDbConnection con = new OleDbConnection(Excel07ConString)) {
  using (OleDbCommand cmd = new OleDbCommand()) {
    using (OleDbDataAdapter oda = new OleDbDataAdapter()) {
      cmd.Connection = con;
      con.Open();
      DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
      sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
      DataTable dt = new DataTable(); 
      cmd.Connection = con;
      cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
      //con.Open();
      oda.SelectCommand = cmd;
      oda.Fill(dt);
      con.Close();
      //Populate DataGridView.
      WorkLoadDisp.DataSource = dt;
      label1.Text = dt.Rows.Count.ToString();
    }
  }
}

希望这有帮助。