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

 

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

What Security Threats Are You Missing?

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

744 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

10 Experts available now in Live!

Get 1:1 Help Now