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
567 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
[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
  • 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
Industry Leaders: 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: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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

738 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