保留 Cell 值或范围的单引号前缀

可能的使用场景

当您在具有前导撇号或单引号的单元格内放置一些值时,Microsoft Excel 会将其隐藏,但当您选择该单元格时,它会在公式栏中显示前导撇号或单引号,如以下屏幕截图所示。

待办事项:图片_替代_文本

Aspose.Cells 也像 Microsoft Excel 一样隐藏了前导撇号或单引号,但它设置了样式.QuotePrefix作为真的对于那个细胞。如果您设置单元格的空样式,则样式.QuotePrefix成为错误的再次。为了处理这个问题,Aspose.Cells提供StyleFlag.QuotePrefix属性,当它被设置时错误的, 然后样式.QuotePrefix根本不更新,它的旧值被保留。这意味着如果旧值样式.QuotePrefix财产是真的 , 它将保留真的如果旧值是错误的 , 它将保留错误的.

保留 Cell 值或范围的单引号前缀

下面的示例代码解释了StyleFlag.QuotePrefix属性如前所述。请阅读代码中的注释并查看下面给出的代码的控制台输出以获得更多帮助。

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Create workbook
Workbook wb = new Workbook();
//Access first worksheet
Worksheet ws = wb.Worksheets[0];
//Access cell A1
Cell cell = ws.Cells["A1"];
//Put some text in cell, it does not have Single Quote at the beginning
cell.PutValue("Text");
//Access style of cell A1
Style st = cell.GetStyle();
//Print the value of Style.QuotePrefix of cell A1
Console.WriteLine("Quote Prefix of Cell A1: " + st.QuotePrefix);
//Put some text in cell, it has Single Quote at the beginning
cell.PutValue("'Text");
//Access style of cell A1
st = cell.GetStyle();
//Print the value of Style.QuotePrefix of cell A1
Console.WriteLine("Quote Prefix of Cell A1: " + st.QuotePrefix);
//Print information about StyleFlag.QuotePrefix property
Console.WriteLine();
Console.WriteLine("When StyleFlag.QuotePrefix is False, it means, do not update the value of Cell.Style.QuotePrefix.");
Console.WriteLine("Similarly, when StyleFlag.QuotePrefix is True, it means, update the value of Cell.Style.QuotePrefix.");
Console.WriteLine();
//Create an empty style
st = wb.CreateStyle();
//Create style flag - set StyleFlag.QuotePrefix as false
//It means, we do not want to update the Style.QuotePrefix property of cell A1's style.
StyleFlag flag = new StyleFlag();
flag.QuotePrefix = false;
//Create a range consisting of single cell A1
Range rng = ws.Cells.CreateRange("A1");
//Apply the style to the range
rng.ApplyStyle(st, flag);
//Access the style of cell A1
st = cell.GetStyle();
//Print the value of Style.QuotePrefix of cell A1
//It will print True, because we have not updated the Style.QuotePrefix property of cell A1's style.
Console.WriteLine("Quote Prefix of Cell A1: " + st.QuotePrefix);
//Create an empty style
st = wb.CreateStyle();
//Create style flag - set StyleFlag.QuotePrefix as true
//It means, we want to update the Style.QuotePrefix property of cell A1's style.
flag = new StyleFlag();
flag.QuotePrefix = true;
//Apply the style to the range
rng.ApplyStyle(st, flag);
//Access the style of cell A1
st = cell.GetStyle();
//Print the value of Style.QuotePrefix of cell A1
//It will print False, because we have updated the Style.QuotePrefix property of cell A1's style.
Console.WriteLine("Quote Prefix of Cell A1: " + st.QuotePrefix);

控制台输出

Quote Prefix of Cell A1: False

Quote Prefix of Cell A1: True

When StyleFlag.QuotePrefix is False, it means, do not update the value of Cell.Style.QuotePrefix.

Similarly, when StyleFlag.QuotePrefix is True, it means, update the value of Cell.Style.QuotePrefix.

Quote Prefix of Cell A1: True

Quote Prefix of Cell A1: False