颜色适用于所有单元 [英] Colour applying to all cells

查看:93
本文介绍了颜色适用于所有单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将颜色应用于Spreadsheet Gear生成的电子表格中的某些单元格.

I am trying to apply colours to certain cells in a spreadsheet generated by Spreadsheet Gear.

我当前的代码

        var workbook = Factory.GetWorkbook();
        var worksheet = workbook.Worksheets["Sheet1"];
        // Set the new worksheet name
        worksheet.Name = "Group export";

        var cells = worksheet.Cells;

        cells[0, 0].Style.Font.Size = 20;

        cells[5, 0].Value = "Default Values";
        cells["A6"].Style.Interior.Color = System.Drawing.Color.FromArgb(166,166,166); //Line 86

但是,当打开电子表格时,我发现字体大小和单元格颜色已应用于电子表格中的每个单元格. 默认值"单元格仅在正确的单元格中,但是我在工作表中的任意位置应用的任何背景或字体样式均适用于所有单元格.

However, when opening the spreadsheet, I find that the font size and cell colour is applied to every single cell in the spreadsheet. The "Default Values" cell is only in the correct cell, however any background or font styling I apply anywhere in sheet applies to all cells.

我设置了一个监视cell ["A6"].Style.Interior.Color和cell ["A5"].Style.Interior.Color的监视器,并在第86行之后直接设置了一个断点,以确认这是样式所在发生.

I set up a watch for cells["A6"].Style.Interior.Color and cells["A5"].Style.Interior.Color, and a breakpoint directly after line 86 to confirm that this is where the styling happens.

为什么样式适用于电子表格中的所有单元格?

Why is the styling applying to all cells in the spreadsheet?

推荐答案

问题是您正在为 style 设置Interior定义(请参见IRange.内部).在样式级别设置任何属性时,它将影响使用同一样式的所有其他单元格.

The problem is that you are setting the Interior definition for the style (see IRange.Style.Interior and the IStyle interface) that the cell is using and not directly setting the interior for the cell itself (IRange.Interior). When you set any property at the style level, it will affect all other cells that use that same style.

可以从Excel的 Ribbon>主页>样式中获得正常",不良",良好"等信息.)默认情况下,所有单元格都使用常规"样式,因此,通过设置IRange.Style.Interior,您可以为所有使用常规"样式的单元格设置颜色,即工作簿中的所有单元格.

Think "Normal", "Bad", "Good", etc., available from Excel's Ribbon > Home > Styles). By default, all cells utilize the "Normal" style, so by setting IRange.Style.Interior you are setting the color for all cells that utilize the "Normal" style--namely all cells in the workbook.

要使用该样式设置不影响其他任何单元格的单个单元格颜色,您需要设置

To set individual cell colors that don't affect any other cells using that style you would need to set the Interior directly under IRange, such as:

cells[0, 0].Font.Size = 20;
cells["A6"].Interior.Color = System.Drawing.Color.FromArgb(166,166,166);

这篇关于颜色适用于所有单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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