C#處理Excel,讀取

            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            Workbooks wbks = app.Workbooks;
            object missing = System.Reflection.Missing.Value;
            string excelPath = System.Windows.Forms.Application.StartupPath + @"\template\source.xlsx";

            _Workbook _wbk = wbks.Open(excelPath, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing);
            app.Visible = true;
            Sheets shs = _wbk.Sheets;
            _Worksheet _wsh = (_Worksheet)shs.get_Item(1);


            Range rng1 = _wsh.Cells.get_Range("A2", "F29");
            object[,] arrayItem = (object[,])rng1.Value2;
int unit_id = 0;
            object[] lastLine = new object[6];

            for (int i = 1; i <= arrayItem.GetLength(0); i++)
            { //每一行進行循環
                object[] tempOriginal = new object[6];
                for (int j = 1; j <= arrayItem.GetLength(1); j++)
                    tempOriginal[j - 1] = arrayItem[i, j];//取值

                doSomething();

            }

            _wbk.Close();


            wbks.Close();
            wbks = null;
            app.Quit();
            app = null;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章