Thursday, 17 November 2016

Excel upload using Excel.4.5 and ICSharpCode.SharpZipLib dll

//upload excel file
string FileName = Path.GetFileName(filestudent.PostedFile.FileName);
string Extension = Path.GetExtension(filestudent.PostedFile.FileName);
string FilePath= Server.MapPath("~/Supervisor/" + Convert.ToString(Session["MinistryAccountId"])            
                          + DateTime.Now.ToString("ddMMyy") + Extension);
filestudent.SaveAs(filepath);      

Exceltodatatable(FilePath,Extension)



  public DataTable Exceltodatatable(string FilePath, string Extension)
        {
            FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader;
            if (Extension.ToLower() == ".xls")
                excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
            else
                excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            excelReader.IsFirstRowAsColumnNames = true;
            DataSet result = excelReader.AsDataSet();
            return result.Tables[0];
        }

No comments:

Post a Comment