How to copy contents of Excel UsedRange into an array for faster reading?

erotavlas

I'm trying to improve the performance of an application that is reading cells from an Excel worksheet row by row. I found this solution

https://stackoverflow.com/a/25842904/1462656

You want to do it in one operation:

object[,] objectArray = cSheet.get_Range("A1:C4").Value2; 
dataRange.Value2 = objectArray;

Is it assuming all the cells are of type Value2?.

In general I would like to know how to get the contents of the UsedRange into a local array for faster access (instead of keep going back and forth through the interop). But I don't know in advance the size, but I know in advance the cell types on each column. They are a combination of Value2 and Formula.

My code so far

        Application application = new Application();
        application.Workbooks.Open(file);
        Workbook workbook = application.Workbooks.get_Item(1);
        Worksheet worksheet = workbook.Worksheets.get_Item(1);

        Range UsedRange = worksheet.UsedRange;
        int rows = UsedRange.Rows.Count;
        int cols = UsedRange.Columns.Count;

        object[,] objectArray = new object[rows,cols];

I don't know what to do next.

I was going to do this

objectArray = (object[,])UsedRange.Range[?,?];

But I don't know the syntax to specify the Range using the columns count and rows count I found above.

Originally I had a for loop like this which was extremely slow

for (int rowIndex = 2; rowIndex < UsedRange.Rows.Count; rowIndex++){

    string str1= UsedRange.Rows.Cells[rowIndex, 0 + 1].Value2.ToString();
    string str2= UsedRange.Rows.Cells[rowIndex, 1 + 1].Formula as string;

}
erotavlas

I could not specify the range as cell values as in the other solutions for example

shtName.get_Range("A1:Z100")

So what I was missing was a way to specify the used range without entering cell range as above, and I found the answer from this example

https://www.dotnetperls.com/excel-vbnet

        Range UsedRange = worksheet.UsedRange;

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

        for (int row = 2; row < objectArray.GetUpperBound(0); row++)
        {
             string str1= (objectArray[row, 1] == null) ? string.Empty : objectArray[row, 1].ToString();

             string str2= (objectArray[row, 2] == null) ? string.Empty : objectArray[row, 2].ToString();

             //...etc
        }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reading an Excel file UsedRange error

From Dev

How to copy contents of an array to a variable

From Dev

reading contents of a returned array?

From Dev

reading contents of a returned array?

From Dev

Trying to copy contents of array

From Dev

How to copy contents in an array of object into another array in C++?

From Dev

How to copy the contents only of an Excel cell minus the formatting?

From Dev

How to copy the contents of one 2d array to another? (VBA)

From Dev

How to copy a csv file contents and rewrite with additional array in the same file?

From Dev

How to copy the contents of ByteBuffer?

From Dev

How to create an array or dictionary from reading excel

From Dev

How to create an array or dictionary from reading excel

From Dev

Excel resetting "UsedRange"

From Dev

Excel vba reading large amount of files faster

From Dev

Copy contents of a unique pointer array in the copy constructor

From Dev

Copy contents of a unique pointer array in the copy constructor

From Dev

Excel UsedRange as Column only address

From Dev

How to shift the contents of an array?

From Dev

How to shuffle the contents of an array

From Dev

How to properly copy contents of one char array into another when passed to function, WITHOUT using STL?

From Dev

How to copy a JavaScript 2D array to clipboard to paste it in excel?

From Dev

How to ignore reading contents of /*comment*/ while reading a file

From Dev

Reading contents of excel file in python webapp2

From Dev

Reading cell contents in an Excel file with F# and Open XML SDK

From Dev

How to Copy Contents in one DataGridview to another DataGridview

From Dev

How to copy contents of the const char* type variable?

From Dev

How to copy NSData contents to temporary file?

From Dev

How to copy contents of one canvas to another?

From Dev

How to copy contents of a baseline in Clearcase UCM?

Related Related

  1. 1

    Reading an Excel file UsedRange error

  2. 2

    How to copy contents of an array to a variable

  3. 3

    reading contents of a returned array?

  4. 4

    reading contents of a returned array?

  5. 5

    Trying to copy contents of array

  6. 6

    How to copy contents in an array of object into another array in C++?

  7. 7

    How to copy the contents only of an Excel cell minus the formatting?

  8. 8

    How to copy the contents of one 2d array to another? (VBA)

  9. 9

    How to copy a csv file contents and rewrite with additional array in the same file?

  10. 10

    How to copy the contents of ByteBuffer?

  11. 11

    How to create an array or dictionary from reading excel

  12. 12

    How to create an array or dictionary from reading excel

  13. 13

    Excel resetting "UsedRange"

  14. 14

    Excel vba reading large amount of files faster

  15. 15

    Copy contents of a unique pointer array in the copy constructor

  16. 16

    Copy contents of a unique pointer array in the copy constructor

  17. 17

    Excel UsedRange as Column only address

  18. 18

    How to shift the contents of an array?

  19. 19

    How to shuffle the contents of an array

  20. 20

    How to properly copy contents of one char array into another when passed to function, WITHOUT using STL?

  21. 21

    How to copy a JavaScript 2D array to clipboard to paste it in excel?

  22. 22

    How to ignore reading contents of /*comment*/ while reading a file

  23. 23

    Reading contents of excel file in python webapp2

  24. 24

    Reading cell contents in an Excel file with F# and Open XML SDK

  25. 25

    How to Copy Contents in one DataGridview to another DataGridview

  26. 26

    How to copy contents of the const char* type variable?

  27. 27

    How to copy NSData contents to temporary file?

  28. 28

    How to copy contents of one canvas to another?

  29. 29

    How to copy contents of a baseline in Clearcase UCM?

HotTag

Archive