public class ImportExcel
...{
導入xls數據#region 導入xls數據
//從Excel導入
public DataSet GetXsl(string FileName)
...{
string ext = FileName.Substring(FileName.LastIndexOf(".") + 1);
DataSet data = new DataSet();
if (ext.ToLower().Equals("xls"))
...{
OleDbConnection objConn = null;
try
...{
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FileName + ";Extended PropertIEs=Excel 8.0";
objConn = new OleDbConnection(sConnectionString);
objConn.Open();
//DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
//string tableName = schemaTable.Rows[0][2].ToString().Trim();
string strSql = "Select * From [Sheet1$]";
OleDbCommand objCmd = new OleDbCommand(strSql, objConn);
OleDbDataAdapter sqlada = new OleDbDataAdapter();
sqlada.SelectCommand = objCmd;
sqlada.Fill(data, "UserInfo");
objConn.Close();
//File.Delete(FileName);//是否保存上傳的exl
return data;
}
catch (OleDbException ex)
...{
throw ex;
&n
finally
...{
objConn.Close();
}
}
return data;
}