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

问题描述 投票:0回答:2

我有一个上传 Excel 文件的 MVC 文件上传解决方案,我遇到了 Excel 包的问题,它使用 Dimension.End.Row 属性读取我的 Excel 文档行到文件末尾,我想阅读直到最后填充的行而不是整个电子表格行,因为它用空引用错误轰炸了我的代码,Excel 来自外部源,它是一个锁定文件,大约有 7000 行,但只有大约 3000 行填充了数据, Dimension.End.Row 属性读取整个 7000 行,因此当我循环并映射行和列时,在最后填充的数据行 3001 之后,它具有空值。我怎样才能让它只读取Excel文件中填充的行,就像我说的,我无法修改这个文件,因为它是锁定的和只读的,我只能读取它并上传它。请参阅下面我的代码。

[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");
c# excel model-view-controller epplus
2个回答
1
投票

你可以这样做;

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);
  }

1
投票

您可以尝试如下所示:

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());
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.