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

可能的使用场景

当您在单元格中放入具有前导撇号或单引号标记的值时,Microsoft Excel会隐藏它,但当您选择单元格时,它会在公式栏中显示前导撇号或单引号,如下面的屏幕截图所示。

todo:image_alt_text

Aspose.Cells也会隐藏前导撇号或单引号,但它将**Style.QuotePrefix**设置为**true**,以表示该单元格。如果您设置单元格的空样式,则**Style.QuotePrefix**再次变为**false**。为了解决此问题,Aspose.Cells提供了**StyleFlag.QuotePrefix**属性,当其设置为**false**时,**StyleFlag.QuotePrefix**根本不会更新,其旧值将被保留。这意味着**Style.QuotePrefix**属性的旧值如果为**true**,它将保持不变;如果旧值为false,它将保持不变。

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

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

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Create workbook
Workbook wb = new Workbook();
//Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);
//Access cell A1
Cell cell = ws.getCells().get("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
System.out.println("Quote Prefix of Cell A1: " + st.getQuotePrefix());
//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
System.out.println("Quote Prefix of Cell A1: " + st.getQuotePrefix());
//Print information about StyleFlag.QuotePrefix property
System.out.println();
System.out.println("When StyleFlag.QuotePrefix is False, it means, do not update the value of Cell.Style.QuotePrefix.");
System.out.println("Similarly, when StyleFlag.QuotePrefix is True, it means, update the value of Cell.Style.QuotePrefix.");
System.out.println();
//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.setQuotePrefix(false);
//Create a range consisting of single cell A1
Range rng = ws.getCells().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.
System.out.println("Quote Prefix of Cell A1: " + st.getQuotePrefix());
//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.setQuotePrefix(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.
System.out.println("Quote Prefix of Cell A1: " + st.getQuotePrefix());

控制台输出

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