0

我正在尝试将excel文件中的一列导入datagridview中的一列..现在可以通过将所有数据选择到datagridview来完成导入的想法,但是我如何才能从excel文件中仅选择一列到datatgridview中的一列之后比较它们中的所有数据并确保它们相等?我的意思是是否有任何语句可以从 excel 文件中选择此列到 datagridview 中的其他特定列中,其中它们中的所有数据都相等?

谢谢大家

注意:我不知道这个想法是否清楚,但您可以在需要更多说明时问我

4

1 回答 1

0

这里有一些你也可以尝试让初学者搞砸的东西

如果您需要一个使用 Ranges 的示例..请告诉我,我也可以发布一个示例,但这应该足以让您入门。更改代码 Sorce= 以匹配 Excel 文件的文件路径

using System.Data;
using System.Data.OleDb;
...
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0");
OleDbDataAdapter da = new OleDbDataAdapter("select * from MyObject", con);
DataTable dt = new DataTable();
da.Fill(dt);

如果您想了解如何阅读 excel 文件的完整示例,这里是另一个示例

以下是读取 Excel 文件所需的全部代码。

void Read_My_Excel_File()
{

// Test.xls is in the C:\

string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + C:\Test.xls + ";";

connectionString += "Extended Properties=Excel 8.0;";

// always read from the sheet1.
OleDbCommand myCommand = new OleDbCommand("Select * from [Sheet1$];");
OleDbConnection myConnection = new OleDbConnection(connectionString);
myConnection.Open();
myCommand.Connection = myConnection;
OleDbDataReader myReader = myCommand.ExecuteReader();

while (myReader.Read())
{

// it can read upto 5 columns means A to E. In your case if the requirement is different then change the loop limits a/c to it.

for (int i = 0; i < 5; i++)
{
  Response.Write(myReaderIdea.ToString() + " ");
}
Response.Write("<br>");
}
myConnection.Close();
}
于 2012-08-14T23:45:32.647 回答