日期:2014-05-18  浏览次数:20583 次

Excel 导入到DataSet
把一个Excel导入到Dataset里
这个Excel的前几个记录是1,2,3 后面是A,B,C但是每次数据类型都默认成double类型,也就是每次只可以导入前三个后面几个是空

连接字符串里加了 IMEX=1 郁闷
有知道请指教 谢谢 

string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + sExcelPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";

------解决方案--------------------
C# code
OleDbConnection objConn = null;
DataSet data = new DataSet();
try
{
string strConn = @"Provider=Microsoft.ACE.OleDb.12.0;Data Source=c:"待发工资.xlsx;Extended Properties='Excel 12.0;HDR=YES'";//IMEX=1为只读
//"Provider=Microsoft.Jet.OleDb.4.0;data source=c:"待发工资.xlsx;Extended Properties=""Excel 12.0;HDR=YES;IMEX=1""";
                objConn = new OleDbConnection(strConn);
objConn.Open();
//System .Data . DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
//int SheetIndex = 0;
//string tableName = schemaTable.Rows[SheetIndex][2].ToString().Trim();

string strSql = "Select   *   From   [Sheet1$]";
OleDbCommand objCmd = new OleDbCommand(strSql, objConn);
OleDbDataAdapter sqlada = new OleDbDataAdapter();
sqlada.SelectCommand = objCmd;
sqlada.Fill(data);
string str = "";
for (int i = 0; i < data.Tables[0].Rows[0].ItemArray.Length; i++)
{
str += data.Tables[0].Rows[0].ItemArray[i].ToString() + ".";
}
MessageBox.Show(str);
objConn.Close();
}
catch (Exception ex)
{
objConn.Close();
Console.WriteLine(ex.Message.ToString());
}