Solved

Manage Excel files with C#

Posted on 2014-10-31
5
146 Views
Last Modified: 2014-11-03
HI

I am converting a VB.NET application to C# that uses Excel files. I have tried to search for how to use Excel files in C#, but I just can’t get any of it to work. I’m not experienced in C# so I don’t see the issues so easily.

Can someone help me with some simple example code that …
Opens an existing Excel file
Creates a new Excel file
Reads values from a Worksheet
Write values to a worksheet
Closes the Excel file

I’m using Visual Studio 2008 and Excel 2003.
0
Comment
Question by:daghoff
5 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 40417133
you already have VB code? check this converter: http://converter.telerik.com/
0
 
LVL 25

Expert Comment

by:apeter
ID: 40417330
You want to duplicate the file ?
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40417335
I was going to suggest using a converter.  However, first, is this truly a VB.NET application or VB6?

-saige-
0
 

Author Comment

by:daghoff
ID: 40417565
Hi

Thanks for your answerers, but you have misunderstood. I am not looking for converters. I already have tested many of them for the conversion, but the problem is that converters don’t do a really good job. And they do not successfully convert my Excel code that works well in my VB applications.

So I have decided to rebuild that code from scratch. I have tried most of what I find on webs, but there is always something that does not work. Probably because I use an old version of Visual Studio and Excel. I’m also inexperienced with C#.

My most successful attempt comes from this link:
 http://csharp.net-informations.com/excel/csharp-open-excel.htm.

It should work since it is a fully working example, but I get the error
 “Object reference not set to an instance of an object.”
 In this line:
MessageBox.Show(xlWorkSheet.get_Range("A1", "A1").Value2.ToString());

And I don’t see what the problem can be since the Excel Workbook apparently opens. Or does it not?

This is what I need your help with. I’ll deal with all the other C# problems later.

Regards.
0
 
LVL 25

Accepted Solution

by:
ProfessorJimJam earned 500 total points
ID: 40417719
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now