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
563 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

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!

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

685 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