日期:2014-05-17 浏览次数:20458 次
/// <summary>
/// 连接Excel
/// </summary>
/// <returns></returns>
private OleDbConnection excelConn(string path)
{
OleDbConnection Conn = null;
string excelConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path.ToString().Trim() + ";" + "Extended Properties='Excel 8.0;hdr=no;IMEX=1'";
Conn = new OleDbConnection(excelConn);
return Conn;
}
public string ReadExcelToTempTable(string FName)
{
OleDbConnection connExcel = excelConn(FName);
string Error = "";
try
{
DataTable dt = new DataTable();
connExcel.Open();
string strCom = " SELECT * FROM [" + tableName + "]";///SQL操作语句,就是说:取得所有数据从Content
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, connExcel);
DataSet myDataSet = new DataSet();///建立新的数据集myDataSet
myCommand.Fill(myDataSet);///填充数据集
dt = myDataSet.Tables[0];//Exceltable
return Error;
}
catch (System.Exception e)
{
return e.ToString();
&