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. 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. 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/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.