Click here to Skip to main content
Rate this: bad
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))
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
(no name)107.4K
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.
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?
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?
Ramya SV at 16-Oct-12 0:06am
Excel Package Vesrion is
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".
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.
Ramya SV at 16-Oct-12 1:29am
I know. I have added refernce and the version it shows is
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 Peter Leow 255
1 Mika Wendelius 240
2 Zoltán Zörgő 198
3 CHill60 195
4 TheRealSteveJudge 180
0 Sergey Alexandrovich Kryukov 8,698
1 OriginalGriff 6,591
2 Peter Leow 3,982
3 Zoltán Zörgő 3,634
4 Richard MacCutchan 2,535

Advertise | Privacy | Mobile
Web03 | 2.8.150123.1 | Last Updated 12 Oct 2012
Copyright © CodeProject, 1999-2015
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