Click here to Skip to main content
Rate this: bad
good
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 0:34am
Edited 12-Oct-12 0:35am
103K
v3
Comments
Wes Aday at 12-Oct-12 10:01am
   
Okay so why are you not looking at what ExcelPackage is doing that throws that exception?
digimanus at 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 at 15-Oct-12 2:41am
   
No i am not using any epplus to read the excel file.
digimanus at 15-Oct-12 4:34am
   
are you transferring binary ?
Sergey Alexandrovich Kryukov at 12-Oct-12 14:27pm
   
Is it opened with Excel? Chances are, the file is really corrupted.
--SA
Ramya SV at 15-Oct-12 2:38am
   
I tried with uploading another file, same error is comming.
Sergey Alexandrovich Kryukov at 15-Oct-12 2:46am
   
It can be anything. Why did you ignore my question?
--SA
Ramya SV at 15-Oct-12 4:04am
   
It is opened with Excel.
Sergey Alexandrovich Kryukov at 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 at 16-Oct-12 0:06am
   
Excel Package Vesrion is 1.0.0.1
Sergey Alexandrovich Kryukov at 16-Oct-12 0:47am
   
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 at 16-Oct-12 0:58am
   
It has not been added from .Net tab. It is downloaded seperately and placed in GAC.
Sergey Alexandrovich Kryukov at 16-Oct-12 1:22am
   
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 at 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
0 OriginalGriff 300
1 Sergey Alexandrovich Kryukov 176
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 6,081
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 12 Oct 2012
Copyright © CodeProject, 1999-2014
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