在c语言中从excel工作表中读取数据的查询

在c语言中从excel工作表中读取数据的查询,excel,c#-4.0,Excel,C# 4.0,感谢阿斯坦德回答我的问题 我在这里有更详细的问题 string cs = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + @"D:\\sample.xls;" + "Excel 12.0;HDR=YES;"; OleDbConnection Excelcon = new OleDbConnection(cs); OleDbDataAdapter ad = new OleDbDataAd

感谢阿斯坦德回答我的问题

我在这里有更详细的问题

        string cs = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + @"D:\\sample.xls;" + "Excel 12.0;HDR=YES;";
        OleDbConnection Excelcon = new OleDbConnection(cs);
        OleDbDataAdapter ad = new OleDbDataAdapter();
        ad.SelectCommand = new OleDbCommand("SELECT *FROM [Sheet1$]", Excelcon);
        DataTable dt = new DataTable();
        ad.Fill(dt);
        return dt;
我在select语句中发现错误:

Microsoft Office Access数据库引擎找不到对象“Sheet1$”。确保对象存在,并且正确拼写其名称和路径名

希望有人能帮我找到解决办法。

对我有效的是, 创建文件时,它存储在某个特定位置。在我的例子中,C:/Documents

我已手动将位置更改为D: 这就是我写的

字符串connStringExcel=@Provider=Microsoft.ACE.OLEDB.12.0;数据源=D:\example.xls;扩展属性=Excel 12.0;HDR=是`

因此,实际路径应该是

字符串connStringExcel=@Provider=Microsoft.ACE.OLEDB.12.0;数据源=C:\A\Documents\example.xls;扩展属性=Excel 12.0;HDR=是`

因此,在给出正确位置的路径后,我的问题就解决了

希望它也能帮助其他人。

对我有用的是,
// Create connection string variable. Modify the "Data Source"
// parameter as appropriate for your environment.
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("../ExcelData.xls") + ";" +
"Extended Properties=Excel 8.0;";

// Create connection object by using the preceding connection string.
OleDbConnection objConn = new OleDbConnection(sConnectionString);

// Open connection with the database.
objConn.Open();

// The code to follow uses a SQL SELECT command to display the data from the worksheet.

// Create new OleDbCommand to return data from worksheet.
OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM myRange1", objConn);

// Create new OleDbDataAdapter that is used to build a DataSet
// based on the preceding SQL SELECT statement.
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

// Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect;

// Create new DataSet to hold information from the worksheet.
DataSet objDataset1 = new DataSet();

// Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1, "XLData");

// Bind data to DataGrid control.
DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
DataGrid1.DataBind();

// Clean up objects.
objConn.Close();
创建文件时,它存储在某个特定位置。在我的例子中,C:/Documents

我已手动将位置更改为D: 这就是我写的

字符串connStringExcel=@Provider=Microsoft.ACE.OLEDB.12.0;数据源=D:\example.xls;扩展属性=Excel 12.0;HDR=是`

因此,实际路径应该是

字符串connStringExcel=@Provider=Microsoft.ACE.OLEDB.12.0;数据源=C:\A\Documents\example.xls;扩展属性=Excel 12.0;HDR=是`

因此,在给出正确位置的路径后,我的问题就解决了

希望它也能帮助别人

// Create connection string variable. Modify the "Data Source"
// parameter as appropriate for your environment.
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("../ExcelData.xls") + ";" +
"Extended Properties=Excel 8.0;";

// Create connection object by using the preceding connection string.
OleDbConnection objConn = new OleDbConnection(sConnectionString);

// Open connection with the database.
objConn.Open();

// The code to follow uses a SQL SELECT command to display the data from the worksheet.

// Create new OleDbCommand to return data from worksheet.
OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM myRange1", objConn);

// Create new OleDbDataAdapter that is used to build a DataSet
// based on the preceding SQL SELECT statement.
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

// Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect;

// Create new DataSet to hold information from the worksheet.
DataSet objDataset1 = new DataSet();

// Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1, "XLData");

// Bind data to DataGrid control.
DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
DataGrid1.DataBind();

// Clean up objects.
objConn.Close();
参考


参考

你能给我们看一下实际代码吗?Thanq Astander。我现在有一个更详细的查询。希望你能检查一下。你能给我们看一下实际代码吗?Thanq Astander。我现在有一个更详细的查询。希望你能检查一下。