Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用FileHelpers使用MVC 5导入Excel数据_C#_Asp.net Mvc_Excel_Filehelpers - Fatal编程技术网

C# 使用FileHelpers使用MVC 5导入Excel数据

C# 使用FileHelpers使用MVC 5导入Excel数据,c#,asp.net-mvc,excel,filehelpers,C#,Asp.net Mvc,Excel,Filehelpers,我正在尝试用MVC5编写一个应用程序,它将接受用户指定的文件,并将该文件信息上传到数据库中。文件本身有多个工作表,我认为FileHelpers可以很好地处理这些工作表,但是我找不到任何关于使用字节数组的好文档。我可以很好地获取文件,并到达我的控制器,但不知道从那里去哪里。我目前正在控制器中执行此操作: public ActionResult UploadFile(string filepath) { //we want to check here that the fir

我正在尝试用MVC5编写一个应用程序,它将接受用户指定的文件,并将该文件信息上传到数据库中。文件本身有多个工作表,我认为FileHelpers可以很好地处理这些工作表,但是我找不到任何关于使用字节数组的好文档。我可以很好地获取文件,并到达我的控制器,但不知道从那里去哪里。我目前正在控制器中执行此操作:

public ActionResult UploadFile(string filepath)
    {
        //we want to check here that the first file in the request is not null
        if (Request.Files[0] != null)
        {
            var file = Request.Files[0];


            byte[] data = new byte[file.ContentLength];

            ParseInputFile(data);
            //file.InputStream.Read(data, 0, data.Length);
        }

        ViewBag.Message = "Success!";

        return View("Index");
    }

    private void ParseInputFile(byte[] data)
    {
        ExcelStorage provider = new ExcelStorage(typeof(OccupationalGroup));

        provider.StartRow = 3;
        provider.StartColumn = 2;
        provider.FileName = "test.xlsx";
    }

我是否能够将这样的请求与FileHelper一起使用?我只需要将Excel文件读入数据库。如果没有,我是否应该寻找一种不同的方式来处理上传?

因此,我决定改为使用Excel进行阅读。它将流(在下面的代码中,test)放入一个我可以手动操作的数据集中。我相信这可能不是最干净的方法,但它对我来说是有意义的,并且允许我相当容易地处理多个工作表。以下是我最终使用的常规代码片段:

//test is a stream here that I get using reflection
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(test);

DataSet result = excelReader.AsDataSet();

while(excelReader.Read())
{
    //process the file
}

excelReader.Close();