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;

 

posted @ 2022-11-18 09:49  一年变大牛  阅读(78)  评论(0编辑  收藏  举报