You are here:

C#/Import Excel Error

Advertisement


Question
Import Excel Error
Import Excel Error  
Hi Murat,

I am making an application in which I am importing excel file and storing data to my database.

I am using "Microsoft.Office.Interop.Excel" for excel functions. But when Importing data it shows me an error which I have shown in Image.

The problem I found is if we want to use "Interop", we should have an Office installed on server which is not possible for me.

Please provide me any solution or alternative code to import excel files in my application.

I am not getting solution from anywhere. Please help me.

Thanks.

Answer
Hi there,

Yes, that error says that Excel is not properly installed on the machine you are running that application. You can try other solutions than Office.

Here is an answered question on StackOverflow which redirects to the EPPlus Project. It already has an example code.

FileInfo existingFile = new FileInfo(FilePath);
using (ExcelPackage package = new ExcelPackage(existingFile))
{
   // get the first worksheet in the workbook
   ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
   int col = 2; //The item description
   // output the data in column 2
   for (int row = 2; row < 5; row++)
       Console.WriteLine("\tCell({0},{1}).Value={2}", row, col, worksheet.Cells[row, col].Value);

   // output the formula in row 5
   Console.WriteLine("\tCell({0},{1}).Formula={2}", 3, 5, worksheet.Cells[3, 5].Formula);                
   Console.WriteLine("\tCell({0},{1}).FormulaR1C1={2}", 3, 5, worksheet.Cells[3, 5].FormulaR1C1);

} // the using statement automatically calls Dispose() which closes the package.

Note that there are other answers as well that you may want to try according to your needs.

Hope that helps.

Murat

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Murat Mehmet

Expertise

I can help with questions about desktop and web programming in C#, including SOAP, XML, database managing, custom controls, security etc.

Experience

I have been developing web and especially desktop applications in C# and VB.Net for almost 5 years. My programming life has begun with VB6 long time ago, so its about 8 years that I am in this business.

Organizations
Was in R & D for 2 years in a popular Turkish technology website: cyber-warrior.com

Education/Credentials
2011 Computer Engineering graduation in University of Trakia in Turkey.

©2016 About.com. All rights reserved.