Free Support Forum - aspose.com

Problem with reading a csv/xls using .net(Urgent!)

This is what I am doing right now.

WorkBook workBook = new WorkBook();
workBook.Open(fileUpload.PostedFile.InputStream,GetContentType(fileUpload.PostedFile.FileName));

//GetContentType is function I have written which returns fileFormatType based on file extention
/
if (Path.GetExtension(fileName).ToLower() == “.xls”)
return FileFormatType.Excel2003;
else return FileFormatType.CSV;

/

Worksheet workSheet = workBook.Worksheets[0];

DataTable dt = workSheet.Cells.ExportDataTableAsString(0, 0, workSheet.Cells.MaxRow + 1, 5, true);

Requirement:
1) I provide user with a template file(xls/csv) with 5 columns in it. User makes changes to template and upload them using my webpage. Now I need to parse the csv/excel file they have uploaded.

Problem:
1) How to make sure they have uploaded the same template file I have given them. Because workBook.Open would read even a .exe file renamed with .xsl or .csv as extention.
How exactly to perform following validation.
1) File type is only xls/csv
2) Number of columns in that csv is only 5

Any help would be greatly appreciated



Hi,

Thanks for considering Aspose.

Well, if a file format is not native to MS Excel (like when renaming any file as .xls file), Aspose.Cells will throw CellsException describing: 'This file's format is not supported or you don't specify a correct format'. I think you may use try catch block for this.

And for getting columns 5 in numbers, you may try to use Cells.MaxDataColumn, Cells.MaxDataRow properties, these properties will let you obtain the farmost column/row index.

Thank you.