سؤال

I'm trying to get the total number of columns so that i can set my range from column C to N column. I'm trying to go through each column and pull out the data i need into an array until i reach the last column with data in it.

I tried the below which works for one row, but none of the others i have.

IRange strategyname = foratworkbook.Worksheets["Strategy"].Cells["C4"].CurrentRegion;

C           D           E 
9.040%      10.910%     6.050%
4.920%      5.510%      3.430%
53.030%     64.400%     36.030%

Any Ideas? Thanks

هل كانت مفيدة؟

المحلول

If you know the maximum size of the array you can pull the entire table A:N x row nums into C# as a DataTable, find the first null column and first null row and then remove the columns and rows that are null from that DataTable. That is a reasonably quick and straightforward procedure.

These functions should return the first null row and column in a datatable

   public static int findNullRow(DataTable dt)
   {
       int row = 0;

       for (int a = 0; a < dt.Rows.Count; a++)
       {
           if (dt.Rows[a][0] == null)
           {
               row = a;
               break;
           }
       }

       return row;
   }

   public static int findNullColumn(DataTable dt)
   {
       int col = 0;

       for (int i = 0; i < dt.Columns.Count; i++)
       {
           if (dt.Rows[0][i] == null)
           {
               col = i;
               break;
           }
       }

       return col;
   }

The alternative is work iteratively across the first row of the spreadsheet pulling out the individual cell values and testing to find the first null column. Do the same for the first column of data until you find the first null row.

My feeling is that the first method would be quicker unless the potential number of rows is very large.

نصائح أخرى

Following links will Help You for reading the data up to last edited range. SpreadSheetGear - read unique values from Excel column and Get the number of rows of data with SpreadSheetGear?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top