private DataSet GetSetInfo(string type, string Path)
{
string SqlString = "";
if (type == ".xls")//导入格式
{
SqlString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", Path); //2003版本Excel
}
else
{
SqlString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", Path); //2007版本Excel
}
try
{
OleDbConnection cnnxls = new OleDbConnection(SqlString); //开始读取
OleDbDataAdapter myDa = null;
myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls); //读取表名为Sheet1
DataSet ds = new DataSet();
myDa.Fill(ds, "TableInfo"); //将读取到的表数据添加到DataSet中
return ds;
}
catch
{
MessageBox.Show("操作错误!");
return null;
}
}
评论列表: