Reputation: 6242
I have an excel file which has say 5 columns and 5 rows.
I am using EPPlus (.net core c#)to parse the contents of the file.
I have the below code to count the rows and columns and then parse contents of this excel file.
using (ExcelPackage package = new ExcelPackage(stream))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int rowCount = worksheet.Dimension.End.Row;
int colCount = worksheet.Dimension.End.Column;
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= colCount; col++)
{
//Parse here
}
}
}
With 5 rows and 5 columns of data as mentioned this works fine.
If I update and save the same file, and delete say 3 rows and 3 columns.
Now if I read this file again it still shows 5 columns and rows. Not sure if there is another way to read actual rows and columns?
Am I missing something here.
Upvotes: 0
Views: 6551
Reputation: 262
Try this
using (ExcelPackage package = new ExcelPackage(stream))
{
string directoryServerPath = // your file saving path in server;
if (!Directory.Exists(directoryServerPath))
Directory.CreateDirectory(directoryServerPath);
System.IO.DirectoryInfo directoryFiles = new DirectoryInfo(directoryServerPath);
foreach (FileInfo file in directoryFiles.GetFiles())
{
file.IsReadOnly = false;
file.Delete();
}
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int rowCount = worksheet.Dimension.Rows;
int colCount = worksheet.Dimension.Columns;
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= colCount; col++)
{
//Parse here
}
}
package.SaveAs(new System.IO.FileInfo(directoryServerPath + fileName));
System.IO.File.SetAttributes(directoryServerPath + fileName, FileAttributes.ReadOnly);
}
Upvotes: 1