/// 功能:读取Excel文件,返一个DataTable
/// 日期:2004-10-20
/// </summary>
/// <param name="FileName">文件</param>
/// <param name="Rows">第一列是否为列名,一般为0或1</param>
/// <returns></returns>
[STAThread]
public static DataTable ReadEcxel(string FileName,int Rows)
{
DataTable dt = new DataTable();
// 获取文件绝对路径
string _filePath = FileName;
// 如果Excel中的第一列为栏名,则写成
string sConnectionString = "";
if (Rows == 1)
{
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+_filePath+";Extended Properties=/"Excel 8.0;HDR=YES/"";
}
else
{
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+_filePath+";Extended Properties=/"Excel 8.0;HDR=NO/"";
}
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = sConnectionString;
cn.Open();
string strSQL = "SELECT * FROM [Sheet1$]";
System.Data.OleDb.OleDbDataAdapter da=new System.Data.OleDb.OleDbDataAdapter(strSQL,cn);
da.Fill(dt);
cn.Close();
return dt;
/*
用DataReader方式!
OleDbCommand cmd = cn.CreateCommand();
cmd.CommandText = sSQL;
OleDbDataReader DR = cmd.ExecuteReader();
while(DR.Read())
{
Console.WriteLine(DR[0].ToString());
}
*/
}
/////
导文件
BULK INSERT dbo.TABLE1
FROM 'F:/wwwroot/WebSite2/1.txt' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '/n')
/// 日期:2004-10-20
/// </summary>
/// <param name="FileName">文件</param>
/// <param name="Rows">第一列是否为列名,一般为0或1</param>
/// <returns></returns>
[STAThread]
public static DataTable ReadEcxel(string FileName,int Rows)
{
DataTable dt = new DataTable();
// 获取文件绝对路径
string _filePath = FileName;
// 如果Excel中的第一列为栏名,则写成
string sConnectionString = "";
if (Rows == 1)
{
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+_filePath+";Extended Properties=/"Excel 8.0;HDR=YES/"";
}
else
{
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+_filePath+";Extended Properties=/"Excel 8.0;HDR=NO/"";
}
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = sConnectionString;
cn.Open();
string strSQL = "SELECT * FROM [Sheet1$]";
System.Data.OleDb.OleDbDataAdapter da=new System.Data.OleDb.OleDbDataAdapter(strSQL,cn);
da.Fill(dt);
cn.Close();
return dt;
/*
用DataReader方式!
OleDbCommand cmd = cn.CreateCommand();
cmd.CommandText = sSQL;
OleDbDataReader DR = cmd.ExecuteReader();
while(DR.Read())
{
Console.WriteLine(DR[0].ToString());
}
*/
}
/////
导文件
BULK INSERT dbo.TABLE1
FROM 'F:/wwwroot/WebSite2/1.txt' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '/n')