当前位置: 代码迷 >> ASP.NET >> Excel 导入到DataSet,该如何处理
  详细解决方案

Excel 导入到DataSet,该如何处理

热度:1388   发布时间:2013-02-25 00:00:00.0
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());}
  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!
 
  • 最近登录:Tue Apr 24 14:43:38 CST 2018
  • 最近登录:Tue Apr 24 14:43:38 CST 2018
  • 最近登录:Tue Apr 24 14:43:38 CST 2018
  • 最近登录:Tue Apr 24 14:43:38 CST 2018
  • 最近登录:Tue Apr 24 14:43:38 CST 2018