日期:2014-05-18  浏览次数:21148 次

关于excel导入sql数据库的问题
public DataSet ImportFromExcel(string filePath)
{
DataSet ds = new DataSet();
string connString = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
DataTable table = OleDbHelper.GetExcelTables(connString);
if(table == null || table.Rows.Count <= 0)
{
return null;
}

foreach(DataRow dr in table.Rows)
{
string cmdText = "select * from [" + dr["TABLE_NAME"].ToString() + "]";
DataTable dt = OleDbHelper.FillDataTable(connString, cmdText);
dt.TableName = dr["TABLE_NAME"].ToString();
ds.Tables.Add(dt);
}

return ds;
}
网上大家给的代码一半以上都是和这相似的,我想问的是这里连接字符串中数据库类型是ACCESS,但数据源文件是excel文件这能直接查询操作吗,这样不是将excel文件当数据库文件操作了吗,这是我的理解,求高手讲解,另外我想将excel文件导入到sqlserver 2005中应该怎样修改这段代码呢??



------解决方案--------------------
C# code

public static DataTable CreateExcelDataSource(string url)
        {
            DataTable dt = null;
            string connetionStr = "Provider=Microsoft.Ace.OleDb.12.0;" + "Data Source=" + url + ";" + "Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';";
            string strSql = "select * from [Sheet1$]";
            OleDbConnection oleConn = new OleDbConnection(connetionStr);
            OleDbDataAdapter oleAdapter = new OleDbDataAdapter(strSql, connetionStr);
            try
            {
                dt = new DataTable(); 
                oleAdapter.Fill(dt);

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                oleAdapter.Dispose();
                oleConn.Close();
                oleConn.Dispose();
            }
        }

------解决方案--------------------
楼主的代码好非主流。。。
顶楼上的,只是楼上没考虑到excel表中第一张表名,不一定为sheet
------解决方案--------------------
这个是我代码使用是成功的,给你了,有问题我们在联系 ,qq497950652

public DataSet ExcelToDS(string filePath, string tableName)
{
// string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
//"HDR=Yes;" 这个参数说明第一行是列名,而不是数据. "HDR=No;"正好与前面的相反。 
//"IMEX=1;" 告诉driver总是把数据作为text 类型.注意,这选项会影响excel的写访问(sheet write access negative)。

string fileExtension = Path.GetExtension(filePath);
String strConn="";
if (fileExtension.ToLower() == ".xls")
strConn = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
else if (fileExtension.ToLower() == ".xlsx")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES; IMEX=1\"";
else if (fileExtension.ToLower() == ".xlsb")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + filePath + ";Extended Properties=\"Excel 12.0;HDR=YES ; IMEX=1\"";
else if (fileExtension.ToLower() == ".xlsm")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Macro;HDR=YES; IMEX=1\"";

OleDbConnection conn = new OleDbConnection(strConn);

string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
try
{
strExcel = string.Format(&qu