3

I am reading excel sheet with following code:

  Excel.Range uRange = xlWorkSheet.UsedRange;  
  dynamic data = uRange.Value2;

As you can see I copy used range cell values into dynamic 2D array. One column of selected range is custom formatted and with above technic I am getting just a bunch of numbers instead of nice formatted value when accessing this custom formatted cell (data[x,y]).

If I am accessing only certain cell directly with following code:

 (Excel.Range)xlWorkSheet.Cells[x, y].Text

... I can see the formatted value I need. By accessing each cell separately I am loosing a lot of time because it is slower to access cell by cell inside a loop.

Is it possible to get all Text values from selected range. Something like this:

 Excel.Range uRange =xlWorkSheet.UsedRange; 
 dynamic data = uRange.Text;

When I use above code I am getting empty variable.

2
  • 1
    I've managed to get text values with uRange.Value. What is the exactly difference between Value and Value2 Commented Jul 11, 2011 at 12:44
  • let me know if you go tthis q answered because i have ont Commented Apr 1, 2012 at 4:33

4 Answers 4

2

Note that if you are using COM interop in Silverlight application to access excel files that approach will not work.

Here's the solution, basically you need to iterate through columns, and get 1-dimensional array for each column. (this approach is very fast)

 foreach (var column in fullRange.Columns)
 {
   // vals will be a one-dimensional array 
   // of all values in that column!
   var vals = column.Value;

   foreach (var val in vals)
   {
       if (val == null) continue;
       string cellValue = val.ToString();
       //Debug.WriteLine(" Value read: " + cellValue);
       //do something with the value...

   }
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Here's how I got it to work (formatted mine as dates):

currentWorksheet.Range["A2", "A10"].NumberFormat = "m/d/yyyy";
dynamic vals = currentWorksheet.Range["A2", "A10"].Value;

Comments

0

use Object obj = uRange.Value2

Comments

0

I had the Requirement: Import excel records in the datatable. So I tried with oledbConnection, but it Excel.exe does not removed from the Task Manager. So I tried below approach, and its working fine.

public void ImportData(string _FileName, int _SheetIndex)
    {
        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkbook = null;
        Excel.Worksheet xlWorksheet = null;
        Excel.Range xlRange = null;

        try
        {
            Logger.Write(String.Format("Import Started for File {0}",_FileName));

            xlWorkbook = xlApp.Workbooks.Open(_FileName);
            xlWorksheet = xlWorkbook.Sheets[_SheetIndex];
            xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            object[,] objectArray = (object[,])xlRange.Value[Excel.XlRangeValueDataType.xlRangeValueDefault];



        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (xlRange != null)
            {
                Marshal.ReleaseComObject(xlRange);
            }

            if (xlWorksheet != null)
            {
                Marshal.ReleaseComObject(xlWorksheet);
            }

            if (xlWorkbook != null)
            {
                xlWorkbook.Close(Excel.XlSaveAction.xlDoNotSaveChanges);
                Marshal.ReleaseComObject(xlWorkbook);
            }

            xlApp.Quit();
            Marshal.ReleaseComObject(xlApp);
            Logger.Write(String.Format("Import End for File {0}", _FileName));
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.