Solved

getting the data out of an array into a string, using c# have data in array need to get it into a sting

Posted on 2013-01-30
7
560 Views
Last Modified: 2013-01-31
have the below code pulling the data out of the spread sheet and getting it into an array,
but now need to get it into a single varable.
have tried
String abc;
            abc = excelCell[1,1];
but throws up a Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)


Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);

            Excel.Range excelCell =  (Excel.Range)excelWorksheet.get_Range("A1", "E100");
            excelCell[1,2]= "dfddf";
   
            //       MessageBox.Show(excelCell[1][1].ToString());
            String abc;
            abc = excelCell[1,1];

topbottom
0
Comment
Question by:sydneyguy
  • 4
  • 2
7 Comments
 
LVL 40
ID: 38838516
You have the solution in your MessageBox. The MessageBox needs a String, and you are calling ToString to get it.

Since you want to assign to a String variable, do the same thing:

abc = excelCell[1,1].ToString();
0
 

Author Comment

by:sydneyguy
ID: 38838592
i did try this but it returned a "System.__ComObject" were

excelCell[1,2]= "dfddf";
            String abc;
            String xyz;
            xyz = " data str";   this works fine

            abc = excelCell[1, 1].ToString();  this string returns a com_object not a string object to work with

string
0
 
LVL 2

Assisted Solution

by:khanalprajwal
khanalprajwal earned 200 total points
ID: 38839316
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:sydneyguy
ID: 38839515
have read and looked through the code that you suggested but seems like i have to rebuild it with the new code. i already have it going i just need to get the data out of the System.__ComObject i can see it in the debug result it must be easy to get it from here.
would prefer to get this going than to we write the entire module
any ideas
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 300 total points
ID: 38840276
Sorry, I forgot one bit of code.

abc = excelCell[1,1].ToString(); calls ToString on the Cell, not on its value, and this returns the type by default.

This is the right one:

abc = excelCell[1,1].Value.ToString();
0
 

Author Comment

by:sydneyguy
ID: 38841759
the above solution got a bit complecated but got the below code up and running now and can access the string at what ever location that i wish so thanks for you help

   String filePath = @"C:\aeltest\@mill.csv";
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(filePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(1);

            Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)excelSheet.Cells[1, 1];
            string cellValue = range.Value2.ToString();
0
 

Author Closing Comment

by:sydneyguy
ID: 38841767
thanks for the help did not end up using either of the answers as i worked out a solution that fixed my problem as you can see in the above text, thanks for pointing me in the right direction
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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