Solved

Opening an excel spreadsheet in C#

Posted on 2004-04-27
14
2,625 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
Comment Utility
0
 
LVL 2

Expert Comment

by:Vaishali_chaudhry
Comment Utility
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
Comment Utility
are you still having problems?
0
 

Author Comment

by:slimbx
Comment Utility
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
Comment Utility
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
Comment Utility
thanks!

do you need help with the 2nd part?
0
 

Author Comment

by:slimbx
Comment Utility
I was going for either or but if you be so kind as to, that would cool. Thanks jkna_gunn
slimbx
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

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

Author Comment

by:slimbx
Comment Utility
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
Comment Utility
ok so what code do you have so far and what isn't working
0
 

Author Comment

by:slimbx
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
pml siva:
Do you have that code in VB.NET.

RGDS
Sam
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

16 Experts available now in Live!

Get 1:1 Help Now