Opening an excel spreadsheet in C#

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
slimbxAsked:
Who is Participating?
 
jkna_gunnConnect With a Mentor Commented:
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
 
Vaishali_chaudhryCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jkna_gunnCommented:
are you still having problems?
0
 
slimbxAuthor Commented:
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
 
jkna_gunnCommented:
thanks!

do you need help with the 2nd part?
0
 
slimbxAuthor Commented:
I was going for either or but if you be so kind as to, that would cool. Thanks jkna_gunn
slimbx
0
 
jkna_gunnCommented:
so when you say load a spread sheet into a listview
do you mean 1->1 cell mapping?
0
 
slimbxAuthor Commented:
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
 
jkna_gunnCommented:
ok so what code do you have so far and what isn't working
0
 
slimbxAuthor Commented:
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
 
jkna_gunnCommented:
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
 
pml_sivaCommented:
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
 
BOOBICCommented:
pml siva:
Do you have that code in VB.NET.

RGDS
Sam
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.