Click here to Skip to main content
15,921,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to upload an Excel file with .xls and .xlsx formats and save it into a local C: Drive and then convert it into Datatable.

I am getting File Format Exception near
C#
using (ExcelPackage xlPackage = new ExcelPackage(existingFile))

Code:
C#
DataTable dt;
string XmlString = string.Empty;     

string strFile = ExcelFileUpload.FileName;
var fileExt = System.IO.Path.GetExtension(ExcelFileUpload.FileName);
if (fileExt == ".xlsx" || fileExt == ".xls")
{
    ExcelFileUpload.SaveAs("C:\\Docs\\" + strFile);
    FileInfo existingFile = new FileInfo("C:\\Docs\\" + strFile);
    using (ExcelPackage xlPackage = new ExcelPackage(existingFile)) ***//throws Exception***
    {
        ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];
        dt = new DataTable(); 
    }
}
Posted
Updated 12-Oct-12 0:35am
v3
Comments
[no name] 12-Oct-12 10:01am    
Okay so why are you not looking at what ExcelPackage is doing that throws that exception?
Herman<T>.Instance 12-Oct-12 10:56am    
what happens is you upload an xls file and open it with xlsx functionality? XLS is a different format than xlsx. XLSX is a zipped package. (are you using epplus to read the excel file?)
Ramya SV 15-Oct-12 2:41am    
No i am not using any epplus to read the excel file.
Herman<T>.Instance 15-Oct-12 4:34am    
are you transferring binary ?
Sergey Alexandrovich Kryukov 12-Oct-12 14:27pm    
Is it opened with Excel? Chances are, the file is really corrupted.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900