Click here to Skip to main content
12,072,809 members (71,995 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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
using (ExcelPackage xlPackage = new ExcelPackage(existingFile))
Code:
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 12-Oct-12 1:34am
Edited 12-Oct-12 1:35am
v3
Comments
Wes Aday 12-Oct-12 10:01am
   
Okay so why are you not looking at what ExcelPackage is doing that throws that exception?
digimanus 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.
digimanus 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
Ramya SV 15-Oct-12 2:38am
   
I tried with uploading another file, same error is comming.
   
It can be anything. Why did you ignore my question?
--SA
Ramya SV 15-Oct-12 4:04am
   
It is opened with Excel.
Sergey Alexandrovich Kryukov 15-Oct-12 11:15am
   
Thank you. Now, can you make sure that you are using the version of Excep interop assembly which is up to date?
--SA
Ramya SV 16-Oct-12 0:06am
   
Excel Package Vesrion is 1.0.0.1
   
I don't know such version. What "package"? Interop assembly version is shown when you add a reference using the tab ".NET" of the window "Add Reference".
--SA
Ramya SV 16-Oct-12 0:58am
   
It has not been added from .Net tab. It is downloaded seperately and placed in GAC.
   
You have no clue. If something is placed in GAC, you need to add a reference to link the assembly anyway. Further help looks useless. Please learn how assemblies work and come back.
--SA
Ramya SV 16-Oct-12 1:29am
   
I know. I have added refernce and the version it shows is 1.0.0.1.
Run time version is v2.0.50727

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 12 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100