Solved

Opening an excel spreadsheet in C#

Posted on 2004-04-27
14
2,627 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
14 Comments
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10935817
0
 
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
 

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 250 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
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.

 
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

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

22 Experts available now in Live!

Get 1:1 Help Now