Do we need to install Microsoft office in server for Excel import in Asp.net?

前端 未结 3 1560
遥遥无期
遥遥无期 2020-12-16 06:08

do we need to install Microsoft office in server to run a application to import data from excel file to mssql database ?

any suggestions or ideas ?

the code

3条回答
  •  再見小時候
    2020-12-16 06:50

    As @Romil said you can use the .NET framework for that:

    string ConnectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Extended Properties=""Excel 8.0;HDR=Yes"";", fileName);
    
    using (OleDbConnection conn = new OleDbConnection(ConnectionString))
    {
         conn.Open();
         DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
    
         foreach (DataRow schemaRow in schemaTable.Rows)
         {
              string sheet = schemaRow["TABLE_NAME"].ToString();
              using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn))
              {
                   cmd.CommandType = CommandType.Text;
                   DataTable outputTable = new DataTable(sheet);
                   output.Tables.Add(outputTable);
                   new OleDbDataAdapter(cmd).Fill(outputTable);
              }
         }
                    conn.Close();
    }
    

提交回复
热议问题