Advertisement

01.07.2008 at 10:21AM PST, ID: 23064288
[x]
Attachment Details

Finding the Number of Rows/Columns in Excel Sheet

Asked by ViceroyFizzlebottom in Microsoft Visual C#.Net, .Net Editors & IDEs

Tags: Microsoft`, C#

Thanks in advance for any assistance. Basically, I am trying to create a simple little application that will allow me to pick several Excel files (with the same schema) and then concatenate all these files into one large master Excel file (or even tab delimited text.)

My only real obstacle in this whole process is being able to accurately identify the number of rows any particular file has. In the attached code snippet, I am using a function I found on Google, however it doesn't return an accurate count.

Any assistance on fixing my current implementation, or maybe pointing out a better way to do it would be greatly appreciated.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
wb = excel.Workbooks.Open(file.FullName,
                                                missing,
                                                missing,
                                                missing,
                                                missing,
                                                missing,
                                                missing,
                                                missing,
                                                missing,
                                                missing,
                                                missing,
                                                missing,
                                                missing,
                                                missing,
                                                missing);
                    Sheets sheets = wb.Worksheets;
                    Worksheet worksheet = (Worksheet)sheets.get_Item(1);
 
                    int iNumRows = GetNumRows(ref worksheet);
 
                    for (int i = 1; i < iNumRows; i++)
                    {
                        Range range =  worksheet.get_Range("A"+i.ToString(), "V"+i.ToString());
                        Array arrRangeValues = (Array)range.Cells.Value2;
                        
                        string[,] strArray = new string[1, arrRangeValues.Length];
                        Array.Copy(arrRangeValues, strArray, arrRangeValues.Length);
 
                        lstList.Add(strArray);
                    }
 
private static int GetNumRows(ref Worksheet worksheet)
        {
            int iLastRow = worksheet.Rows.CurrentRegion.Count;
            return iLastRow;
        }
[+][-]01.07.2008 at 03:20PM PST, ID: 20604283

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C#.Net, .Net Editors & IDEs
Tags: Microsoft`, C#
Sign Up Now!
Solution Provided By: galar71
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628