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

前端 未结 3 1572
遥遥无期
遥遥无期 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:51

    The problem with jet, is you still have to install the data provider for it (though this is also free) or have office installed for it to work. If you simply need to read an excel file there are plenty of fully managed libs out there that will do the track just fine without the need to install anything.

    I've listed a few here. I have used excelreader plenty with good results.

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

    Excel reader seem to be light on the documentation side of things. 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);
                }
            }
        }
    }
    

提交回复
热议问题