开发者

start filling a two dimensional array from [0,0] in C#

i am retrieving information from an excel sheet, i want to take the information from the excel sheet and insert it in a 2 dementional array so i can use it later. it always start filling from the开发者_C百科 row [1,1] and ignore the rest. any idea how i can make it start from [0,0]. this is the code i am using.

if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {

                        var strs = openFileDialog1.InitialDirectory;
                        var name = openFileDialog1.FileName;

                        var path = strs + name;

                        Excel.Application xlApp;
                        Excel.Workbook xlWorkBook;
                        Excel.Worksheet xlWorkSheet;
                        Excel.Range range;

                        string str = "";
                        int rCnt = 0;
                        int cCnt = 0;

                        xlApp = new Excel.Application();
                        xlWorkBook = xlApp.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                        range = xlWorkSheet.UsedRange;
                        Hashtable myHT = new Hashtable();

                        int countRow = 0;
                        string[,] myArray;
                        myArray = new string[range.Rows.Count, range.Columns.Count];

                        for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
                        {
                            for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                            {
                                //str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                                myArray[cCnt, rCnt] = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                                //MessageBox.Show(str);
                            }
                            countRow++;
                        }

                        label1.Text = Convert.ToString(countRow);

                        xlWorkBook.Close(true, null, null);
                        xlApp.Quit(); 


Simply change the line:

myArray[cCnt, rCnt] = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;

to:

myArray[cCnt - 1, rCnt - 1] = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;


The UsedRange property is apparently pretty quirky, and doesn't always return the correct range. Here's a link with an alternative:

http://www.mrexcel.com/archive/VBA/15835.html


It seems to me like you just need to offset the indexing into the spreadsheet:

for (int row = 0; row < range.Rows.Count; row++)
{
    for (int column; column < range.Columns.Count; column++)
    {
        // Offset indexing into range.Cells as Excel is 1-based
        Excel.Range range = (Excel.Range) range.Cells[row + 1, column + 1];
        myArray[column, row] = (string) range.Value2;
    }
}

(I've removed the countRow bit as you can get that from range.Rows.Count. I've also renamed the variables for clarity, and refactored the body of the loop slightly.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜