C#/Excel data upload error
QUESTION: Hi Murat,
I am making an application in which I am downloading excel from website and uploading its data to my sql server database.
Now when I run code I am getting Error like, Exception from HRESULT: 0x800A03EC
I found that the problem is excel which I am opening has some unreadable data. I have attached an image of excel error.
This error is a reason for what I am getting Error in my code.
This is My C# Code :
string path = MapPath("/MyFolder/File1.xls");
var client = new WebClient();
client.DownloadFile("Url for Excel File", path);
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(path);
I am getting above error in xlApp.Workbooks.Open(path).
Help me for this error.
ANSWER: Hi there,
Code seems fine. Problem is probably about your file. Ensure your file is xls and not xlsx. Try to open manually with Excel and see if you get the same error.
Hope that helps,
---------- FOLLOW-UP ----------
There is no problem with code or extension. Problem is with file. When I open file manually it shows error which I have shown above in image. I have to open file and save it again then it will upload data perfectly.
But I am downloading file from code and uploading data directly to database. So I cant do it manually.
If the problem is in excel file, you cannot read the file through code. First you have to check and fix the file. Once you are sure the file is correct and can be read, then you can read with code. I have no idea why your file cannot be read, there are tons of reasons for showing that message. File may not be uploaded correctly, is another extension, you may be using older version of excel than the file etc.