问题描述
publicvoidOutputExcel(){stringfilePath=@"C:WindowsApplication11.xls";stringstrConn=@"Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+filePath+";ExtendedProperties=Excel8.0";OleDbConnectionmyConn=newOleDbConnection();myConn.ConnectionString=strConn;stringstrComm=@"SELECT*FROM[Sheet1$]";//提示出错,找不到Sheet1?为什么会这样呢?OleDbCommandmyComm=newOleDbCommand(strComm,myConn);OleDbDataAdaptermyAdapter=newOleDbDataAdapter(myComm);DataSetds=newDataSet();myAdapter.Fill(ds);foreach(stringstrinds.Tables[0].Rows){MyLog(str);}}publicstaticvoidMyLog(stringmessage){FileStreamaFlie=newFileStream(@"C:WindowsApplication11",FileMode.Append);StreamWritersw=newStreamWriter(aFlie);sw.Write(message);sw.Close();}
解决方案
解决方案二:
你Excel中有Sheet1$这个页表吗你可以通过读取的数据的结构查看一下。DataTabletables=myConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,newobject[]{null,null,null,"table"});foreach(DataRowrowintables.Rows){MessageBox.Show(row["table_name"].ToString());}