C# 读取上传的Excel文件而不保存

C# 读取上传的Excel文件而不保存,c#,excel,file,C#,Excel,File,在这段代码中,我从用户那里获得上传的文件,并将其保存在我的应用程序的文件夹中,然后将OLEDBConnection连接到此Excel文件并读取数据。我的问题是-是否有人能建议一种方法,最好是读取此excel文件,但不保存它,因为在我的情况下,使用数据填充数据表 if (Request != null) { HttpPostedFileBase file = Request.Files[0]; if ((file != null) &&am

在这段代码中,我从用户那里获得上传的文件,并将其保存在我的应用程序的文件夹中,然后将OLEDBConnection连接到此Excel文件并读取数据。我的问题是-是否有人能建议一种方法,最好是读取此excel文件,但不保存它,因为在我的情况下,使用数据填充数据表

   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")
          {
            string fileLocation = Server.MapPath("~/Content/") + Request.Files[0].FileName;
            if (System.IO.File.Exists(fileLocation))
            {

              System.IO.File.Delete(fileLocation);
            }
            Request.Files[0].SaveAs(fileLocation);
            string excelConnectionString = string.Empty;
            excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
            fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            //connection String for xls file format.
            if (fileExtension == ".xls")
            {
              excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
              fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            //connection String for xlsx file format.
            else if (fileExtension == ".xlsx")
            {
              excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
              fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }
            //Create Connection to Excel work book and add oledb namespace
            OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
            excelConnection.Open();

            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", excelConnection);
            OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            objAdapter1.Fill(ds);

            DataTable Dt = ds.Tables[0];
看看这个图书馆。

编辑 例如:

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];
看看这个图书馆。

编辑 例如:

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];
这条线

HttpPostedFileBase file = Request.Files[0];
假设无法将HttpPostedFile转换为HttpPostedFileBase

HttpPostedFileBase file = Request.Files[0];
假设无法将HttpPostedFile转换为HttpPostedFileBase