C# 如何在Aspose中格式化单元格

C# 如何在Aspose中格式化单元格,c#,aspose,aspose-cells,C#,Aspose,Aspose Cells,我试图在一个特定的单元格中加粗一段文字,但无法。 这是我正在使用的代码: Style boldStyle = workBook.CreateStyle(); boldStyle.BackgroundColor = Color.Red; StyleFlag boldStyleFlag = new StyleFlag(); boldStyleFlag.HorizontalAlignment =true ;

我试图在一个特定的单元格中加粗一段文字,但无法。 这是我正在使用的代码:

  Style boldStyle = workBook.CreateStyle();
           boldStyle.BackgroundColor = Color.Red;
            StyleFlag boldStyleFlag = new StyleFlag();
            boldStyleFlag.HorizontalAlignment =true ;
            boldStyleFlag.FontBold = true;
            Cell c = workSheetIntroduction.Cells["B1"];
            c.SetStyle(boldStyle, boldStyleFlag);
试试下面的代码

  Workbook workbook = new Workbook("F:\\test.xlsx");
        Worksheet s = workbook.Worksheets.First();
        Cell c = s.Cells.FirstCell;
        if (c != null)
        {
            Style st = c.GetStyle();
            st.Font.IsBold = true;
            c.SetStyle(st);
        }

        workbook.Save("F:\\test1.xlsx");

为了更好地参考前面的代码,我还添加了测试代码,创建了test.xlsx,并在第一个单元格中添加了一些文本。测试代码并让我知道。
Workbook workBook = new Workbook();

Worksheet workSheetIntroduction = workBook.Worksheets[0];


//This Style object will make the fill color Red and font Bold

Style boldStyle = workBook.CreateStyle();

boldStyle.ForegroundColor = Color.Red;

boldStyle.Pattern = BackgroundType.Solid;

boldStyle.Font.IsBold = true;


//Bold style flag options

StyleFlag boldStyleFlag = new StyleFlag();

boldStyleFlag.HorizontalAlignment = true;

boldStyleFlag.FontBold = true;


//Apply this style to row 1

Row row1 = workBook.Worksheets[0].Cells.Rows[0];

row1.ApplyStyle(boldStyle, boldStyleFlag);



//Now set the style of indvidual cell

Cell c = workSheetIntroduction.Cells["B1"];


Style s = c.GetStyle();

s.ForegroundColor = Color.Red;

s.Pattern = BackgroundType.Solid;

s.Font.IsBold = true;

c.SetStyle(s);


//Save the workbook

workBook.Save("output.xlsx");