Do I need to install Microsoft Office on a server to import Excel into Asp.net?

Do I need to install Microsoft Office on the server to run the application to import data from the excel file into the mssql database?

any suggestions or ideas?

code used by me

public partial class _Default : System.Web.UI.Page
{
private String strConnection = "Data Source=MYCBJ017550027;Initial Catalog=MySamplesDB;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSend_Click(object sender, EventArgs e)
{
string path = fileuploadExcel.PostedFile.FileName;
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);
OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]",excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
sqlBulk.DestinationTableName = "Excel_table";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
}
}
+5
source share
3 answers

If you only read xls files, use Microsoft.Jet.OLEDB.4.0that which is built into your .net infrastructure.

If you are reading xlsx files, use Microsoft.ACE.OLEDB.12.0. Drivers for this can be downloaded freely from the Microsoft website. You do not need to install Microsoft for interaction.

Use the following connection string

    string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;  
Data Source=" + path + ";Extended Properties=Excel 12.0;HDR=YES";

Download drivers here

Refer to this example to run

+8
source

@Romil, .NET :

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();
}
+2

, ( ) . excel, , -.

I have listed a few here. I used excelreader a lot with good results.

http://excelreader.codeplex.com/
http://epplus.codeplex.com/

The Excel reader seems to easily cover the documentation. so here is an example of how to use it

IExcelDataReader reader = null;
try
{
    using (FileStream stream = new FileStream(ofd.FileName, FileMode.Open))
    {
        string ext = System.IO.Path.GetExtension(ofd.FileName).Replace(".", "").ToUpper();
        if (ext == "XLS")
            reader = ExcelReaderFactory.CreateBinaryReader(stream);
        else
            reader = ExcelReaderFactory.CreateOpenXmlReader(stream);

        reader.IsFirstRowAsColumnNames = true;
        using (DataSet ds = reader.AsDataSet())
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ImportData toAdd = new ImportData()
                    {
                        Format = dr[0].ToString(),
                    };

                Database.Datastore.InsertObject(toAdd);
            }
        }
    }
}
0
source

All Articles