简体   繁体   English

MVC 5如何使用EPPlus(OfficeOpenXml)ExcelPackage上传Excel文件并读取行,直到最后填充的数据行

[英]MVC 5 How to upload Excel file and read rows till the last populated data row using EPPlus (OfficeOpenXml) ExcelPackage

I have a MVC file upload solution which uploads a Excel file, I am having an issue with the Excel Package where it reads my Excel docs row right to the end of the file using Dimension.End.Row property, I want to read till the last populated row and not the entire spreadsheet rows as it bombs out my code with a null reference error, the Excel is from an external source and it is a locked file which has about 7000 rows, but only about 3000 rows are populated with data, the Dimension.End.Row property reads the entire 7000 rows therefore when I loop though and map the rows and columns, after the last populated data row 3001 on wards it has null values. 我有一个上载Excel文件的MVC文件上传解决方案,我在Excel软件包中遇到问题,该软件包使用Dimension.End.Row属性将我的Excel文档行读取到文件末尾,我想读取直到最后填充的行,而不是整个电子表格的行,因为它用空引用错误轰炸了我的代码,Excel来自外部源,并且它是一个锁定的文件,大约有7000行,但只有大约3000行填充了数据, Dimension.End.Row属性读取整个7000行,因此当我循环遍历并映射行和列时,在病房中最后一个填充的数据行3001之后,它具有空值。 How can I enable this to read only the populated rows in the excel file, like I said I cannot modify this file as its locked and read-only, I can only read it and upload it. 我怎样才能使它只能读取excel文件中的填充行,就像我说过的那样,我不能修改该文件为锁定和只读状态,所以只能读取并上传它。 please see my code below. 请在下面查看我的代码。

[HttpPost]
    public ActionResult Upload(FormCollection formCollection)
    {
        if (Request != null)
        {
            HttpPostedFileBase file = Request.Files["UploadedFile"];
            if ((file != null && file.ContentLength > 0 && !string.IsNullOrEmpty(file.FileName)))
            {
                string fileName = file.FileName;
                string fileContentType = file.ContentType;
                byte[] filebytes = new byte[file.ContentLength];
                var data = file.InputStream.Read(filebytes, 0, Convert.ToInt32(file.ContentLength));

                var suppleirList = new List<CleanSupplierClaim>();

                using (var package = new ExcelPackage(file.InputStream))
                {
                    var currentSheet = package.Workbook.Worksheets;
                    var workSheet = currentSheet.First();
                    var noOfColumns = workSheet.Dimension.End.Column;
                    var noOfRows = workSheet.Dimension.End.Row;// Here is where my issue is

                    for (int rowIterator = 5; rowIterator < noOfRows; rowIterator++)
                    {
                        var claim = new CleanSupplierClaim();

                        claim.Action = workSheet.Cells[rowIterator, 1].ToString();
                        claim.Line_Number = workSheet.Cells[rowIterator, 2].Value.ToString();
                        claim.Total_Claim = workSheet.Cells[rowIterator, 3].Value.ToString();
                        claim.ClaimReference = workSheet.Cells[rowIterator, 4].Value.ToString();
                        claim.Supplier_Claim = workSheet.Cells[rowIterator, 5].Value.ToString();
                        claim.Currency = workSheet.Cells[rowIterator, 6].Value.ToString();

                        suppleirList.Add(claim);
                    }



                }
            }
        }
        return View("Index");

You can do something like this; 您可以执行以下操作;

for (int rowIterator = 5; rowIterator < noOfRows; rowIterator++)
{
      if(workSheet.Cells[rowIterator, 1] == null)//can this be null?
      {
        break; //if it's null exit from the for loop
      }

      var claim = new CleanSupplierClaim();

      claim.Action = workSheet.Cells[rowIterator, 1].ToString();
      claim.Line_Number = workSheet.Cells[rowIterator, 2].Value.ToString();
      claim.Total_Claim = workSheet.Cells[rowIterator, 3].Value.ToString();
      claim.ClaimReference = workSheet.Cells[rowIterator, 4].Value.ToString();
      claim.Supplier_Claim = workSheet.Cells[rowIterator, 5].Value.ToString();
      claim.Currency = workSheet.Cells[rowIterator, 6].Value.ToString();

      suppleirList.Add(claim);
  }

You can try something like below: 您可以尝试以下操作:

public void readXLS(string FilePath)
{
    FileInfo existingFile = new FileInfo(FilePath);
    using (ExcelPackage package = new ExcelPackage(existingFile))
    {
        //get the first worksheet in the workbook
        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
        int colCount = worksheet.Dimension.End.Column;  //get Column Count
        int rowCount = worksheet.Dimension.End.Row;     //get row count
        for (int row = 1; row <= rowCount; row++)
        {
            for (int col = 1; col <= colCount; col++)
            {
                Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
            }
        }
    }
}

Reff: http://sforsuresh.in/reading-excel-file-using-epplus-package/ Reff: http ://sforsuresh.in/reading-excel-file-using-epplus-package/

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

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