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 @   一年变大牛  阅读(81)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示