如何在 Aspose 中格式化单元格

How to format cells in Aspose

我想在特定单元格中将一段文本设为粗体,但无法做到。 这是我使用的代码:

  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");
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");