Excel工作簿-从C#读取速度很慢? [英] Excel Work Book - Read from C# substantially slow ?

查看:739
本文介绍了Excel工作簿-从C#读取速度很慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试从excel工作簿中读取内容,并发现读取具有3560行和7列的工作表需要花费很长时间,大约需要1分钟17秒.我所做的就是遍历整个工作表并将值存储在列表中.

was experimenting with reading from an excel workbook and noticed it takes a long time to read a sheet with 3560 rows and 7 columns, about 1 minute and 17 seconds. All I did was loop through the whole sheet and store the values in a list.

这是正常现象吗,还是我做错了什么事?

Is this normal, or am I doing something wrong ?

 static void Main(string[] args)
    {
        List<string> testList = new List<string>();
        Excel.Application excelApp = new Excel.Application();
        Excel.Workbook workbook = excelApp.Workbooks.Open(@"C:\Users\rnewell\Desktop\FxData.xlsx");
        Excel.Worksheet worksheet = workbook.Sheets[1];
        Excel.Range range = worksheet.UsedRange;

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



        int rowCounter = 1;
        int colCounter = 1;

        while (rowCounter < rowCount)
        {
            colCounter = 1;
            while (colCounter <= colCount)
            {
                //Console.Write(range.Cells[rowCounter, colCounter].Value2.ToString() + " ");
                testList.Add(range.Cells[rowCounter, colCounter].Value2.ToString());
                colCounter++;
            }
            Console.WriteLine();
            rowCounter++;


        }



        Console.ReadKey();
        excelApp.Workbooks.Close();


    }

推荐答案

由于您是从Open XML(* .xlsx)文件格式加载数据,因此建议您使用非交互式.

Since you are loading data from the Open XML (*.xlsx) file format, I would suggest you use Open XML SDK. It doesn't start Excel in the background which is always a good thing, in particular if you need to run your code non-interactively.

我还针对您在Excel中访问数据的不同方法撰写了博客文章.可能会发现有用.

I've also written a blog post on different methods of accessing data in Excel which you might find useful.

这篇关于Excel工作簿-从C#读取速度很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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