2012-03-10

Excel.Range To Array, Array To Excel.Range

Range to Array



        private void btnRange2Array_Click(object sender, EventArgs e)

        {
            Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
            Excel.Range r = ws.get_Range("A1", "B3");
            System.Array arr = (System.Array)r.Value2;
            string[] ar= new string[arr.Length];
            int i = ar.GetLowerBound(0);
            foreach (var item in arr)
                ar[i++] = item!=null? item.ToString():String.Empty;

            txtResults.Text = String.Join(",", ar);

        }



Array to Range




        private void btnArray2Range_Click(object sender, EventArgs e)
        {
            string[,] arr = new string[,] { {"AA", "BB"}, {null,"DD"},{"EE","FF"}};
            Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
            Excel.Range r = ws.get_Range("A1", "B3");
            r.Value2 = arr;
        }


沒有留言:

adsense