如何通过C#在.xlsx文件中逐个读取列 [英] How to read columns one by one in .xlsx file through C#

查看:189
本文介绍了如何通过C#在.xlsx文件中逐个读取列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.xlsx文件中有两列具有不同的IP集合,我想用c#ping这些ip,并且A列中的所有IP都可用,然后用B列重复。



我完成了ping代码,但坚持用C#读取.xslx文件中的值#



下面是参考代码我上了网。



请建议我如何逐个读取列值并执行我的ping任务!!!



我尝试过:



I have two columns in .xlsx file having different set of IP's, i want to ping those ip with c# with all the IP's available in column A one by one and then repeat the same with column B.

I am done with ping code, but stuck with reading values from .xslx file with C#

Below is the reference code i got on net.

Please suggest how can i read column values one by one and execute my ping task!!!

What I have tried:

Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"E:\Test.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            for (int i = 1; i <= rowCount; i++)
            {
                for (int j = 1; j <= colCount; j++)
                {
                    string data = xlRange.Cells[i, j].Value2.ToString();
                    MessageBox.Show(data);
                }
            }

推荐答案

引用:

下面是我在网上获得的参考代码。

Below is the reference code i got on net.

它做了什么?

简单:它读取每一行,并在每行内,它读取每一列。

所以你要做的就是删除内部循环并用一行代码替换它,这些代码访问你感兴趣的一列,适当地转换它以匹配你的ping代码,然后传递它到您现有的ping代码进行处理。



老实说,如果你不能用你所展示的简单代码为自己解决这个问题,那么你需要回到C#课程的第一原则并正确学习它们。 发展的概念就像这个词所暗示的那样:系统地运用科学和技术知识来满足特定的目标或要求。 BusinessDictionary.com [ ^ ]

这与有一个不一样快速谷歌并放弃,如果我找不到完全正确的代码。

你永远找不到合适的代码来满足你的需求 - 你应该能够自己写,或者理解它足以修改现有代码,以满足您的需求。

And what does it do?
Simple: it reads each row, and inside each row, it reads each column.
So all you have to do is remove the inner loop and replace it with a line of code that accesses the one column you are interested in, convert it appropriately to match your ping code, and pass it to your existing ping code for processing.

To be honest, if you can't work that out for yourself with a simple piece of code like you show, then you need to go right back to first principles in your C# course and learn them properly. The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
You will never find exactly the right code to fit your needs - you are expected to be able to either write it yourself, or understand it well enough to modify existing code so it fits your needs.


这篇关于如何通过C#在.xlsx文件中逐个读取列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆