Reading uploaded Excel file without saving it

前端 未结 2 499
醉酒成梦
醉酒成梦 2021-01-27 19:48

In this snippet of code - I get the uploaded file from the user and save it in a folder in my app and then make OleDbConmnection to this Excel File and read the data. My questio

相关标签:
2条回答
  • 2021-01-27 20:15

    This line

    HttpPostedFileBase file = Request.Files[0];
    

    Say that can't possible convert HttpPostedFile to HttpPostedFileBase

    0 讨论(0)
  • 2021-01-27 20:17

    See this library. Excel Data Reader

    EDIT example:

    if (Request != null)
    {
       HttpPostedFileBase file = Request.Files[0];
       if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
       {
            string fileName = file.FileName;
            string fileContentType = file.ContentType;
            string fileExtension = System.IO.Path.GetExtension(Request.Files[0].FileName);
    
            if (fileExtension == ".xls" || fileExtension == ".xlsx")
            {
                IExcelDataReader excelReader;
                if (fileExtension == ".xls")
                    excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                else
                    excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
    
                excelReader.IsFirstRowAsColumnNames = true;
                DataSet ds = excelReader.AsDataSet();
    
                DataTable Dt = ds.Tables[0];
    
    0 讨论(0)
提交回复
热议问题