C#如何把某个文件夹下的所有Excel文件导入Oracle数据库。详细点!
展开全部
用下面这个方法把excel导入dataview,再把它导入database就简单瞎基铅了
public DataSet ReadExcel_ds(string FilePath)
{
string subfile = FilePath.Substring(FilePath.LastIndexOf(".") + 1);
DataSet ds = new DataSet();
string strCon = "";
if (subfile.ToUpper() == "XLS")
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" + FilePath;
}
if (subfile.ToUpper() == "XLSX")//锋郑excel2007读取
{
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
}
System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
try
{
Conn.Open();
System.Data.DataTable sTable = Conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);//新添加 2010-10-14
string tableName = sTable.Rows[0][2].ToString().Trim();//新添加 2010-10-14 获取工作表名称
string strCom = "SELECT * FROM [" + tableName + "] ";
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
myCommand.Fill(ds, "[" + tableName + "磨好]");
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
finally
{
Conn.Close();
}
return ds;
}
public DataSet ReadExcel_ds(string FilePath)
{
string subfile = FilePath.Substring(FilePath.LastIndexOf(".") + 1);
DataSet ds = new DataSet();
string strCon = "";
if (subfile.ToUpper() == "XLS")
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" + FilePath;
}
if (subfile.ToUpper() == "XLSX")//锋郑excel2007读取
{
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
}
System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
try
{
Conn.Open();
System.Data.DataTable sTable = Conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);//新添加 2010-10-14
string tableName = sTable.Rows[0][2].ToString().Trim();//新添加 2010-10-14 获取工作表名称
string strCom = "SELECT * FROM [" + tableName + "] ";
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
myCommand.Fill(ds, "[" + tableName + "磨好]");
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
finally
{
Conn.Close();
}
return ds;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询