Link to home
Start Free TrialLog in
Avatar of sydneyguy
sydneyguyFlag for Australia

asked on

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

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];

User generated imageUser generated image
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

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();
Avatar of sydneyguy

ASKER

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

User generated image
SOLUTION
Avatar of khanalprajwal
khanalprajwal
Flag of Nepal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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();
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