0

使用已安装的 Interop 和 Office 2013 创建和自定义 Excel 文件时,结果以某种方式极其缓慢(超过 5 分钟)。事实上,同样的事情在 Excel 2010 互操作上效果很好(只需 50 秒,完全相同的过程)。(下面的代码片段)

很高兴知道是否有更快的方法来做到这一点。我知道有不同的库可以做到这一点,但我想坚持使用 Interop,因为一切都已经相同了。我首先创建 Excel 文件,然后检查是否有任何空单元格或包含特定字符串的单元格并更改这些单元格的颜色。为了创建 Excel,我使用了 Object 数组并对其进行了解析,这确实更快。将其拉下的主要内容是搜索和更改单元格颜色。

// Check for empty cell and make interior silver color
for (int row = 0; row < rowNo; row++)
{
    for (int col = 0; col < columnNo; col++)
    {
        if (string.IsNullOrEmpty(objData[row, col].ToString()))
        {
            // Access that cell in Excel now and change interior color
            Range cell = (Range)activeSheet.Cells[row + 2, col + 1];
            cell.Interior.Color = System.Drawing.Color.Silver; 
        }
    }
}

// Check for cells contains "column header string#"
for (int col = 16; col < columnNo; col++)
{
    // Get column header - only once and use it for all rows in the same column
    string cellValue = activeSheet.Cells[1, col + 1].Value2.ToString();
    for (int row = 0; row < rowNo; row++)
    {
        string value = objData[row, col].ToString();
        if (string.IsNullOrEmpty(value) || value.Contains(cellValue+"#"))
        {
            Range cell = (Range)activeSheet.Cells[row + 2, col + 1];
            cell.Interior.Color = System.Drawing.Color.Silver;
            cell.Font.Color = System.Drawing.Color.Red;
        }
    }
}
4

1 回答 1

0

我会考虑选择整个范围并设置条件格式,以便空白单元格是您想要的颜色。对不起,没有任何代码给你,我已经有一段时间没有在那个领域玩了。

于 2013-06-21T01:06:50.603 回答