简体   繁体   English

将上传 controller 从 support.xlsx 更改为.csv

[英]Changing upload controller from supporting .xlsx to .csv

I have an MVC application which allows an admin user to upload 2 different excel file onto the system;我有一个 MVC 应用程序,它允许管理员用户将 2 个不同的 excel 文件上传到系统; the controller then creates a dataset with the excel data and then populates either a "Schools" or a "School2" database with the dataset using an SqlBulkCopy. controller 然后使用 excel 数据创建一个数据集,然后使用 SqlBulkCopy 使用该数据集填充“Schools”或“School2”数据库。

These uploads work perfect when I test them locally using IIS Express, although the same version deployed to AWS elastic beanstalk throws an error when I press the import button.当我使用 IIS Express 在本地测试它们时,这些上传工作完美,尽管当我按下导入按钮时,部署到 AWS elastic beanstalk 的相同版本会引发错误。 As far as I am aware, this is due to my AWS RDS needing access to the OleDB provider jet drivers;据我所知,这是因为我的 AWS RDS 需要访问 OleDB 提供商的喷气驱动程序; something which I can not do because these drivers can not just be installed on an AWS RDS like they can be on an EC2 instance.我不能做的事情是因为这些驱动程序不能像安装在 EC2 实例上一样安装在 AWS RDS 上。

So my plan is to change my upload controller around to accept.csv files instead of excel files.所以我的计划是将我上传的 controller 更改为 accept.csv 文件而不是 excel 文件。 This should solve my problem and allow my upload buttons to work after being deployed on AWS.这应该可以解决我的问题,并允许我的上传按钮在部署到 AWS 后工作。 Could someone help me/point me in the right direction to change my controller to support.csv instead of excel please?有人可以帮我/指出我正确的方向,将我的 controller 更改为 support.csv 而不是 excel 吗?

Upload Controller:上传 Controller:

namespace CampBookingSys.Controllers
{
    public class UploadController : Controller
    {
        SqlConnection con = new SqlConnection(@"Data Source=bookingdb.cwln7mwjvxdd.eu-west-1.rds.amazonaws.com,1433;Initial Catalog=modeldb;User ID=craig1990;Password=27Oct90!;Database=modeldb;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");

        OleDbConnection Econ;

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            string filename = Guid.NewGuid() + Path.GetExtension(file.FileName);
            string filepath = "/excelfolder/" + filename;
            file.SaveAs(Path.Combine(Server.MapPath("/excelfolder"), filename));
            InsertExceldata(filepath, filename);
            return View();
        }

        [HttpPost]
        public ActionResult Index2(HttpPostedFileBase file)
        {
            string filename = Guid.NewGuid() + Path.GetExtension(file.FileName);
            string filepath = "/excelfolder/" + filename;
            file.SaveAs(Path.Combine(Server.MapPath("/excelfolder"), filename));
            InsertExceldata2(filepath, filename);
            return RedirectToAction("Index");
        }

        private void ExcelConn(string filepath)
        {
            string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", filepath);
            Econ = new OleDbConnection(constr);
        }

        private void InsertExceldata(string filepath, string filename)
        {
            string fullpath = Server.MapPath("/excelfolder/") + filename;
            ExcelConn(fullpath);
            string query = string.Format("Select * from [{0}]", "Sheet1$");
            OleDbCommand Ecom = new OleDbCommand(query, Econ);
            Econ.Open();

            DataSet ds = new DataSet();
            OleDbDataAdapter oda = new OleDbDataAdapter(query, Econ);
            Econ.Close();
            oda.Fill(ds);

            DataTable dt = ds.Tables[0];

            SqlBulkCopy objbulk = new SqlBulkCopy(con);
            objbulk.DestinationTableName = "dbo.Schools";
            objbulk.ColumnMappings.Add("AcademicYear", "AcademicYear");
            objbulk.ColumnMappings.Add("RollNumber", "RollNumber");
            objbulk.ColumnMappings.Add("OfficialSchoolName", "OfficialSchoolName");
            objbulk.ColumnMappings.Add("Address1", "Address1");
            objbulk.ColumnMappings.Add("Address2", "Address2");
            objbulk.ColumnMappings.Add("Address3", "Address3");
            objbulk.ColumnMappings.Add("Address4", "Address4");
            objbulk.ColumnMappings.Add("County", "County");
            objbulk.ColumnMappings.Add("Eircode", "Eircode");
            objbulk.ColumnMappings.Add("LocalAuthority", "LocalAuthority");
            objbulk.ColumnMappings.Add("X", "X");
            objbulk.ColumnMappings.Add("Y", "Y");
            objbulk.ColumnMappings.Add("ITMEast", "ITMEast");
            objbulk.ColumnMappings.Add("ITMNorth", "ITMNorth");
            objbulk.ColumnMappings.Add("Latitude", "Latitude");
            objbulk.ColumnMappings.Add("Longitude", "Longitude");

            con.Open();
            objbulk.WriteToServer(dt);
            con.Close();
        }

        private void InsertExceldata2(string filepath, string filename)
        {
            string fullpath = Server.MapPath("/excelfolder/") + filename;
            ExcelConn(fullpath);
            string query = string.Format("Select * from [{0}]", "Sheet1$");
            OleDbCommand Ecom = new OleDbCommand(query, Econ);
            Econ.Open();

            DataSet ds = new DataSet();
            OleDbDataAdapter oda = new OleDbDataAdapter(query, Econ);
            Econ.Close();
            oda.Fill(ds);

            DataTable dt = ds.Tables[0];

            SqlBulkCopy objbulk = new SqlBulkCopy(con);
            objbulk.DestinationTableName = "dbo.School2";
            objbulk.ColumnMappings.Add("RollNumber", "RollNumber");
            objbulk.ColumnMappings.Add("OfficialSchoolName", "OfficialSchoolName");
            objbulk.ColumnMappings.Add("Address1", "Address1");
            objbulk.ColumnMappings.Add("Address2", "Address2");
            objbulk.ColumnMappings.Add("Address3", "Address3");
            objbulk.ColumnMappings.Add("Address4", "Address4");
            objbulk.ColumnMappings.Add("County", "County");
            objbulk.ColumnMappings.Add("Eircode", "Eircode");
            objbulk.ColumnMappings.Add("PhoneNumber", "PhoneNumber");
            objbulk.ColumnMappings.Add("Email", "Email");
            objbulk.ColumnMappings.Add("PrincipalName", "PrincipalName");
            objbulk.ColumnMappings.Add("DeisSchool", "DeisSchool");
            objbulk.ColumnMappings.Add("SchoolGender", "SchoolGender");
            objbulk.ColumnMappings.Add("PupilAttendanceType", "PupilAttendanceType");
            objbulk.ColumnMappings.Add("IrishClassification", "IrishClassification");
            objbulk.ColumnMappings.Add("GaeltachtArea", "GaeltachtArea");
            objbulk.ColumnMappings.Add("FeePayingSchool", "FeePayingSchool");
            objbulk.ColumnMappings.Add("Religion", "Religion");
            objbulk.ColumnMappings.Add("OpenClosedStatus", "OpenClosedStatus");
            objbulk.ColumnMappings.Add("TotalGirls", "TotalGirls");
            objbulk.ColumnMappings.Add("TotalBoys", "TotalBoys");
            objbulk.ColumnMappings.Add("TotalPupils", "TotalPupils");

            con.Open();
            objbulk.WriteToServer(dt);
            con.Close();
        }
    }
}

My first advice is to do bulk inserts in the DBMS, not in code.我的第一个建议是在 DBMS 中进行批量插入,而不是在代码中。 Doing them via code is only prone to add addtional issues.通过代码执行它们只会增加额外的问题。

As far as parsing the.xlsx files go, the OleDB driver is propably unessesary.至于解析 .xlsx 文件 go,OleDB 驱动程序可能是不必要的。 There are a few basic rules for working with office formats:使用办公格式有一些基本规则:

  • if you can limit it to the new ones (.xlsx), you can use the OpenXML SDK .如果您可以将其限制为新的 (.xlsx),您可以使用OpenXML SDK Or any of the Wrappers people made around it.或者任何围绕它制作的 Wrappers。 Or even just the.ZipArchive and XMLReader classes.甚至只是 .ZipArchive 和 XMLReader 类。
  • if you need to support the old formats (.xls) too, you got to use the (t)rusty Office COM Interop.如果您也需要支持旧格式 (.xls),则必须使用 (t)rusty Office COM Interop。 This has all the usual issue of COM Interop, and aditionally needs office installed and a Interactive session这具有 COM 互操作的所有常见问题,并且还需要安装办公室和交互式 session
  • for any given Display Technology and Problem, there might be a 3rd option.对于任何给定的显示技术和问题,可能有第三种选择。 But those are few and far in between.但这些很少,而且介于两者之间。 As we always got the Interop to fall back on, we never developed a complete Office processing class like so many other languages have.由于我们总是依靠 Interop,我们从未像许多其他语言那样开发完整的 Office 处理 class。

I would put OlebDB in that last category - a rare and very specific solution.我会将 OlebDB 归为最后一类——一种罕见且非常具体的解决方案。

It always advise for using the first option.它总是建议使用第一个选项。

And Officer COM Interop should be silently burried, with the old formats being removed from the file format options.并且警官 COM 互操作应该被默默地掩埋,从文件格式选项中删除旧格式。 Considering this is a WebApplication that will likely run as services, you will not get the nesseary interactive session anyway.考虑到这是一个可能作为服务运行的 WebApplication,无论如何您都不会获得必要的交互式 session。

Of course accepting.csv is also an option.当然接受。csv 也是一种选择。 And indeed Excel has full.CSV support.确实 Excel 具有完整的.CSV 支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM