Link to home
Start Free TrialLog in
Avatar of ptslv
ptslvFlag for United States of America

asked on

Error programming to export a Visual C#.Net Windows Forms Application dynamic dataset to MS Excel using C#

I have a VS C#.Net Windows Forms application in which I am trying to export a dynamic dataset to Excel.  I have the reference Microsoft.Office.Interop.Excel added and have "using Microsoft.Office.Interop.Excel;".  I found this code on the internet and am getting an error:  The type or namespace name 'ExcelFile' could not be found (are you missing a using directive or an assembly reference?).

I am using MS Office 2013.

Would someone please assist me with getting past this error?  Right now there is only 1 table in my dataset.

public void ExportRptToExcel(DataSet dsTngData)
 {
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();                  
            var efRpt = new ExcelFile();    // Create new ExcelFile.              

            // Imports all the tables from DataSet to new file.

            foreach (System.Data.DataTable dt in dsTngData.Tables)
            {                
                var wsRpt = efRpt.Worksheets.Add(dt.TableName); 
                wsRpt.InsertDataTable(dt, "A1", true);
            }
            efRpt.SaveXls("dsTngData.xls");
  }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ptslv

ASKER

Kyle,

Thanks for the quick response.  That is the reference I had added.  I found it under the COM references.  Is there another reference that I need?

ptslv
Remove that one, there should be one under .Net

User generated image
Avatar of ptslv

ASKER

Kyle,

I am using Visual Studio 2012, Framework 4.5.  I do not have a tab for .NET when adding references.

ptslv
Are you right clicking on the solution or the project?  

Also from these steps it says you should be using the 4.0 framework . . . not sure if the 4.5 is compatible?

https://msdn.microsoft.com/en-us/library/vstudio/Dd264733(v=vs.110).aspx
Avatar of ptslv

ASKER

I changed the Framework from 4.5 to 4.0, saved and closed my project, re-opened my project.  I deleted the reference that I had and re-added it and the  Microsoft Excel 15.0 Object Library, and performed a Build.  I am still getting the same error message.

I have my solution open and am right-clicking on the References tab in the Solution Explorer.  I selected "Add Reference".  I still do not see a tab for .NET.
Do you have a 32 bit or 64 bit version of office?  Also what about your target cpu?  Not sure why you're not seeing the .Net references.  

If you right click on the project itself, and say add reference, do you get a different list?
Avatar of ptslv

ASKER

I am using 64 Bit.  

I do not have the option to Add References when I left-click or right-click on Project.  

I noticed that you are using Framework 4.0 Client Profile.  What is the difference between Framework 4.0 and Framework 4.0 Client Profile?
The client profile is a little lighter . . . but it shouldn't make a difference regarding that.  Was VS 2012 installed after word?  If so try doing a repair on the office installation.  The fact that you don't have a .net option at all is a little more disconcerting.

If you start with a blank windows forms solution, do you get that option?
Avatar of ptslv

ASKER

When I start a new Windows Forms project, the Add Reference option shows up in the Project tab.  But I still do not get the .NET tab under Add References.

The version VS that I have was installed after Office 2013 was installed.  I will try a repair on the VS and see what happens.  I also now have a NuGet warning since I rolled back to Framework 4.0 that I am trying to work through.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad you got it resolved.  Feel free to accept your own comment as the solution and mine as an assisted solution . . . applying points to the degree you feel I helped with the overall outcome.
Avatar of ptslv

ASKER

I gave the solution a B because, although performing a repair showed that I was having problems with my log files, it did not impact the library version that I had.  Somewhere there is a disconnect with the version of Visual Studio 2012 that we are both using.  Part of the solution is in my final comment, but I gave all the points to Kyle for sticking with me.