使用Kendo UI上传Excel文件

时间:2015-10-26 09:52:41

标签: excel kendo-asp.net-mvc

当我尝试使用kendo ui上传excel文件时,我在互联网上找到了一个代码。它使用了一个名为" Constants"的关键字。但是这个关键字无法识别" .xls"文件扩展名。我坚持这个并做了一些研究,但没有解决这个问题的答案。这是我的代码:

public ActionResult Submit(IEnumerable<HttpPostedFileBase> files)
        {
           if(files!= null)
           {
               string fileName;
               string filePath;
               string fileExtension;
               foreach(var f in files)
               {
                   //Set file details
                   SetFileDetails(f, out fileName, out filePath, out fileExtension);

                   if(fileExtension == Constants.xls || fileExtension == Constants.xlsx)
                   {
                       //Save the uploaded file to app folder
                       string savedExcelFiles = Constants.UploadedFolder + fileName;
                       f.SaveAs(Server.MapPath(savedExcelFiles));

                       ReadDataFromExcelFiles(savedExcelFiles);
                   }
                   else
                   {
                       //file not supported send alert
                   }

               }
           }
           return RedirectToActionPermanent("Index","Connect");
        }

        private static void SetFileDetails(HttpPostedFileBase f,out string fileName,out string filePath,out string fileExtension)
        {
            fileName=Path.GetFileName(f.FileName);
            fileExtension=Path.GetExtension(f.FileName);
            filePath = Path.GetFullPath(f.FileName);
        }

        private void ReadDataFromExcelFiles(string savedExcelFiles)
        {
            var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;",Server.MapPath(savedExcelFiles));

            //fill the DataSet by the sheets
            var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]",connectionString);
            var ds = new DataSet();
            List<UploadExcel> uploadExl = new List<UploadExcel>();

            adapter.Fill(ds,"Subscriber");
            DataTable data=ds.Tables["Subscriber"];

            GetSetUploadExcelData(uploadExl,data);
        }

        private static void GetSetUploadExcelData (List<UploadExcel> uploadExl,DataTable data)
        {
            for(int i=0;i<data.Rows.Count-1;i++)
            {
                UploadExcel NewUpload = new UploadExcel();
                NewUpload.ID = Convert.ToInt16(data.Rows[i]["ID"]);
                NewUpload.CostCenter = Convert.ToString(data.Rows[i]["CostCenter"]);
                NewUpload.FirstName = Convert.ToString(data.Rows[i]["FirstName"]);
                NewUpload.LastName = Convert.ToString(data.Rows[i]["LastName"]);

                NewUpload.MobileNo = Convert.ToString(data.Rows[i]["MobileNo"]);
                NewUpload.EmailID = Convert.ToString(data.Rows[i]["EmailID"]);
                NewUpload.Services = Convert.ToString(data.Rows[i]["Services"]);

                NewUpload.UsageType = Convert.ToString(data.Rows[i]["UsageType"]);
                NewUpload.Network = Convert.ToString(data.Rows[i]["Network"]);
                NewUpload.UsageIncluded = Convert.ToInt16(data.Rows[i]["UsageIncluded"]);
                NewUpload.Unit = Convert.ToString(data.Rows[i]["Unit"]);

                uploadExl.Add(NewUpload);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我怀疑Constants.xls与原始代码作者用于保存.xls / .xlsx扩展名的静态classenum相关。

如果你创建一个常量类,如:

public static class Constants
{
    public static string xls = "xls";
    public static string xlsx = "xlsx";
}

这将有所帮助。

如果您需要更多帮助,请告知我们。

编辑:只是查看代码,看起来他们也在为uploadfolder位置提供常量映射,所以我怀疑这只是一个静态类而不是具有应用程序特定细节的枚举。在某种程度上有点像在webconfig中使用appSettings