Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 将Excel工作表导入MS SQL数据库_Asp.net_Excel - Fatal编程技术网

Asp.net 将Excel工作表导入MS SQL数据库

Asp.net 将Excel工作表导入MS SQL数据库,asp.net,excel,Asp.net,Excel,我有Excel 2007和Visual Web Developer Express 2010。 我想导入xlsx文件的Sheet1,然后读取器将数据添加到数据集,并将该数据放置在MS SQL数据库中 string ExcelConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES";

我有Excel 2007和Visual Web Developer Express 2010。 我想导入xlsx文件的Sheet1,然后读取器将数据添加到数据集,并将该数据放置在MS SQL数据库中

    string ExcelConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES";
    string SQLConStr = "got connection string that works";

    OleDbConnection ExcelConnection = new OleDbConnection(ExcelConStr);
    using (ExcelConnection)
    {
        string sql = string.Format("Select * FROM [{0}]", "Sheet1$");
        OleDbCommand command = new OleDbCommand(sql, ExcelConnection);
        ExcelConnection.Open();
        using (OleDbDataReader dr = command.ExecuteReader())
        {
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SQLConStr))
            {
                bulkCopy.DestinationTableName = "dbo.databaseName";            
                bulkCopy.WriteToServer(dr);
            }
        }
    }

如果有人提出建议,我需要像bulkcopy这样的免费且易于使用的东西。

如果没有bulkcopy,它也可以工作。。试试这个,它将100%适用于.csv和.xlsx。。。我正在用它

    string header = "No";
    string sql = string.Empty;
    DataTable dt = new DataTable();
    string pathOnly = string.Empty;
    string fileName = string.Empty;

    pathOnly = Path.GetDirectoryName(path);
    fileName = Path.GetFileName(path);

    if (IsFirstRowHeader) { header = "Yes"; }     

String cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=" + header + ";IMEX=1;\""; 

  OleDbConnection con = new OleDbConnection(cs);
  if (con.State == ConnectionState.Closed) con.Open();

        #region use to find sheet and fire query on sheetname
        DataTable dtsheets = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

        String[] excelSheets = new String[dtsheets.Rows.Count];
        int i = 0;
        // Add the sheet name to the string array.
        foreach (DataRow row in dtsheets.Rows)
        {
            excelSheets[i] = row["TABLE_NAME"].ToString();
            i++;
        }
        if (extension == ".csv") sql = "SELECT  * FROM [" + fileName + "]";
        else sql = "SELECT  * FROM [" + excelSheets[0] + "]";
        #endregion

  OleDbCommand command = new OleDbCommand(sql, con);
  OleDbDataAdapter adapter = new OleDbDataAdapter(command);
  dt.Locale = CultureInfo.CurrentCulture;
  adapter.Fill(dt);
  con.Close();

如果没有批量复制,它也可以工作。。试试这个,它将100%适用于.csv和.xlsx。。。我正在用它

    string header = "No";
    string sql = string.Empty;
    DataTable dt = new DataTable();
    string pathOnly = string.Empty;
    string fileName = string.Empty;

    pathOnly = Path.GetDirectoryName(path);
    fileName = Path.GetFileName(path);

    if (IsFirstRowHeader) { header = "Yes"; }     

String cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=" + header + ";IMEX=1;\""; 

  OleDbConnection con = new OleDbConnection(cs);
  if (con.State == ConnectionState.Closed) con.Open();

        #region use to find sheet and fire query on sheetname
        DataTable dtsheets = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

        String[] excelSheets = new String[dtsheets.Rows.Count];
        int i = 0;
        // Add the sheet name to the string array.
        foreach (DataRow row in dtsheets.Rows)
        {
            excelSheets[i] = row["TABLE_NAME"].ToString();
            i++;
        }
        if (extension == ".csv") sql = "SELECT  * FROM [" + fileName + "]";
        else sql = "SELECT  * FROM [" + excelSheets[0] + "]";
        #endregion

  OleDbCommand command = new OleDbCommand(sql, con);
  OleDbDataAdapter adapter = new OleDbDataAdapter(command);
  dt.Locale = CultureInfo.CurrentCulture;
  adapter.Fill(dt);
  con.Close();