?
Solved

Opening an excel spreadsheet in C#

Posted on 2004-04-27
14
Medium Priority
?
2,633 Views
Last Modified: 2011-10-03
Hello everyone. I am new to C#, and have kind of a dumb but interesting question. I have a C# program that I am trying to do two things with at this point. One is I want to provide the ability to load and open a spreadsheet in excel from C#. Currently the code to do this is as follows:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="excel";
proc.StartInfo.Arguments=dlgOpenFileDialog.FileName;
proc.Start();
proc.WaitForExit();            //waits until program is relinquished

The issue here is that the filename, denoted by the filename property of my open file dialog control, already take into account the \\ in the path selected, but when I try starting the process, I am receiving errors about cannot find the specified file, and it breaks up the filepath into sections. How can I correct this code.

Along with showing spreadsheets, I need to also load a spreadsheet into a listview or similar control. I am just looking for examples of this. I will offer a lot of points as this is a two part question in one. Hopefully this is legal. Thanks in advance.

slimbx
0
Comment
Question by:slimbx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 2

Expert Comment

by:Vaishali_chaudhry
ID: 10936010
in c# if you want a double quotation string to behave as is and all special characters to be escaped use a "@" called verbatim.
for eg:

string fileName="c:\\dir1\file1.txt";
string fileName=@"c:\file1\file1.txt"; are same and simpler to use...

Try using a verbatim in your filename ,i cant suggest where ...if possible post the filename returned by the dialog box.
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10937699
are you still having problems?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:slimbx
ID: 10940060
rama_krishna580, I actually tried the code at http://www.c-sharpcorner.com/winforms/ExcelReadMG.asp . I was recieving the following error with it though:

Property, indexer, or event 'Value' is not supported by the language; try directly calling accessor methods 'Excel.Range.get_Value(object)' or 'Excel.Range.set_Value(object, object)'.

This error happens in the first line below:
Array myvalues = (Array)range.Cells.Value();
string[] strArray = ConvertToStringArray(myvalues);

=====================


Vaishali_chaudhry, My file path already comes from the open file dialog, so how can I perform the @ on the filename when it is already done for me? I tried saving the file name to a string variable and then using the @, but it still didnt work for me?



Thanks to you both for your help thus far!!
0
 
LVL 6

Accepted Solution

by:
jkna_gunn earned 1000 total points
ID: 10940243
to open an excel file i do this

object missing = System.Reflection.Missing.Value;
string filename = "";
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
    filename = ofd.FileName;
}
Excel.Application app = new Excel.ApplicationClass();
app.Visible = true;
app.Workbooks.Open(filename, missing,missing, missing, missing,missing,missing, missing, missing, missing,missing, missing, missing, missing ,missing);
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10940513
thanks!

do you need help with the 2nd part?
0
 

Author Comment

by:slimbx
ID: 10940590
I was going for either or but if you be so kind as to, that would cool. Thanks jkna_gunn
slimbx
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10940642
so when you say load a spread sheet into a listview
do you mean 1->1 cell mapping?
0
 

Author Comment

by:slimbx
ID: 10940835
Yes. See this site, this is the code that I am using:  
http://www.c-sharpcorner.com/winforms/ExcelReadMG.asp

All I really need is to show the first worksheet in the spreadsheet in the listview. If you look in my comments above, I show where the errors are raising. I am using Excel v10 object, instead of 9. Thi smay be my problem, but I am still kind of green around the gills with C#.
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10940937
ok so what code do you have so far and what isn't working
0
 

Author Comment

by:slimbx
ID: 10942790
I am getting the following error:

Property, indexer, or event 'Value' is not supported by the language; try directly calling accessor methods 'Excel.Range.get_Value(object)' or 'Excel.Range.set_Value(object, object)'.

This error happens in the first line below:
Array myvalues = (Array)range.Cells.Value();
string[] strArray = ConvertToStringArray(myvalues);

P.S. - I am using Excel version 10 object library not 9. But this should still work okay though I think.

Hope this is helpful. -slimbx
0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10946902
i will have to look into that, i actually did the opposite things, i converted a listview contents to excel, but im sure i can manage to do that too.

will let you know.
0
 
LVL 4

Expert Comment

by:pml_siva
ID: 10956194
To open an Excel file do this...

The first step is to create a reference in your project to Excel 9.0 Objects Library.  This is done by right mouse clicking on the References folder in the Solution Explorer and choosing Add Reference. This brings up the Tab Dialog below.  Choose the COM Tab and pick Microsoft Excel 9.0 Objects Library.

This action puts an Interop.Excel.dll and Interop.Office.dll into your bin directory so you can manipulate excel.  

Now we can declare our Excel Application Object and the compiler will recognize it:

private Excel.Application ExcelObj = null;

Write this code into an event(Button Click..)
// prepare open file dialog to only search for excel files (had trouble setting this in design view)
  this.openFileDialog1.FileName = "*.xls";
  if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
   {

  // ***********  Here is the call to Open a Workbook in Excel ****************
  // It uses most of the default values (except for the read-only which we set to true)
    Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(
          openFileDialog1.FileName, 0, true, 5,
          "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
          0, true);

    // get the collection of sheets in the workbook
    Excel.Sheets sheets = theWorkbook.Worksheets;

   // get the first and only worksheet from the collection
   // of worksheets
    Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
0
 

Expert Comment

by:BOOBIC
ID: 11544773
pml siva:
Do you have that code in VB.NET.

RGDS
Sam
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

800 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