2015年8月30日日曜日

Excel 100×10のデータを取得する方法の比較

A1~J100の範囲のデータを取得する

Case1:Cellsでぐるぐる回しながら取得する
Case2:get_Rangeでぐるぐる回しながら取得する
Case3:範囲のデータを1度に取得する

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Excel.Application xlApplication = null;
            Excel.Workbooks xlWorkbooks = null;
            Excel.Workbook xlWorkbook = null;
            Excel.Sheets xlSheets = null;
            Excel.Worksheet xlWorksheet = null;

            try
            {
                xlApplication = new Excel.Application();
                xlApplication.Visible = true;
                xlWorkbooks = xlApplication.Workbooks;
                xlWorkbook = xlWorkbooks.Add();
                xlSheets = xlWorkbook.Sheets;
                xlWorksheet = xlSheets[1];

                Case1(xlWorksheet);
                Case2(xlWorksheet);
                Case3(xlWorksheet);

                xlWorkbooks.Close();
                xlApplication.Quit();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (xlSheets != null)
                {
                    Marshal.FinalReleaseComObject(xlSheets);
                }

                if(xlWorkbook != null)
                {
                    Marshal.FinalReleaseComObject(xlWorkbook);
                }

                if (xlWorkbooks != null)
                {
                    Marshal.FinalReleaseComObject(xlWorkbooks);
                }

                if (xlApplication != null)
                {
                    Marshal.FinalReleaseComObject(xlApplication);
                }
            }
        }

        static void Case1(Excel.Worksheet xlWorksheet)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            Excel.Range xlRange = null;
            for (int r = 1; r <= 100; r++)
            {
                for (int c = 1; c <= 10; c++)
                {
                    xlRange = xlWorksheet.Cells[r, c];
                    Marshal.FinalReleaseComObject(xlRange);
                }
            }
            sw.Stop();
            Console.WriteLine("[{0}]{1}", MethodBase.GetCurrentMethod().Name, sw.Elapsed);
        }

        static void Case2(Excel.Worksheet xlWorksheet)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            Excel.Range xlRange = null;
            string[] column = new string[]{ string.Empty, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
            for (int r = 1; r <= 100; r++)
            {
                for (int c = 1; c <= 10; c++)
                {
                    xlRange = xlWorksheet.get_Range(column[c] + r.ToString());
                    Marshal.FinalReleaseComObject(xlRange);
                }
            }
            sw.Stop();
            Console.WriteLine("[{0}]{1}", MethodBase.GetCurrentMethod().Name, sw.Elapsed);
        }

        static void Case3(Excel.Worksheet xlWorksheet)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            object[,] cellData = new object[100, 10];
            cellData = xlWorksheet.get_Range("A1", "J100").Value;

            sw.Stop();
            Console.WriteLine("[{0}]{1}", MethodBase.GetCurrentMethod().Name, sw.Elapsed);
        }
    }
}
[Case1]00:00:24.1975109
[Case2]00:00:06.7251560
[Case3]00:00:00.0383916

比較するまでもないよな。。。

0 件のコメント:

コメントを投稿