0

I'm reading an excel file with interop but I'm getting different values than I see on screen. I don't know what can be happening.

Excel that I'm reading:

When I read the cell E5 or E6 I'm getting the text "NUM VAGON", but what I see in screen is "Nº VAGON", the same with others rows and columns, different value of what I see and what I get.

There is no other worksheet so this is not the problem. If I copy all text in another excel it works fine.

The same applies with Value and Value2.

Here is my code:

 xl.Application app = null;

 try
 {
     //app = new Microsoft.Office.Interop.Excel.ApplicationClass();
     app = new Microsoft.Office.Interop.Excel.Application();
     //xl.Workbook theWorkbook = app.Workbooks.Open(eFile, 0, true, 5, "", "", true, xl.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
     xl.Workbook theWorkbook = app.Workbooks.Open(eFile);
     //xl.Sheets sheets = theWorkbook.Worksheets;
     //xl.Worksheet worksheet = (xl.Worksheet)sheets.get_Item(1);
     xl.Worksheet worksheet = (xl.Worksheet)theWorkbook.Sheets[1];
     xl.Range range = null;

     range = worksheet.get_Range("E6", "E6");
     //System.String titulo = (System.String)range.Cells.Value2;
     System.String titulo = (System.String)range.Cells.Text;
jose
  • 15
  • 3
  • 1
    Possible duplicate of [What is the difference between .text, .value, and .value2?](https://stackoverflow.com/questions/17359835/what-is-the-difference-between-text-value-and-value2) – mjwills Nov 09 '18 at 11:44
  • It i copy the cell into notepad i see the same value shown on screen, not the one i'm getting by code – jose Nov 09 '18 at 11:51
  • What is the value of `range.Cells[0].Value`? `range.Cells[0].Text`? `range.Cells[0].Value2`? `range.Cells[1].Value`? `range.Cells[1].Text`? `range.Cells[1].Value2`? – mjwills Nov 09 '18 at 11:56
  • In the same order, NUM VAGON, NUM VAGON, NUM VAGON, null, null, null – jose Nov 09 '18 at 14:06

1 Answers1

0

I think .Text gives you the formatted cell content (and for some reason the Interop dll is transforming the N° into NUM). You seem to have tried .Value2 (the commented line just above). Doesn't that give what you want? The Value property exists too but is almost the same as Value2.

This post explains the difference between Text, Value and Value2: https://stackoverflow.com/a/17363466/6996150

Update: How / where do you see that NUM text? When debugging? Or do you output it somewhere? Maybe the text is correct (N°) but formatted by the debugger or the tool you use to read your output?

johey
  • 1,139
  • 1
  • 9
  • 25
  • Thank you, the same applies to other ranges, for example i see in a range 5 columns of data in a row y by code i'm getting null in all. I've tried Text, Value and Value2 and the same result in all – jose Nov 09 '18 at 11:50
  • You never know; can you try to output it somewhere else, e.g. to the console. `Console.WriteLine(range.Cells.Text);` – johey Nov 09 '18 at 14:32