62,046
社区成员
发帖
与我相关
我的任务
分享
public DataSet Execl()
{
DataTable dt=GetSchemaTableName();
ArrayList list = new ArrayList();
foreach (DataRow item in dt.Rows)
{
list.Add(item["TABLE_NAME"].ToString());
}
DataSet Myds = new DataSet();
OleCon.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
OleCon.Open();
OleCmd.CommandText = "select * from [" + list[0] + "]";
OleCmd.Connection = OleCon;
OleDa.SelectCommand = OleCmd;
//DataTable dt = new DataTable();
try
{
//OleCon.ConnectionString = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + FileName + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
OleDa.Fill(Myds, "Execl");
return Myds;
//OleCon.Open();
//dt = OleCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
//return dt;
}
catch(Exception e)
{
return Myds;
//return dt;
}
finally
{
OleCon.Close();
OleCmd.Dispose();
OleDa.Dispose();
OleCon.Dispose();
}
//2007或者2010版本的Office
//string conn_str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + "; Extended Properties=Excel 12.0;";
//2003的Office
string conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
Public DataSet ds(){
string type2 = fu_fileuplaod.FileName;
var type3 = type2.Substring(type2.LastIndexOf('.'));
if (!type3.Equals(".xls") && !type3.Equals(".xlsx"))
{
RegisterScriptAlert("上传excel文档的格式与所要求的格式不一致");
return;
}
string newName = Server.MapPath("..//App_Data//Uploads//") + DateTime.Now.ToString("hhmmss") + ".xls";
fu_fileuplaod.SaveAs(newName);
string connStr = string.Empty;
if (type3.Equals(".xlsx"))
{
connStr = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + newName +
";Extended Properties='Excel 12.0 Xml; HDR=YES; IMEX=1'";
}
else if (type3.Equals(".xls"))
{
connStr = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + newName +
";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
}
OleDbConnection conn = new OleDbConnection(connStr);
if (conn.State.ToString() == "Closed")
{
conn.Open();
}
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
ArrayList list = new ArrayList();
foreach (DataRow item in dt.Rows)
{
list.Add(item["TABLE_NAME"].ToString());
}
var str11 = "select * from [" + list[0] + "]";
OleDbDataAdapter oda = new OleDbDataAdapter(str11, conn);
DataSet ds = new DataSet();
oda.Fill(ds);
conn.Close();
File.Delete(newName)
}