把EXCEL文件当做一个数据源来进行数据的读取 *** 作,实例如下:
public DataSet ExcelToDS(string Path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0" +"Data Source="+ Path +""+"Extended Properties=Excel 8.0"
OleDbConnection conn = new OleDbConnection(strConn)
conn.Open()
string strExcel = ""
OleDbDataAdapter myCommand = null
DataSet ds = null
strExcel="select * from [sheet1$]"
myCommand = new OleDbDataAdapter(strExcel, strConn)
ds = new DataSet()
myCommand.Fill(ds,"table1")
return ds
}
对于EXCEL中的表即sheet([sheet1$])如果不是仔键清固定的可以使用下念前面的亮尺方法得到
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0" +"Data Source="+ Path +""+"Extended Properties=Excel 8.0"
OleDbConnection conn = new OleDbConnection(strConn)
DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,null)
string tableName=schemaTable.Rows[0][2].ToString().Trim()
Sub xxx() Workbooks.Open Application.GetOpenFilename("Excel文件手顷旦(*.xlsx),*.xlsx") ... ActiveWorkbook.Close 1 End Sub ...部分自己增加乎型代码。麻烦毕扰采纳,谢谢!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)