Can anyone suggest, how to check if xls file empty or not?
I have tried FileInfo("fname").Length == 0
But it's not working.
Thanks
Can anyone suggest, how to check if xls file empty or not?
I have tried FileInfo("fname").Length == 0
But it's not working.
Thanks
An "empty" Excel file will still have an Excel file header, which is why your test for a size of 0 is failing.
You can use Excel Interop to see what the UsedRange is:
for .xls
you can try this - it works for me.
FileUpload1.SaveAs(Server.MapPath("~/FileUpload/") + path.Value);
Workbook book = Workbook.Load(Server.MapPath(("~/FileUpload/") + FileUpload1.FileName));
Worksheet sheet = book.Worksheets[0];
sheetCount.Value = sheet.Cells.LastRowIndex.ToString();
foreach (Worksheet ws in book.Worksheets)
{
if (ws.Cells.Rows.Count != 0)
{
ddlSheets.Items.Add(ws.Name.ToString());
}
}
I think there is not simple solution similar to one you have stated.
You will have to actually programatically read the file by some Excel parser and check if there are some data.
Using Excel Interop I have created this function. It works as Ribbon add-in, however you can replace Globals.ThisAddIn
with Excel.Application
.
private bool IsEmptyFolder(Excel.Workbook wb)
{
try
{
foreach(Excel.Worksheet sheet in wb.Worksheets)
{
if (Globals.ThisAddIn.Application.WorksheetFunction.CountA(sheet.Cells) != 0) return false;
}
return true;
}
catch
{
return false;
}
}
If it finds any worksheet with a value in a cell, it returns false. Otherwise it returns true (the workbook is empty).