简体   繁体   English

Excel-获取单元格格式很慢

[英]Excel - Getting cell formatting is slow

I'm using C# to pull data from an Excel file. 我正在使用C#从Excel文件中提取数据。 I need to get the text and some minor formatting data from a sheet. 我需要从工作表中获取文本和一些次要格式数据。 My test sheet has 115 rows and 10 columns. 我的测试表有115行和10列。 The performance seems sluggish. 表现似乎呆滞。 If I only pull out the text using the code below it takes about 2 seconds to run. 如果仅使用下面的代码提取文本,则大约需要2秒钟才能运行。 If I check the font (in the if(c.Font.Bold==null..... line) it goes up to 8 seconds. If I get the borders info then it goes up to 17 seconds. 如果我检查字体(在if(c.Font.Bold == null .....行中),则最多需要8秒。如果我获得边框信息,则最多需要17秒。

The problem is that I'll have many, many sheets I need to pull data from and speed will become an issue. 问题是我将需要从很多表中提取数据,而速度将成为一个问题。 Any suggestions on what I can do to speed this up? 关于我可以做些什么来加快它的任何建议? I really appreciate any help. 我非常感谢您的帮助。

foreach (Range c in oSheet.UsedRange.Cells)
{
    var txt = c.Text;
    if (c.Font.Bold == null || c.Font.Italic == null || Convert.ToInt32(c.Font.Underline) > 0 || Convert.ToBoolean(c.Font.Bold) || Convert.ToBoolean(c.Font.Italic))
        txt = "";

    var borderBottom = c.Borders.Item[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle;
    var borderTop = c.Borders.Item[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].LineStyle;
    var borderLeft = c.Borders.Item[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle;
    var borderRight = c.Borders.Item[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle;
}

If your Excel file is a Excel 2007/2010 file (.xlsx), you can use ExcelPackage or EPPlus components to read the file. 如果您的Excel文件是Excel 2007/2010文件(.xlsx),则可以使用ExcelPackageEPPlus组件读取文件。 They are mush faster that office interop. 他们比办公室互操作更快。

I used EPPlus and it iterated over 2000 cell almost instantly! 我使用了EPPlus ,几乎立即迭代了2000多个单元!

ExcelPackage ep = new ExcelPackage(new FileStream(path, FileMode.Open, FileAccess.Read));
var sheet = ep.Workbook.Worksheets[1];
foreach (var cell in sheet.Cells[sheet.Dimension.Address])
{
    var txt = cell.Text;
    var font = cell.Style.Font;
    if (!font.Bold || font.Italic || font.UnderLine)
        txt = "";
    var borderBottom = cell.Style.Border.Bottom.Style;
    var borderTop = cell.Style.Border.Top.Style;
    var borderLeft = cell.Style.Border.Left.Style;
    var borderRight = cell.Style.Border.Right.Style;
    // ...
}

I'm not at all familiar with C#, but in VBA I use Application.ScreenUpdating property set to false on the start and set back to true when finished. 我一点都不熟悉C#,但是在VBA中,我使用Application.ScreenUpdating属性在开始时设置为false,并在完成时设置为true。 In general case this dramatically increases speed, especially if macro performs any visible sheets updates. 在一般情况下,这会极大地提高速度,特别是如果宏执行任何可见的工作表更新时。

I'm pretty sure such property should be available in C# as well. 我很确定此类属性也应在C#中可用。 Hope that was helpful) 希望对您有所帮助)

You could use the below steps. 您可以使用以下步骤。 This is very fast and one line code ( no need of loops and all). 这非常快,只需一行代码(无需循环等所有操作)。 I am taking a simple excel to explain here : 我在这里用一个简单的excel来解释:

Before 之前

在此处输入图片说明

I managed to store the range as A1:C4 in a variable dynamically in exRange and used the below code to give border 我设法将范围作为A1:C4存储在exRange中的动态变量中,并使用以下代码给边框

((Range)excelSheet.get_Range(exRange)).Cells.Borders.LineStyle = XlLineStyle.xlContinuous;


After

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM